Advanced swing container (III)

Source: Internet
Author: User
11.5 jviewport class

Jviewport is rarely used outside jscrollpane. Usually it is located in the middle of jscrollpane and uses the viewportlayout manager to respond to the positioning request for displaying large component in a small space. In addition to the middle of jscrollpane, jviewport can also be used for the row and column headers of jscrollpane.

11.5.1 create jviewport

Jviewport has only one parameter-free constructor: Public jviewport (). Once we create a jviewport, we can add components to it through setview (component.

11.5.2 jviewport attributes

Table 11-10 shows the 13 attributes of jviewport. You can also set the layout manager to layout management other than viewportlayout, but it is not recommended because the viewportlayout layout manager can make jviewport work correctly.

Jviewport does not support borders due to the complexity and performance of scrolling. Try to use the setborder (Border) method to set the border to a non-null value, it will throw illegalargumentexception. Because there is no border, The insets attribute is always set to (0, 0, 0 ). We cannot display borders around jviewport, but we can display borders around the component where the view is located. You only need to place a border around the component, or place the component in a jpanel with a border, and then add it to the jviewport. If we do add borders around the component, the border is visible only when the component is visible. If we do not want the border to scroll, we must place the jviewport in a component with its own border like jscrollpane.

Tip: to set the background color displayed in jscrollpane, we need to set the background color of the view area: ascrollpane. getviewport (). setbackground (newcolor ).

The viewsize attribute is based on the size of components in the jviewport ). The viewposition attribute is the upper left corner of the viewrect area. The size of the rectangle area is the extended size of the view area ). If you are confused, Figure 11-18 helps us understand the various attributes in jviewport.

The scrollmode attribute can be set to one of the class constants listed in Table 11-11. In most cases, we can use the default blist_scroll_mode mode.

To move the visible part of the view, you only need to modify the viewposition attribute. This will move viewrect so that we can see different parts of the view. To display this line, the program from 11-5 binds the keyboard shortcut key to the jviewport, so that we can use the arrow keys to move the view. (Normally, jscrollpane gets these keyboard actions .) The main part of the code is required to set the corresponding Input/Action ing. The code displayed in bold is required to move the view.

package swingstudy.ch11; import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.EventQueue;import java.awt.Point;import java.awt.event.ActionEvent; import javax.swing.AbstractAction;import javax.swing.Action;import javax.swing.ActionMap;import javax.swing.Icon;import javax.swing.ImageIcon;import javax.swing.InputMap;import javax.swing.JComponent;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JViewport;import javax.swing.KeyStroke; public class MoveViewSample { public static final int INCREASE = 0; // directionpublic static final int DECREASE = 1; // directionpublic static final int X_AXIS = 0;// axispublic static final int Y_AXIS = 1;// axispublic static final int UNIT = 0; // typepublic static final int BLOCK = 1;// type static class MoveAction extends AbstractAction {JViewport viewport;int direction;int axis;int type;public MoveAction(JViewport viewport, int direction, int axis, int type) {if(viewport == null) {throw new IllegalArgumentException("null viewport not permitted");}this.viewport = viewport;this.direction = direction;this.axis = axis;this.type = type;} public void actionPerformed(ActionEvent event) {Dimension extentSize = viewport.getExtentSize();int horizontalMoveSize = 0;int verticalMoveSize = 0;if(axis == X_AXIS) {if(type == UNIT) {horizontalMoveSize = 1;}else {// type == BLOCKhorizontalMoveSize = extentSize.width;}}else {// axis == Y_AXISif(type == UNIT) {verticalMoveSize = 1;}else {// type = BLOCKverticalMoveSize = extentSize.height;}}if(direction == DECREASE) {horizontalMoveSize = -horizontalMoveSize;verticalMoveSize = -verticalMoveSize;}// translate origin by some amountPoint origin = viewport.getViewPosition();origin.x += horizontalMoveSize;origin.y += verticalMoveSize;// set new viewing originviewport.setViewPosition(origin);}} /** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub Runnable runner = new Runnable() {public void run() {JFrame frame = new JFrame("JViewport Sample");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);Icon icon = new ImageIcon("dog.jpg");JLabel dogLabel = new JLabel(icon);JViewport viewport =  new JViewport();viewport.setView(dogLabel); InputMap inputMap = viewport.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);ActionMap actionMap = viewport.getActionMap(); // up key moves view up unitAction upKeyAction = new MoveAction(viewport, DECREASE, Y_AXIS, UNIT);KeyStroke upKey = KeyStroke.getKeyStroke("UP");inputMap.put(upKey, "up");actionMap.put("up", upKeyAction); // down key moves view down unitAction downKeyAction = new MoveAction(viewport, INCREASE, Y_AXIS, UNIT);KeyStroke downKey = KeyStroke.getKeyStroke("DOWN");inputMap.put(downKey, "down");actionMap.put("down", downKeyAction); // left key moves view left unitAction leftKeyAction = new MoveAction(viewport, DECREASE, X_AXIS, UNIT);KeyStroke leftKey = KeyStroke.getKeyStroke("LEFT");inputMap.put(leftKey, "left");actionMap.put("left", leftKeyAction); // right key mvoes view right unitAction rightKeyAction = new MoveAction(viewport, INCREASE, X_AXIS, UNIT);KeyStroke rightKey = KeyStroke.getKeyStroke("RIGHT");inputMap.put(rightKey, "right");actionMap.put("right", rightKeyAction); // pgup key moves view up blockAction pgUpKeyAction = new MoveAction(viewport, DECREASE, Y_AXIS, BLOCK);KeyStroke pgUpKey = KeyStroke.getKeyStroke("PAGE_UP");inputMap.put(pgUpKey, "pgUp");actionMap.put("pgUp", pgUpKeyAction); // pgdn key moves view down blockAction pgDnKeyAction = new MoveAction(viewport, INCREASE, Y_AXIS, BLOCK);KeyStroke pgDnKey = KeyStroke.getKeyStroke("PAGE_DOWN");inputMap.put(pgDnKey, "pgDn");actionMap.put("pgDn", pgDnKeyAction); // shift-pgup key moves view left blockAction shiftPgUpKeyAction = new MoveAction(viewport, DECREASE, X_AXIS, BLOCK);KeyStroke shiftPgUpKey = KeyStroke.getKeyStroke("shift PAGE_UP");inputMap.put(shiftPgUpKey, "shiftPgUp");actionMap.put("shiftPgUp", shiftPgUpKeyAction); // shift-pgdn key moves view right blockAction shiftPgDnKeyAction = new MoveAction(viewport, INCREASE, X_AXIS, BLOCK);KeyStroke shiftPgDnKey = KeyStroke.getKeyStroke("shift PAGE_DOWN");inputMap.put(shiftPgDnKey, "shiftPgDn");actionMap.put("shiftPgDn", shiftPgDnKeyAction); frame.add(viewport, BorderLayout.CENTER);frame.setSize(300, 200);frame.setVisible(true);}};EventQueue.invokeLater(runner);} }
11.5.3 custom jviewport View

Each installable swing viewer shares the same jviewport appearance through basicviewportui, and there is no actual appearance difference. However, there is still a set of uiresource-related attributes of jviewport, as shown in table 11-12. The jviewport component has four such attributes.

Conclusion 11.6

In this chapter, we discuss some advanced swing containers. For the box class, it is easier to use the boxlayout manager to take into account the minimum size of the component. The optimal size and maximum size are the best possible way to create single-row or single-column components.

For the jsplitpane component, we can create a one-row or one-column component by adding a separator between the two components, and allow users to manually modify the component size by moving the separator.

The jtabbedpane container displays only one component in the contained component set at a time. The displayed component is selected by selecting a label. The label can contain titles, icons, and tooltip text with or without hot keys. This is the popular property page we usually see in the program.

The jscrollpane and jviewport containers allow us to display a large component in a small area. Jscrollpane adds a scroll bar to make the terminal user move the visible part, and jviewport does not add these scroll bars.

In Chapter 12th, we will discuss a single component in the swing library, including jprogressbar, jscrollbar, and shared boundedrangemodel as the jslider of its data model.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.