Dual JList with buttons in between : List « Swing JFC « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Swing JFC » ListScreenshots 
Dual JList with buttons in between
Dual JList with buttons in between

/*
Definitive Guide to Swing for Java 2, Second Edition
By John Zukowski     
ISBN: 1-893115-78-X
Publisher: APress
*/

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.swing.AbstractListModel;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;

public class DualListBox extends JPanel {

  private static final Insets EMPTY_INSETS = new Insets(0000);

  private static final String ADD_BUTTON_LABEL = "Add >>";

  private static final String REMOVE_BUTTON_LABEL = "<< Remove";

  private static final String DEFAULT_SOURCE_CHOICE_LABEL = "Available Choices";

  private static final String DEFAULT_DEST_CHOICE_LABEL = "Your Choices";

  private JLabel sourceLabel;

  private JList sourceList;

  private SortedListModel sourceListModel;

  private JList destList;

  private SortedListModel destListModel;

  private JLabel destLabel;

  private JButton addButton;

  private JButton removeButton;

  public DualListBox() {
    initScreen();
  }

  public String getSourceChoicesTitle() {
    return sourceLabel.getText();
  }

  public void setSourceChoicesTitle(String newValue) {
    sourceLabel.setText(newValue);
  }

  public String getDestinationChoicesTitle() {
    return destLabel.getText();
  }

  public void setDestinationChoicesTitle(String newValue) {
    destLabel.setText(newValue);
  }

  public void clearSourceListModel() {
    sourceListModel.clear();
  }

  public void clearDestinationListModel() {
    destListModel.clear();
  }

  public void addSourceElements(ListModel newValue) {
    fillListModel(sourceListModel, newValue);
  }

  public void setSourceElements(ListModel newValue) {
    clearSourceListModel();
    addSourceElements(newValue);
  }

  public void addDestinationElements(ListModel newValue) {
    fillListModel(destListModel, newValue);
  }

  private void fillListModel(SortedListModel model, ListModel newValues) {
    int size = newValues.getSize();
    for (int i = 0; i < size; i++) {
      model.add(newValues.getElementAt(i));
    }
  }

  public void addSourceElements(Object newValue[]) {
    fillListModel(sourceListModel, newValue);
  }

  public void setSourceElements(Object newValue[]) {
    clearSourceListModel();
    addSourceElements(newValue);
  }

  public void addDestinationElements(Object newValue[]) {
    fillListModel(destListModel, newValue);
  }

  private void fillListModel(SortedListModel model, Object newValues[]) {
    model.addAll(newValues);
  }

  public Iterator sourceIterator() {
    return sourceListModel.iterator();
  }

  public Iterator destinationIterator() {
    return destListModel.iterator();
  }

  public void setSourceCellRenderer(ListCellRenderer newValue) {
    sourceList.setCellRenderer(newValue);
  }

  public ListCellRenderer getSourceCellRenderer() {
    return sourceList.getCellRenderer();
  }

  public void setDestinationCellRenderer(ListCellRenderer newValue) {
    destList.setCellRenderer(newValue);
  }

  public ListCellRenderer getDestinationCellRenderer() {
    return destList.getCellRenderer();
  }

  public void setVisibleRowCount(int newValue) {
    sourceList.setVisibleRowCount(newValue);
    destList.setVisibleRowCount(newValue);
  }

  public int getVisibleRowCount() {
    return sourceList.getVisibleRowCount();
  }

  public void setSelectionBackground(Color newValue) {
    sourceList.setSelectionBackground(newValue);
    destList.setSelectionBackground(newValue);
  }

  public Color getSelectionBackground() {
    return sourceList.getSelectionBackground();
  }

  public void setSelectionForeground(Color newValue) {
    sourceList.setSelectionForeground(newValue);
    destList.setSelectionForeground(newValue);
  }

  public Color getSelectionForeground() {
    return sourceList.getSelectionForeground();
  }

  private void clearSourceSelected() {
    Object selected[] = sourceList.getSelectedValues();
    for (int i = selected.length - 1; i >= 0; --i) {
      sourceListModel.removeElement(selected[i]);
    }
    sourceList.getSelectionModel().clearSelection();
  }

  private void clearDestinationSelected() {
    Object selected[] = destList.getSelectedValues();
    for (int i = selected.length - 1; i >= 0; --i) {
      destListModel.removeElement(selected[i]);
    }
    destList.getSelectionModel().clearSelection();
  }

  private void initScreen() {
    setBorder(BorderFactory.createEtchedBorder());
    setLayout(new GridBagLayout());
    sourceLabel = new JLabel(DEFAULT_SOURCE_CHOICE_LABEL);
    sourceListModel = new SortedListModel();
    sourceList = new JList(sourceListModel);
    add(sourceLabel, new GridBagConstraints(001100,
        GridBagConstraints.CENTER, GridBagConstraints.NONE,
        EMPTY_INSETS, 00));
    add(new JScrollPane(sourceList)new GridBagConstraints(0115.5,
        1, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
        EMPTY_INSETS, 00));

    addButton = new JButton(ADD_BUTTON_LABEL);
    add(addButton, new GridBagConstraints(12120.25,
        GridBagConstraints.CENTER, GridBagConstraints.NONE,
        EMPTY_INSETS, 00));
    addButton.addActionListener(new AddListener());
    removeButton = new JButton(REMOVE_BUTTON_LABEL);
    add(removeButton, new GridBagConstraints(14120.25,
        GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(
            0505)00));
    removeButton.addActionListener(new RemoveListener());

    destLabel = new JLabel(DEFAULT_DEST_CHOICE_LABEL);
    destListModel = new SortedListModel();
    destList = new JList(destListModel);
    add(destLabel, new GridBagConstraints(201100,
        GridBagConstraints.CENTER, GridBagConstraints.NONE,
        EMPTY_INSETS, 00));
    add(new JScrollPane(destList)new GridBagConstraints(2115.5,
        1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
        EMPTY_INSETS, 00));
  }

  public static void main(String args[]) {
    JFrame f = new JFrame("Dual List Box Tester");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    DualListBox dual = new DualListBox();
    dual.addSourceElements(new String[] { "One""Two""Three" });
    dual.addSourceElements(new String[] { "Four""Five""Six" });
    dual.addSourceElements(new String[] { "Seven""Eight""Nine" });
    dual.addSourceElements(new String[] { "Ten""Eleven""Twelve" });
    dual
        .addSourceElements(new String[] { "Thirteen""Fourteen",
            "Fifteen" });
    dual.addSourceElements(new String[] { "Sixteen""Seventeen",
        "Eighteen" });
    dual.addSourceElements(new String[] { "Nineteen""Twenty""Thirty" });
    f.getContentPane().add(dual, BorderLayout.CENTER);
    f.setSize(400300);
    f.setVisible(true);
  }

  private class AddListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      Object selected[] = sourceList.getSelectedValues();
      addDestinationElements(selected);
      clearSourceSelected();
    }
  }

  private class RemoveListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      Object selected[] = destList.getSelectedValues();
      addSourceElements(selected);
      clearDestinationSelected();
    }
  }
}

class SortedListModel extends AbstractListModel {

  SortedSet model;

  public SortedListModel() {
    model = new TreeSet();
  }

  public int getSize() {
    return model.size();
  }

  public Object getElementAt(int index) {
    return model.toArray()[index];
  }

  public void add(Object element) {
    if (model.add(element)) {
      fireContentsChanged(this, 0, getSize());
    }
  }

  public void addAll(Object elements[]) {
    Collection c = Arrays.asList(elements);
    model.addAll(c);
    fireContentsChanged(this, 0, getSize());
  }

  public void clear() {
    model.clear();
    fireContentsChanged(this, 0, getSize());
  }

  public boolean contains(Object element) {
    return model.contains(element);
  }

  public Object firstElement() {
    return model.first();
  }

  public Iterator iterator() {
    return model.iterator();
  }

  public Object lastElement() {
    return model.last();
  }

  public boolean removeElement(Object element) {
    boolean removed = model.remove(element);
    if (removed) {
      fireContentsChanged(this, 0, getSize());
    }
    return removed;
  }
}

           
       
Related examples in the same category
1. Use JList component to display custom objects with ListCellRendererUse JList component to display custom objects with ListCellRenderer
2. An example of JList with a DefaultListModelAn example of JList with a DefaultListModel
3. Create JList from array of string valueCreate JList from array of string value
4. A graphical list selection monitorA graphical list selection monitor
5. Test of the DragGesture classes and JList to see if we can recognize a simple drag gestureTest of the DragGesture classes and JList to see if we can recognize a simple drag gesture
6. extends ListCellRenderer to display iconsextends ListCellRenderer to display icons
7. Add JList to Scroll paneAdd JList to Scroll pane
8. Dual Sample: JList and ComboBoxDual Sample: JList and ComboBox
9. JList selection changed listenerJList selection changed listener
10. Triple List from same data arrayTriple List from same data array
11. List with and without ScrollPane List with and without ScrollPane
12. Set visible row count and fixed cell height and widthSet visible row count and fixed cell height and width
13. List: Shared Data SampleList: Shared Data Sample
14. List Data Event DemoList Data Event Demo
15. How to use the list componentHow to use the list component
16. Create list from list modelCreate list from list model
17. How to create list cell rendererHow to create list cell renderer
18. React to list selection actionReact to list selection action
19. Construct the list componentConstruct the list component
20. Tab list rendererTab list renderer
21. List with textfield inputList with textfield input
22. Sharing a Model between a JList and JComboBoxSharing a Model between a JList and JComboBox
23. Demonstrate Swing ScrollingListDemonstrate Swing ScrollingList
24. Demonstrate Swing JList ListModelDemonstrate Swing JList ListModel
25. Weak ListModel
26. ListModel DemoListModel Demo
27. ModifyModelSample: ListModel DemoModifyModelSample: ListModel Demo
28. ArrayList with a ListModel for ease of use
29. Drag and Drop:JList and ListDrag and Drop:JList and List
30. List selection event
31. JList is a component that displays a list of objects: It allows the user to select one or more items.JList is a component that displays a list of objects: It allows the user to select one or more items.
32. A JTextArea is a multi-line text area that displays plain text.
33. JTextPane component is an advanced component for working with text.
34. Model for a JButton: manage only the state of the button
35. A default button model
36. JList: ListModel and ListSelectionModel. The ListModel handles data. ListSelectionModel works with the GUI.JList: ListModel and ListSelectionModel. The ListModel handles data. ListSelectionModel works with the GUI.
37. A single-selection JList.
38. Listening for Changes to the Items in a JList Component
39. Listening for Changes to the Selection in a JList Component
40. Detecting Double and Triple Clicks on an Item in a JList Component
41. Arranging Items in a JList Component
42. changes the layout orientation so that its items are displayed top-to-bottom and left-to-right.
43. Make the number of visible rows dependent on the height of the list, the visibleRowCount property must be set to 0:
44. Setting the Selection Mode of a JList Component
45. The selected items must be in a contiguous range
46. Multiple ranges of selected items are allowed
47. Setting the Selected Items in a JList Component
48. import javax.swing.JList;
49. Select all the items
50. Clear all selections
51. Select the first item
52. Add another selection - the third item
53. Deselect the first item
54. Select a single item
55. Getting the Selected Items in a JList Component
56. Get the index of the last selected item
57. Determine if the third item is selected
58. Determine if there are any selected items
59. Return the selected item objects:
60. Adding and Removing an Item in a JList Component
61. Append an item
62. Insert an item at the beginning
63. Create a list that allows adds and removes
64. Set method replaces an item
65. Methods are used to remove items
66. Getting the Items in a JList Component
67. Methods are used to find an item:
68. These methods can be used to find the range of visible items:
69. Get index of first visible item
70. Get index of last visible item
71. Setting a Tool Tip for an Item in a JList Component
72. Setting the Dimensions of an Item in a JList Component
73. It is also possible to set the item dimensions using a sample value:
74. Creating a JList Component
75. The items can be arbitrary objects. The toString() method of the objects is displayed in the list component.
76. Determining the Selected JRadioButton in a Button Group
77. A spinner that rolls from the end of a list to beginning
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.