Java Reference
In-Depth Information
If we allowed multiple selections on the list with the SINGLE _ SELECTION _ INTERVAL method,
we could use the getFirstIndex() and getLastIndex() methods to get the range of
index values for the item that may have changed. If on the other hand you employ the
MULTIPLE _ SELECTION _ INTERVAL option, you would need to figure out which items in the
range were actually selected. You could do this by calling the getSelectedIndices()
method or the getSelectedValues() method for the list object. The first of these returns an
int array of index values for selected items, and the second returns an array of type Object
containing references to the selected items.
A JList object doesn't support scrolling directly, but it is scrolling 'aware'. To get a scrollable list, one
with scrollbars, you just need to pass the JList object to the JScrollPane constructor, as we do in
the FontDialog constructor. This creates a pane with scrollbars - either vertical, horizontal, or both,
as necessary. We set a minimum size for the JScrollPane object to limit how small it can be made in
the split pane into which we will insert it in a moment. Note how easy it is to get the mouse wheel
supported for scrolling here. We just call the setWheelScrollingEnabled() method for the scroll
pane with the argument as true and it's done.
Displaying the Selected Font
We will display the selected font in a JLabel object that we place in another JPanel pane. Adding the
following code to the constructor will do this:
public FontDialog(SketchFrame window) {
// Initialization as before...
// Button panel code as before...
// Set up the data input panel to hold all input components as before...
// Add the font choice prompt label as before...
// Set up font list choice component as before...
// Panel to display font sample
JPanel display = new JPanel();
fontDisplay = new JLabel("Sample Size: x X y Y z Z");
fontDisplay.setPreferredSize(new Dimension(300,100));
display.add(fontDisplay);
// Plus the code for the rest of the constructor...
}
We create the JPanel object, display , and add the JLabel object, fontDisplay , to it. Remember,
we update this object in the valueChanged() handler for selections from the list of font names. We
will also be updating it when the font size or style is changed. The fontDisplay object just represents
some sample text. You can choose something different i f you like.
Just for the experience, let's use a split pane to hold the scroll pane containing the list, chooseFont ,
and the display panel.
Using a Split Pane
A JSplitPane object represents a pane with a movable horizontal or vertical split, so that it can hold
two components. The split pane divider can be adjusted by dragging it with the mouse. Here's the
code to do that:
Search WWH ::




Custom Search