Professional Documents
Culture Documents
Java I Lecture 15.pps
Java I Lecture 15.pps
Chapter 12
Basic
Graphical User Interface
Components,
Part II
Icon as is
String as is
Java I--Copyright © 2000 Tom Hunter
JList: How the JList is Drawn (Rendered)
• A JList can display an Icon and a String—but not
both at the same time.
JScrollPane
JList
ListSelectionEvent occurs.
list.setSelectionMode( ListSelectionModel.MULTIPLE_INTERVAL_SELECTION
);
ListSelectionModel.SINGLE_SELECTION);
ListSelectionModel.SINGLE_INTERVAL )
list.addListSelectionListener( this );
I clicked on item[4],
while I held down the
shift key and it shows:
—the lead as 4,
—anchor as 2 (where
I started), and
—array of selected
indexes shows 2,3,4 as
you see.
The
setVisibleRowCount()
method only takes effect when
the list is in a scroll pane
c.add( sp );
}
}
Java I--Copyright © 2000 Tom Hunter
JList
• You recall, the JList delegates its labor to other classes
that actually do the work.
ListModel interface.
ListDataListeners—the controllers
ListDataListener
ListDataListener
to an object that
setSelectedItem(Object item)
Java I--Copyright © 2000 Tom Hunter
JComboBox: Comparison of Delegate Data Types
Delegate JList JComboBox
SelectionModel DefaultListSelectionModel —
KeySelectionManager — JComboBox,
DefaultKeySelectionManager
JList.setListData() method.
JComboBox.setModel( ComboBoxModel )
JComboBox.setEditable( true )
…
comboBox.addItem( “Top” );
comboBox.addItem( “Center” ); This example uses a default constructor.
comboBox.addItem( “Bottom” ); You could also have used:
public JComboBox( ComboBoxModel )
comboBox.setEditable( true );
public JComboBox( Object[] )
public JComboBox( Vector )
ItemListener interface.
cb.getSelectedIndex()
• This method returns an int, which becomes a subscript
for the array, which returns a String, which becomes the
text.
Java I--Copyright © 2000 Tom Hunter
JComboBox: Classic Event Handling
• Event handling in a JComboBox is tricky for several
reasons.
—When their selected item changes—either by
being selected, or by having them be edited—
every JComboBox fires two events.
c.setLayout(new FlowLayout());
c.add(comboBox);
hyperlinkUpdate( HyperlinkEvent h )
HyperlinkEvent:
—public HyperlinkEvent.EventType
getEventType()
editor.addHyperlinkListener(
new HyperlinkListener()
{
public void hyperlinkUpadate( HyperLinkEvent e )
{
editor.setPane( e.getURL() );
}
}
);