|
Java™ by example!
|
|
|
How do I refresh the contents of a JList?
 JListExample.java:
// Created with JBuilder (c) Philip Craiger import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * Title: A JList Programming Example * Description: * Copyright: Copyright (c) 2001 * Company: * @author Dr. Philip Craiger * @version 1.0 */ public class JListExample extends JFrame { private JButton btnMove = new JButton(); private JButton bthRemove = new JButton(); // Model View Controller: We change contents of the JList through // manipulation of a MODEL, not the actual JList. private DefaultListModel model_1 = new DefaultListModel(); private DefaultListModel model_2 = new DefaultListModel(); private JList jList1 = new JList(model_2); private JList jList2 = new JList(model_1); // JLists do not scroll by default. We need to add them to // an encompassing JScrollPane private JScrollPane jScrollPane1 = new JScrollPane(); private JScrollPane jScrollPane2 = new JScrollPane(); public JListExample() { // our constructor super("A JList Example"); // call to super constructor try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { JListExample foo = new JListExample(); foo.setSize(new Dimension(450,200)); foo.show(); } private void jbInit() throws Exception { // Note we are adding and removing JList items from the MODEL // NOT directly from the JList. Because JList is associated with the // model, the JList contents are updated automatically model_1.addElement("Java"); model_1.addElement("Visual Basic"); model_1.addElement("C++"); model_1.addElement("C"); model_1.addElement("Common LISP"); model_1.addElement("Fortran"); model_1.addElement("Pascal"); model_1.addElement("Python"); this.getContentPane().setLayout(null); btnMove.setText("Move >>"); btnMove.setBounds(new Rectangle(150, 35, 98, 36)); btnMove.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { btnMove_actionPerformed(e); } }); bthRemove.setText("<< Remove"); bthRemove.setBounds(new Rectangle(152, 87, 97, 36)); bthRemove.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { bthRemove_actionPerformed(e); } }); jScrollPane1.setBounds(new Rectangle(286, 15, 109, 133)); jScrollPane2.setBounds(new Rectangle(14, 15, 106, 136)); // Allow user to select a single item from JList jList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); jList2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); this.getContentPane().add(jScrollPane2, null); jScrollPane2.getViewport().add(jList2, null); // Must add JList this.getContentPane().add(jScrollPane1, null); // to a JScrollPane this.getContentPane().add(btnMove, null); this.getContentPane().add(bthRemove, null); jScrollPane1.getViewport().add(jList1, null); // JList to JScrollPane jList2.setSelectedIndex(0); } void btnMove_actionPerformed(ActionEvent e) { model_2.addElement(jList2.getSelectedValue()); // change the MODEL model_1.removeElement(jList2.getSelectedValue()); // change the MODEL jList2.setSelectedIndex(0); // Highlight first item in JList } void bthRemove_actionPerformed(ActionEvent e) { model_1.addElement(jList1.getSelectedValue()); // change the MODEL model_2.removeElement(jList1.getSelectedValue()); // change the MODEL jList2.setSelectedIndex(0); // Highlight first item in JList } } |
Further Information
Author of answer:
Comments to this answer are only viewable by members. Login or become a member!
|
|
|
|
|