new JList(ListModel dataModel) (Share model with JComboBox) : JList « javax.swing « Java by API






new JList(ListModel dataModel) (Share model with JComboBox)

  

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

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class MainClass {

  public static void main(final String args[]) {
    final String labels[] = { "A", "B", "C", "D", "E" };
    final DefaultComboBoxModel model = new DefaultComboBoxModel(labels);

    JFrame frame = new JFrame("Shared Data");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JComboBox comboBox1 = new JComboBox(model);
    comboBox1.setMaximumRowCount(5);
    comboBox1.setEditable(true);
    frame.add(comboBox1, BorderLayout.NORTH);
    
    JList jlist = new JList(model);
    JScrollPane scrollPane = new JScrollPane(jlist);
    frame.add(scrollPane, BorderLayout.CENTER);

    JButton button = new JButton("Add");
    frame.add(button, BorderLayout.SOUTH);
    ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        model.addElement("a");
      }
    };
    button.addActionListener(actionListener);


    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}


           
         
    
  








Related examples in the same category

1.JList.VERTICAL_WRAP
2.new JList(Object[] listData)
3.new JList(Vector listData)
4.new JScrollPane(JList list)
5.JList: addListSelectionListener(ListSelectionListener listener)
6.JList: addMouseListener(MouseListener lis)
7.JList: addSelectionInterval(int anchor, int lead)
8.JList: clearSelection()
9.JList: getFirstVisibleIndex()
10.JList: getLastVisibleIndex()
11.JList: getLayoutOrientation()
12.JList: getMaxSelectionIndex()
13.JList: getNextMatch(String prefix, int startIndex, Bias bias)
14.JList: getSelectedIndices()
15.JList: getSelectedValues()
16.JList.getSelectionBackground()
17.JList: getSelectionForeground()
18.JList: getVisibleRowCount()
19.JList: isSelectedIndex(int index)
20.JList: isSelectionEmpty()
21.JList: locationToIndex(Point p)
22.JList: removeSelectionInterval(int index0, int index1)
23.JList: setCellRenderer(ListCellRenderer cellRenderer)
24.JList: setDragEnabled(boolean b)
25.JList: setFixedCellHeight(int height)
26.JList: setFixedCellWidth(int width)
27.JList: setLayoutOrientation(int ori)
28.JList: setModel(ListModel model)
29.JList: setPrototypeCellValue(Object prototypeCellValue)
30.JList: setSelectedIndex(int index)
31.JList: setSelectedValue(Object anObject, boolean shouldScroll)
32.JList: setSelectionInterval(int anchor, int lead)
33.JList: setSelectionMode(int selectionMode)
34.JList: setVisibleRowCount(int count)
35.extends JList (custom tooltops)