Sharing a Model between a JList and JComboBox : 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 
Sharing a Model between a JList and JComboBox
Sharing a Model between a JList and JComboBox

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class ListIt {

  final String partsList[][] { { "Item 1""10""99c" },
      "Item 2""12""$18.99" }"Item 3""1""$10.00" } };

  class PartsListModel extends DefaultListModel implements ComboBoxModel {

    Object currentValue;

    public PartsListModel() {
      for (int i = 0, n = partsList.length; i < n; i++) {
        addElement(partsList[i]);
      }
    }

    // ComboBoxModel methods
    public Object getSelectedItem() {
      return currentValue;
    }

    public void setSelectedItem(Object anObject) {
      currentValue = anObject;
      fireContentsChanged(this, -1, -1);
    }
  }

  class MyLabelRenderer extends JLabel implements ListCellRenderer {
    public MyLabelRenderer() {
      setOpaque(true);
    }

    public Component getListCellRendererComponent(JList list, Object value,
        int index, boolean isSelected, boolean cellHasFocus) {
      if (value != null) {
        String values[] (String[]) value;
        String setting = values[0" / " + values[1" / "
            + values[2];
        setText(setting);
      }
      setBackground(isSelected ? Color.BLUE : Color.WHITE);
      setForeground(isSelected ? Color.WHITE : Color.BLUE);
      return this;
    }
  }

  public ListIt() {
    JFrame f = new JFrame();
    final PartsListModel pcm = new PartsListModel();
    ListCellRenderer lcr = new MyLabelRenderer();
    JList jl = new JList(pcm);
    jl.setCellRenderer(lcr);
    ListSelectionModel lsm = jl.getSelectionModel();
    lsm.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    jl.addListSelectionListener(new ListSelectionListener() {
      public void valueChanged(ListSelectionEvent e) {
        if (!e.getValueIsAdjusting()) {
          String element[] (String[]) pcm.getElementAt(e
              .getFirstIndex());
          System.out.println(element[0" : " + element[1" : "
              + element[2]);
        }
      }
    });
    JScrollPane jsp = new JScrollPane(jl);
    JComboBox jc = new JComboBox(pcm);
    jc.setRenderer(lcr);
    JButton jb = new JButton("Add Merchandise");
    jb.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        pcm.addElement(partsList[(int) (Math.random() * partsList.length)]);
      }
    });
    Container c = f.getContentPane();
    c.add(jsp, BorderLayout.NORTH);
    c.add(jc, BorderLayout.CENTER);
    c.add(jb, BorderLayout.SOUTH);
    f.setSize(250250);
    f.show();
  }

  public static void main(String args[]) {
    new ListIt();
  }
}


           
       
Related examples in the same category
1. An example of the JList component in actionAn example of the JList component in action
2. An example of JList with a DefaultListModelAn example of JList with a DefaultListModel
3. A variation on SimpleListA variation on SimpleList
4. 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
5. A graphical list selection monitorA graphical list selection monitor
6. Test of the DragGesture classes and JList to see if weTest of the DragGesture classes and JList to see if we
7. Dual JList Dual JList
8. ComplexRenderingSample: ListCellRendererComplexRenderingSample: ListCellRenderer
9. List Sample DemoList Sample Demo
10. Dual Sample: JList and ComboBoxDual Sample: JList and ComboBox
11. Selecting JList SampleSelecting JList Sample
12. Triple List SampleTriple List Sample
13. List with and without ScrollPane List with and without ScrollPane
14. SizingSamples: simple List SizingSamples: simple List
15. List: Shared Data SampleList: Shared Data Sample
16. List Data Event DemoList Data Event Demo
17. How to use the list componentHow to use the list component
18. Create list from list modelCreate list from list model
19. How to create list cell rendererHow to create list cell renderer
20. React to list selection actionReact to list selection action
21. Construct the list componentConstruct the list component
22. Tab list rendererTab list renderer
23. List with textfield inputList with textfield input
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
ww__w.___j__av___a_2_s_.c___o__m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.