An example of JList with a DefaultListModel : List « Swing JFC « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Swing JFC » ListScreenshots 
An example of JList with a DefaultListModel
An example of JList with a DefaultListModel

/*
Java Swing, 2nd Edition
By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole
ISBN: 0-596-00408-7
Publisher: O'Reilly 
*/

// ListModelExample.java
//An example of JList with a DefaultListModel that we build up at runtime.
//

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class ListModelExample extends JPanel {

  JList list;

  DefaultListModel model;

  int counter = 15;

  public ListModelExample() {
    setLayout(new BorderLayout());
    model = new DefaultListModel();
    list = new JList(model);
    JScrollPane pane = new JScrollPane(list);
    JButton addButton = new JButton("Add Element");
    JButton removeButton = new JButton("Remove Element");
    for (int i = 0; i < 15; i++)
      model.addElement("Element " + i);

    addButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        model.addElement("Element " + counter);
        counter++;
      }
    });
    removeButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (model.getSize() 0)
          model.removeElementAt(0);
      }
    });

    add(pane, BorderLayout.NORTH);
    add(addButton, BorderLayout.WEST);
    add(removeButton, BorderLayout.EAST);
  }

  public static void main(String s[]) {
    JFrame frame = new JFrame("List Model Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new ListModelExample());
    frame.setSize(260200);
    frame.setVisible(true);
  }
}
           
       
Related examples in the same category
1. An example of the JList component in actionAn example of the JList component in action
2. A variation on SimpleListA variation on SimpleList
3. A simple example of a JList object built from an array of StringsA simple example of a JList object built from an array of Strings
4. A graphical list selection monitorA graphical list selection monitor
5. Test of the DragGesture classes and JList to see if weTest of the DragGesture classes and JList to see if we
6. Dual JList Dual JList
7. ComplexRenderingSample: ListCellRendererComplexRenderingSample: ListCellRenderer
8. List Sample DemoList Sample Demo
9. Dual Sample: JList and ComboBoxDual Sample: JList and ComboBox
10. Selecting JList SampleSelecting JList Sample
11. Triple List SampleTriple List Sample
12. List with and without ScrollPane List with and without ScrollPane
13. SizingSamples: simple List SizingSamples: simple List
14. List: Shared Data SampleList: Shared Data Sample
15. List Data Event DemoList Data Event Demo
16. How to use the list componentHow to use the list component
17. Create list from list modelCreate list from list model
18. How to create list cell rendererHow to create list cell renderer
19. React to list selection actionReact to list selection action
20. Construct the list componentConstruct the list component
21. Tab list rendererTab list renderer
22. List with textfield inputList with textfield input
23. Sharing a Model between a JList and JComboBoxSharing a Model between a JList and JComboBox
24. Demonstrate Swing ScrollingListDemonstrate Swing ScrollingList
25. Demonstrate Swing JList ListModelDemonstrate Swing JList ListModel
26. Weak ListModel
27. ListModel DemoListModel Demo
28. ModifyModelSample: ListModel DemoModifyModelSample: ListModel Demo
29. ArrayList with a ListModel for ease of use
30. Drag and Drop:JList and ListDrag and Drop:JList and List
w__ww___.___j_a___v_a___2___s___.__c_om_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.