Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Swing Lists.

Similar presentations


Presentation on theme: "Advanced Swing Lists."— Presentation transcript:

1 Advanced Swing Lists

2 Contents List Models Inserting or Removing Values Rendering Values

3 I. List Models The list component uses the model-view- controller design pattern to separate the visual appearance from the underlying data. The JList class is responsible for the visual appearance of the data: It actually knows very little about how the data are stored All it knows is that it can retrieve the data through some object that implements the ListModel interface.

4 The ListModel interface
public interface ListModel { int getSize(); Object getElementAt(int i); void addListDataListener(ListDataListener l); void removeListDataListener(ListDataListener l); } The JList Gets a count of elements Retrieves each one of the elements. Adds itself as a ListDataListener. That way, if the collection of elements changes, the JList gets notified so that it can repaint itself.

5 The ListModel interface
This turns out to be easy to implement. The tedious part, adding and removing listeners, has been done for us in the AbstractListModel class, which we extend. We only need to supply the getSize and getElementAt methods.

6 A Very Long List A list of all three-letter words
There are 26 x 26 x 26 = 17,576 three-letter combinations. Rather than storing all these combinations, we recompute them as requested when the user scrolls through them.

7

8

9

10

11 II. Inserting or Removing Values
The DefaultListModel class implements the ListModel interface and manages a collection of objects. Construct a DefaultListModel object, fill it with the initial values, and associate it with the list. DefaultListModel model = new DefaultListModel(); model.addElement("quick"); model.addElement("brown"); JList list = new JList(model);

12 Now you can add or remove values from the model object.
The model object then notifies the list of the changes, and the list repaints itself. model.removeElement("quick"); model.addElement("slow");

13 III. Rendering Values You can easily represent your list values with any drawing. Install a list cell renderer into the JList object for all custom drawing by calling setCellRenderer. A list cell renderer is any class that implements the following interface: interface ListCellRenderer { Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus); }

14 getListCellRendererComponent
This method is called for each cell. It returns a component that paints the cell contents. The component is placed at the appropriate location whenever a cell needs to be rendered. One way to implement a cell renderer is to create a class that extends JComponent.

15 class MyCellRenderer extends JComponent
class MyCellRenderer extends JComponent implements ListCellRenderer { public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus){ return this; } public void paintComponent(Graphics g) { // paint code goes here } public Dimension getPreferredSize() { // size measurement code goes here } // instance fields }

16

17

18

19


Download ppt "Advanced Swing Lists."

Similar presentations


Ads by Google