You can divide screen space among three or more components by putting split panes inside of split panes Instead of adding the components of interest directly to a split pane, you often
Trang 1PHẦN 2 - SWING
SPECIAL PANE
Trang 2How to Use Scroll Panes
A JScrollPane provides a scrollable view of a component When screen real estate is limited, use a scroll pane to display a component that is large or one whose size can change dynamically
textArea = new JTextArea(5, 30);
JScrollPane scrollPane = new JScrollPane(textArea);
contentPane.setPreferredSize(new Dimension(400, 100)); contentPane.add(scrollPane, BorderLayout.CENTER);
Trang 3How to Use Scroll Panes
Trang 4How to Use Scroll Panes
JRadioButton form[][] = new JRadioButton[12][5];
String counts[] = { "" , "0-1" , "2-5" , "6-10" , "11-100" , "101+" }; String categories[] = {
"Household" , "Office" , "Extended Family" ,
"Company (US)" , "Company (World)" , "Team" ,
"Will" , "Birthday Card List" , "High School" ,
"Country" , "Continent" , "Planet" };
JPanel p = new JPanel( );
p.setSize(600, 400);
p.setLayout( new GridLayout(13, 6, 10, 0));
scrollpane = new JScrollPane( p );
getContentPane( ).add( scrollpane , BorderLayout CENTER );
Trang 5How to Use Scroll Panes
for ( int row = 0; row < 13; row++) {
ButtonGroup bg = new ButtonGroup( );
for ( int col = 0; col < 6; col++) {
if (row == 0) p.add( new JLabel(counts[col]));
else if (col == 0) p.add( new JLabel(categories[row - 1])); else {
form[row - 1][col - 1] = new JRadioButton( );
Trang 6Providing Custom Decorations
Column Header
row Header
Horizotal Scrollbar
Vertical Scrollbar
Trang 7Providing Custom Decorations
Trang 8Providing Custom Decorations
// Add in some JViewports for the column and row headers.
JViewport jv1 = new JViewport( );
jv1.setView( new JLabel( new ImageIcon( "images/blue.gif" )));
// And throw in an information button
JButton jb1 = new JButton( new ImageIcon( "images/open.gif" ));
jb1.addActionListener( new ActionListener( ) {
public void actionPerformed(ActionEvent ae) {
Trang 9Dynamically Changing the Client's Size
Changing the size of a scroll pane's client is a two-step
process First, set the client's preferred size Then,
call revalidate on the client to let the scroll pane know
that it should update itself and its scroll bars
if (changed) {
//Update client's preferred size because the area taken up //by the graphics has gotten larger or smaller (if cleared)
drawingArea.setPreferredSize( /* the new size */ );
//This lets the scroll pane know to update itself
//and its scroll bars
drawingArea.revalidate();
}
Note that when the client changes size, the scroll bars
adjust The scroll pane doesn't resize, nor does the
viewport
Trang 10Dynamically Changing the Client's Size
Trang 11Dynamically Changing the Client's Size
drawingArea = new JPanel() {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Rectangle rect;
for (int i = 0; i < objects.size(); i++) {
rect = (Rectangle)objects.elementAt(i);
g.setColor(colors[(i % color_n)]);
g.fillOval(rect.x, rect.y, rect.width, rect.height);
}
}
};
drawingArea.setBackground(Color.white);
drawingArea.addMouseListener(new MyMouseListener());
//Put the drawing area in a scroll pane
JScrollPane scroller = new JScrollPane(drawingArea);
scroller.setPreferredSize(new Dimension(200,200));
Trang 12Dynamically Changing the Client's Size
//Set up the instructions
JLabel instructionsLeft = new JLabel(
"Click left mouse button to place a circle." );
JLabel instructionsRight = new JLabel(
"Click right mouse button to clear drawing area." ); JPanel instructionPanel = new JPanel( new GridLayout(0,1)); instructionPanel.add(instructionsLeft);
instructionPanel.add(instructionsRight);
//Layout this demo.
setLayout( new BorderLayout());
add(instructionPanel, BorderLayout NORTH );
add(scroller, BorderLayout CENTER );
Trang 13Dynamically Changing the Client's Size
class MyMouseListener extends MouseInputAdapter {
final int W = 100;
final int H = 100;
public void mouseReleased(MouseEvent e) {
boolean changed = false;
Trang 14Dynamically Changing the Client's Size
int this_width = (x + W + 2);
if (this_width > size.width) {
size.width = this_width; changed=true;}
//Update client's preferred size because
//the area taken up by the graphics has
//gotten larger or smaller (if cleared)
drawingArea.setPreferredSize(size);
//Let the scroll pane know to update itself
//and its scrollbars
drawingArea.revalidate();
}
drawingArea.repaint();
}}
Trang 15API: JScrollPane
public JScrollPane( )
public JScrollPane(Component view)
public JScrollPane(Component view, int
verticalScrollBarPolicy, int horizontalScrollBarPolicy)
public JScrollPane(int verticalScrollBarPolicy, int
horizontalScrollBarPolicy)
Create new scrollpanes You can start off by
specifying the view (i.e., the component to scroll), the scrollbar policies, or both Just make sure you get the scrollbar policies in the right order! Of
course, any of these pieces can be specified or
changed after the scrollpane has been created See the setViewportView( ) method later in this
chapter.
Trang 16API: JScrollPane
public void setVerticalScrollBarPolicy(int policy)
Determines when the vertical scrollbar appears in the
scrollpane Legal values are:
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED
JScrollPane.VERTICAL_SCROLLBAR_NEVER
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS
public void setHorizontalScrollBarPolicy(int policy)
Determines when the horizontal scrollbar appears in the scrollpane The options are:
Trang 17API: JScrollPane
public void setViewportView(Component view)
Creates a viewport if necessary and then sets its view JScrollPane scrollpane = new JScrollPane();
scrollpane.setViewportView(myBigComponentToScroll);
public void setCorner(String key,Component corner)
Set the corner specified Legal values for the key are: JScrollPane.LOWER_LEFT_CORNER
JScrollPane.LOWER_RIGHT_CORNER
JScrollPane.UPPER_LEFT_CORNER
JScrollPane.UPPER_RIGHT_CORNER
Trang 18How to Use Split Panes
The JSplitPane component allows you to place two (and only two) components side by side or one on top of the other in a
single pane You can separate the pane horizontally or vertically, and the user can adjust this separator graphically at runtime
You can divide screen space among three or more components
by putting split panes inside of split panes
Instead of adding the components of interest directly to a split pane, you often put each component into a scroll pane You
then put the scroll panes into the split pane This allows the user
to view any part of a component of interest, without requiring the component to take up a lot of screen space or adapt to
displaying itself in varying amounts of screen space
Trang 19SplitPane Example
Trang 20SplitPane Example
Trang 21How to Use Split Panes
Trang 22How to Use Split Panes
//Create a split pane with the two scroll panes in it
splitPane = new
JSplitPane(JSplitPane.HORIZONTAL_SPLIT, listScrollPane, pictureScrollPane);
Trang 23How to Use Split Panes
//Create a split pane with the two scroll panes
splitPane = new
JSplitPane(JSplitPane.HORIZONTAL_SPLIT, listScrollPane, pictureScrollPane);
splitPane.setOneTouchExpandable(true);
splitPane.setDividerLocation(150);
//Provide minimum sizes for the two components
//in the split pane
Dimension minimumSize = new Dimension(100, 50);
listScrollPane.setMinimumSize(minimumSize);
pictureScrollPane.setMinimumSize(minimumSize);
Trang 24How to Use Split Panes
The split pane in this example is split horizontally the two
components appear side by side as specified by the
JSplitPane.HORIZONTAL_SPLIT argument to the constructor
Split pane provides one other option, specified with
JSplitPane.VERTICAL_SPLIT, that places one component above the other You can change the split direction after the split pane has been created with the setOrientation method
Two small arrows appear at the top of the divider in the
example's split pane These arrows let the user collapse (and then expand) either of the components with a single click The current look and feel determines whether these controls appear
by default In the Java Look & Feel, they are turned off by
default The example turned them on with a call to the
setOneTouchExpandable
Trang 25Setting the Components in a Split
You can use any of these methods at any time regardless of
the split pane's current split direction Calls to
setLeftComponent and setTopComponent are equivalent and set the specified component in the top or left position,
depending on the split pane's current split orientation
Similarly, calls to setRightComponent and setBottomComponent are equivalent
Trang 26Nesting Split Panes
Trang 27Nesting Split Panes
public class NestingSplitPaneDemo extends JFrame
implements ListSelectionListener {
private Vector imageNames;
private JLabel picture;
private JList list;
private JSplitPane topSplitPane,splitPane;
private JScrollPane listScrollPane, pictureScrollPane;
private JLabel label;
public NestingSplitPaneDemo(String title) {
super(title);
//Create the list of images and put it in a scroll pane
imageNames = new Vector();
Trang 28Nesting Split Panes
//Set up the picture label and put it in a scroll pane
ImageIcon firstImage = new ImageIcon("images/" +
(String)imageNames.firstElement());
picture = new JLabel(firstImage);
picture.setPreferredSize(new Dimension(firstImage.getIconWidth(), firstImage.getIconHeight()));
pictureScrollPane = new JScrollPane(picture);
//Create a split pane with the two scroll panes in it
topSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
Trang 29Nesting Split Panes
//Provide a preferred size for the split pane
topSplitPane.setPreferredSize(new Dimension(400, 200));
label = new JLabel("Click on an image name in the list.",
JLabel.CENTER);
// Create a split pane and put "top" (topSplitPane) and
// JLabel instance in it.
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topSplitPane, label);
Trang 30Nesting Split Panes
public void valueChanged(ListSelectionEvent e) {
int index = theList.getSelectedIndex();
label.setText("Selected image number " + index);
ImageIcon newImage = new ImageIcon("images/" +
} }
Trang 31API: JSplitPane
public JSplitPane()
public JSplitPane(int newOrientation)
public JSplitPane(int newOrientation, boolean
newContinuousLayout)
public JSplitPane(int newOrientation, Component
newLeftComponent,Component newRightComponent)
public JSplitPane(int newOrientation, boolean
newContinuousLayout, Component newLeftComponent,
Component newRightComponent)
Parameters:
newOrientation - HORIZONTAL_SPLIT or VERTICAL_SPLIT
newContinuousLayout - true for the components to redraw
continuously as the divider changes position, false to wait until the divider position stops changing to redraw
newLeftComponent - the Component that will appear on the left
of a horizontally-split pane, or at the top of a vertically-split panenewRightComponent - the Component that will appear on the right of a horizontally-split pane, or at the bottom of a vertically-
Trang 32API: JSplitPane
Set or get the split pane's orientation Use either
HORIZONTAL_SPLIT or VERTICAL_SPLIT defined in JSplitPane If left unspecified, the split pane will be
Trang 33API: JSplitPane
void setOneTouchExpandable(boolean)
boolean getOneTouchExpandable()
Set or get whether the split pane displays a control on the
divider to expand/collapse the divider The default depends on the look and feel In the Java Look & Feel, it's off by default
Set or get the indicated component Each method works
regardless of the split pane's orientation Top and left are
equivalent, and bottom and right are equivalent
Trang 34API: JSplitPane
void add(Component)
Add the component to the split pane You can add only two
components to a split pane The first component added is the top/left component The second component added is the
bottom/right component Attempt to add more components
Move the divider such that both components are at their
preferred sizes This is how a split pane divides itself at startup, unless specified otherwise
Trang 35How to Use Tabbed Panes
With the JTabbedPane class, you can have several
components (usually panels) share the same space The user chooses which component to view by selecting the tab
corresponding to the desired component
To create a tabbed pane, you simply instantiate
JTabbedPane, create the components you wish it to display, and then add the components to the tabbed pane using the addTab method
Trang 36How to Use Tabbed Panes
Trang 37API: JTabbed
public JTabbedPane()
Creates an empty TabbedPane with a default tab placement of JTabbedPane.TOP
public JTabbedPane(int tabPlacement)
Creates an empty TabbedPane with the specified tab placement
of either: JTabbedPane.TOP, JTabbedPane.BOTTOM,
JTabbedPane.LEFT, or JTabbedPane.RIGHT
Parameters:tabPlacement - the placement for the tabs relative to the content
public int getSelectedIndex()
Returns the currently selected index for this tabbedpane
Returns -1 if there is no currently selected tab
public void setSelectedIndex(int index)
Sets the selected index for this tabbedpane The index must be
a valid tab index or -1, which indicates that no tab should be
selected (can also be used when there are no tabs in the
tabbedpane) If a -1 value is specified when the tabbedpane
contains one or more tabs, then the results will be
Trang 38API: JTabbed
public Component getSelectedComponent()
Returns the currently selected component for this tabbedpane Returns null if there is no currently selected tab
public void insertTab(String title, Icon icon, Component component, String tip, int index)
Inserts a component, at index, represented by a title and/or icon, either of which may be null If icon is non-null and it
implements ImageIcon a corresponding disabled icon will
automatically be created and set on the tabbedpane
public void addTab(String title, Icon icon, Component component, String tip)
public void addTab(String title, Icon icon, Component component)
public void addTab(String title, Component component)
Adds a component and tip represented by a title and/or icon, either of which can be null If icon is non-null and it implements ImageIcon a corresponding disabled icon will automatically be created and set on the tabbedpane Cover method for insertTab
Trang 39API: JTabbed
public void removeTabAt(int index)
Removes the tab at index After the component associated with index is removed, its visibility is reset to true to ensure it will be visible if added to other containers
public int getTabCount()
Returns the number of tabs in this tabbedpane
public void setTitleAt(int index, String title)
Sets the title at index to title which can be null An internal
exception is raised if there is no tab at that index
public void setIconAt(int index, Icon icon)
Sets the icon at index to icon which can be null Does not set disabled icon at icon To set disabled icon, use
setDisableIconAt() An internal exception is raised if there is no tab at that index