Title: How to use list
1How to use list
- A JList presents the user with a group of items,
displayed in one or more columns, to choose from.
- Lists can have many items, so they are often put
in scroll panes. -
- ConstructorsJList(Object) // immutable
JList(Vector) // immutable JList()
// mutable - JList(ListModel) // mutable
2How to Use Models
- Provides flexibility in determining how data is
stored and retrieved. - Most Swing components have models.
- JButton has a ButtonModel object that stores the
button's state - what its keyboard mnemonic is,
- whether it's enabled, selected, or pressed, etc
- Some components have multiple models.
- A JList has
- a ListModel object to hold the list's contents
- a ListSelectionModel object to track the list's
current selection.
3ListModel
- They give you flexibility in determining how data
is stored and retrieved.
ListModel
AbstractListModel
DefaultListModel
4(No Transcript)
5String choices "A", "long", "array", "of",
"strings" list new JList(choice)
list.setSelectionMode(ListSelectionModel.SINGLE_I
NTERVAL_SELECTION) list.setLayoutOrientation(JLis
t.HORIZONTAL_WRAP) JScrollPane listScroller
new JScrollPane(list) listScroller.setPreferredSi
ze(new Dimension(250, 80))
6SINGLE_SELECTION
SINGLE_INTERVAL_SELECTION
MULTIPLE_INTERVAL_SELECTION
7HORIZONTAL_WRAP
VERTICAL_WRAP
VERTICAL
8ListSelectionEvent and ListSelectionListener
- List selection events occur when
- the selection in a list or table is either
changing or has just changed. - List selection events are fired from an object
that implements the ListSelectionModel interface.
- To detect list selection events, you register a
listener on the appropriate list selection model
object. - The JList class also gives you the option of
registering a listener on the list itself.
9The ListSelectionListener Interface
- valueChanged(ListSelectionEvent e)
- Called in response to selection changes.
10The ListSelectionEvent API
- Object getSource()(in java.util.EventObject)
Return the object that fired the event. - int getFirstIndex()
- Return the index of the first item whose
selection value has changed. - int getLastIndex()
- Return the index of the last item whose
selection value has changed. - boolean getValueIsAdjusting()
- Return true if the selection is still
changing.
11- // Fig. 11.23 ListFrame.java
- // Selecting colors from a JList.
- import java.awt.FlowLayout
- import java.awt.Color
- import javax.swing.JFrame
- import javax.swing.JList
- import javax.swing.JScrollPane
- import javax.swing.event.ListSelectionListener
- import javax.swing.event.ListSelectionEvent
- import javax.swing.ListSelectionModel
- public class ListFrame extends JFrame
-
- private JList colorJList // list to display
colors - private final String colorNames "Black",
"Blue", "Cyan", - "Dark Gray", "Gray", "Green", "Light Gray",
"Magenta", - "Orange", "Pink", "Red", "White", "Yellow"
- private final Color colors Color.BLACK,
Color.BLUE, Color.CYAN, - Color.DARK_GRAY, Color.GRAY, Color.GREEN,
Color.LIGHT_GRAY,
12ListSelectionModel listSelectionModel
listSelectionModel colorJList.getSelectionModel
() listSelectionModel.addListSelectionListener(
new ListSelectionListener()
public void valueChanged( ListSelectionEvent
event ) .
// end method valueChanged //
end anonymous inner class ) // end call to
addListSelectionListener
- // ListFrame constructor add JScrollPane
containing JList to JFrame - public ListFrame()
-
- super( "List Test" )
- setLayout( new FlowLayout() ) // set frame
layout - colorJList new JList( colorNames ) //
create with colorNames - colorJList.setVisibleRowCount( 5 ) //
display five rows at once -
- // do not allow multiple selections
- colorJList.setSelectionMode(
ListSelectionModel.SINGLE_SELECTION ) - // add a JScrollPane containing JList to
frame - add( new JScrollPane( colorJList ) )
- colorJList.addListSelectionListener(
- new ListSelectionListener() // anonymous
inner class -
- // handle list selection events
13- // Fig. 11.25 MultipleSelectionFrame.java
- // Copying items from one List to another.
- import java.awt.FlowLayout
- import java.awt.event.ActionListener
- import java.awt.event.ActionEvent
- import javax.swing.JFrame
- import javax.swing.JList
- import javax.swing.JButton
- import javax.swing.JScrollPane
- import javax.swing.ListSelectionModel
- public class MultipleSelectionFrame extends
JFrame -
- private JList colorJList // list to hold
color names - private JList copyJList // list to copy color
names into - private JButton copyJButton // button to copy
selected names - private final String colorNames "Black",
"Blue", "Cyan", - "Dark Gray", "Gray", "Green", "Light Gray",
"Magenta", "Orange", - "Pink", "Red", "White", "Yellow"
14- // MultipleSelectionFrame constructor
- public MultipleSelectionFrame()
-
- super( "Multiple Selection Lists" )
- setLayout( new FlowLayout() ) // set frame
layout - colorJList new JList( colorNames ) //
holds names of all colors - colorJList.setVisibleRowCount( 5 ) // show
five rows - colorJList.setSelectionMode(
- ListSelectionModel.MULTIPLE_INTERVAL_SELE
CTION ) - add( new JScrollPane( colorJList ) ) //
add list with scrollpane - copyJButton new JButton( "Copy gtgtgt" ) //
create copy button - copyJButton.addActionListener(
- new ActionListener() // anonymous inner
class -
- // handle button event
- public void actionPerformed(
ActionEvent event )
15- add( copyJButton ) // add copy button to
JFrame - copyJList new JList() // create list to
hold copied color names - copyJList.setVisibleRowCount( 5 ) // show
5 rows - copyJList.setFixedCellWidth( 100 ) // set
width - copyJList.setFixedCellHeight( 15 ) // set
height - copyJList.setSelectionMode(
- ListSelectionModel.SINGLE_INTERVAL_SELECT
ION ) - add( new JScrollPane( copyJList ) ) // add
list with scrollpane - // end MultipleSelectionFrame constructor
- // end class MultipleSelectionFrame
16ListDataEvent and ListDataListener
- List data events occur when the contents of a
mutable list change. - List data events are fired from an object that
implements the ListModel interface. Therefore you
have to register a list data listener with the
list model object.
17- DefaultListModel listModel new
DefaultListModel() - listModel.addElement("Whistler, Canada")
- listModel.addElement("Jackson Hole, Wyoming")
- listModel.addElement("Squaw Valley,
California") - JList list new JList(listModel) //
JList(ListModel) - listModel.addListDataListener(
- new ListDataListener()
-
- // implementation of ListDataListener methods
-
-
- )
-
18ListDataListener Interface
- intervalAdded(ListDataEvent)
- Called when one or more items have been added to
the list. - intervalRemoved(ListDataEvent)
- Called when one or more items have been removed
from the list. - contentsChanged(ListDataEvent)
- Called when the contents of one or more items in
the list have changed.
19ListDataEvent API
- Object getSource()Return the object that fired
the event. - int getIndex0()
- Return the index of the first item whose value
has changed. - int getIndex1()
- Return the index of the last item whose value
has changed. - int getType()
- Return the event type. The possible values are
CONTENTS_CHANGED, INTERVAL_ADDED, or
INTERVAL_REMOVED.