Example usage for javax.swing JList JList

List of usage examples for javax.swing JList JList

Introduction

In this page you can find the example usage for javax.swing JList JList.

Prototype

public JList(final Vector<? extends E> listData) 

Source Link

Document

Constructs a JList that displays the elements in the specified Vector.

Usage

From source file:events.ListSelectionDemo.java

public ListSelectionDemo() {
    super(new BorderLayout());

    String[] listData = { "one", "two", "three", "four", "five", "six", "seven" };
    String[] columnNames = { "French", "Spanish", "Italian" };
    list = new JList(listData);

    listSelectionModel = list.getSelectionModel();
    listSelectionModel.addListSelectionListener(new SharedListSelectionHandler());
    JScrollPane listPane = new JScrollPane(list);

    JPanel controlPane = new JPanel();
    String[] modes = { "SINGLE_SELECTION", "SINGLE_INTERVAL_SELECTION", "MULTIPLE_INTERVAL_SELECTION" };

    final JComboBox comboBox = new JComboBox(modes);
    comboBox.setSelectedIndex(2);//from   w  ww .  ja v  a 2  s  . co  m
    comboBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String newMode = (String) comboBox.getSelectedItem();
            if (newMode.equals("SINGLE_SELECTION")) {
                listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            } else if (newMode.equals("SINGLE_INTERVAL_SELECTION")) {
                listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
            } else {
                listSelectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
            }
            output.append("----------" + "Mode: " + newMode + "----------" + newline);
        }
    });
    controlPane.add(new JLabel("Selection mode:"));
    controlPane.add(comboBox);

    //Build output area.
    output = new JTextArea(1, 10);
    output.setEditable(false);
    JScrollPane outputPane = new JScrollPane(output, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    //Do the layout.
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    add(splitPane, BorderLayout.CENTER);

    JPanel topHalf = new JPanel();
    topHalf.setLayout(new BoxLayout(topHalf, BoxLayout.LINE_AXIS));
    JPanel listContainer = new JPanel(new GridLayout(1, 1));
    listContainer.setBorder(BorderFactory.createTitledBorder("List"));
    listContainer.add(listPane);

    topHalf.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5));
    topHalf.add(listContainer);
    //topHalf.add(tableContainer);

    topHalf.setMinimumSize(new Dimension(100, 50));
    topHalf.setPreferredSize(new Dimension(100, 110));
    splitPane.add(topHalf);

    JPanel bottomHalf = new JPanel(new BorderLayout());
    bottomHalf.add(controlPane, BorderLayout.PAGE_START);
    bottomHalf.add(outputPane, BorderLayout.CENTER);
    //XXX: next line needed if bottomHalf is a scroll pane:
    //bottomHalf.setMinimumSize(new Dimension(400, 50));
    bottomHalf.setPreferredSize(new Dimension(450, 135));
    splitPane.add(bottomHalf);
}

From source file:Demo.ScatterGraph.java

public ScatterGraph(int sampleNb, List<String> paraType_list) {
    this.sampleNb = sampleNb;
    this.paraType_list = paraType_list;

    // widgets: charts(JFreeChart), parameter selectors (JList)
    DefaultListModel xModel = new DefaultListModel();
    xSelector = new JList(xModel);
    JScrollPane xSelPane = new JScrollPane();
    xSelPane.setViewportView(xSelector);

    DefaultListModel yModel = new DefaultListModel();
    ySelector = new JList(yModel);
    JScrollPane ySelPane = new JScrollPane();
    ySelPane.setViewportView(ySelector);

    charts = new JFreeChart[paraType_list.size()][paraType_list.size()];
    for (int i = 0; i < paraType_list.size(); i++) {
        String para = paraType_list.get(i);
        xModel.addElement(para);//from w w w . j a  v  a  2  s.  c o m
        yModel.addElement(para);

        for (int j = 0; j < paraType_list.size(); j++) {
            charts[i][j] = null;
        }
    }

    charts[0][0] = CreateChart(0, 0);
    chartPane = new ChartPanel(charts[0][0]);

    xSelector.setSelectedIndex(0);
    ySelector.setSelectedIndex(0);

    xSelector.addMouseListener(this);
    ySelector.addMouseListener(this);

    // layout
    GridBagLayout layout = new GridBagLayout();
    setLayout(layout);

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.WEST;
    gbc.fill = GridBagConstraints.BOTH;

    gbc.insets = new Insets(5, 20, 5, 5);
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.weightx = 20;
    gbc.weighty = 12;
    gbc.gridheight = 4;
    add(chartPane, gbc);

    gbc.insets = new Insets(5, 5, 5, 20);
    gbc.gridx = 1;
    gbc.weightx = 1;
    gbc.gridheight = 1;

    gbc.gridy = 0;
    gbc.weighty = 1;
    add(new JLabel("X :"), gbc);
    gbc.gridy = 2;
    add(new JLabel("Y :"), gbc);

    gbc.gridy = 1;
    gbc.weighty = 5;
    add(xSelPane, gbc);
    gbc.gridy = 3;
    add(ySelPane, gbc);
}

From source file:gui.InboxPanel.java

public void generate() {
    Message[] arrMsg = GmailAPI.Inbox.toArray(new Message[GmailAPI.Inbox.size()]);
    InboxList = new JList(arrMsg);
    InboxList.setCellRenderer(new DefaultListCellRenderer() { // Setting the DefaultListCellRenderer
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
                boolean cellHasFocus) {
            Message message = (Message) value; // Using value we are getting the object in JList
            Map<String, String> map = null;
            try {
                map = GmailAPI.getMessageDetails(message.getId());
            } catch (MessagingException ex) {
                Logger.getLogger(InboxPanel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(InboxPanel.class.getName()).log(Level.SEVERE, null, ex);
            }/*from   w ww  . j  av  a  2 s  .com*/
            String sub = map.get("subject");
            if (map.get("subject").length() > 22) {
                sub = map.get("subject").substring(0, 20) + "...";
            }
            setText(sub); // Setting the text
            //setIcon( shape.getImage() ); // Setting the Image Icon
            return this;
        }
    });
    InboxList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    InboxList.setLayoutOrientation(JList.VERTICAL);
    InboxList.setVisibleRowCount(-1);
    jScrollPane1.setViewportView(InboxList);

    InboxList.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent evt) {
            try {
                JList list = (JList) evt.getSource();
                int index = list.locationToIndex(evt.getPoint());
                String id = arrMsg[index].getId();
                curId = id;
                Map<String, String> map = GmailAPI.getMessageDetails(id);
                jTextField1.setText(map.get("from"));
                jTextField2.setText(map.get("subject"));
                dateTextField.setText(map.get("senddate"));
                BodyTextPane.setText(map.get("body"));
                //BodyTextPane.setContentType("text/html");
                //BodyTextArea.setCo
            } catch (IOException ex) {
                Logger.getLogger(InboxPanel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (MessagingException ex) {
                Logger.getLogger(InboxPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });
}

From source file:DropDemo.java

private JPanel createList() {
    DefaultListModel listModel = new DefaultListModel();

    for (int i = 0; i < 10; i++) {
        listModel.addElement("List Item " + i);
    }/* w  ww. ja va  2  s .c om*/

    list = new JList(listModel);
    list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    JScrollPane scrollPane = new JScrollPane(list);
    scrollPane.setPreferredSize(new Dimension(400, 100));

    list.setDragEnabled(true);
    list.setTransferHandler(new ListTransferHandler());

    dropCombo = new JComboBox(new String[] { "USE_SELECTION", "ON", "INSERT", "ON_OR_INSERT" });
    dropCombo.addActionListener(this);
    JPanel dropPanel = new JPanel();
    dropPanel.add(new JLabel("List Drop Mode:"));
    dropPanel.add(dropCombo);

    JPanel panel = new JPanel(new BorderLayout());
    panel.add(scrollPane, BorderLayout.CENTER);
    panel.add(dropPanel, BorderLayout.SOUTH);
    panel.setBorder(BorderFactory.createTitledBorder("List"));
    return panel;
}

From source file:be.fedict.eid.tsl.tool.SignSelectCertificatePanel.java

public SignSelectCertificatePanel(SignSelectPkcs11FinishablePanel pkcs11Panel,
        TrustServiceList trustServiceList) {
    this.pkcs11Panel = pkcs11Panel;
    this.trustServiceList = trustServiceList;

    this.component = new JPanel();
    BoxLayout boxLayout = new BoxLayout(this.component, BoxLayout.PAGE_AXIS);
    this.component.setLayout(boxLayout);

    this.component.add(new JLabel("Please select a certificate from the token: "));

    DefaultListModel listModel = new DefaultListModel();
    this.certificateList = new JList(listModel);
    this.component.add(new JScrollPane(this.certificateList));
}

From source file:dnd.ChooseDropActionDemo.java

public ChooseDropActionDemo() {
    super("ChooseDropActionDemo");

    for (int i = 15; i >= 0; i--) {
        from.add(0, "Source item " + i);
    }/*from  ww  w . j  a v  a2 s  .  co m*/

    for (int i = 2; i >= 0; i--) {
        copy.add(0, "Target item " + i);
        move.add(0, "Target item " + i);
    }

    JPanel p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
    dragFrom = new JList(from);
    dragFrom.setTransferHandler(new FromTransferHandler());
    dragFrom.setPrototypeCellValue("List Item WWWWWW");
    dragFrom.setDragEnabled(true);
    dragFrom.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    JLabel label = new JLabel("Drag from here:");
    label.setAlignmentX(0f);
    p.add(label);
    JScrollPane sp = new JScrollPane(dragFrom);
    sp.setAlignmentX(0f);
    p.add(sp);
    add(p, BorderLayout.WEST);

    JList moveTo = new JList(move);
    moveTo.setTransferHandler(new ToTransferHandler(TransferHandler.COPY));
    moveTo.setDropMode(DropMode.INSERT);
    JList copyTo = new JList(copy);
    copyTo.setTransferHandler(new ToTransferHandler(TransferHandler.MOVE));
    copyTo.setDropMode(DropMode.INSERT);

    p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
    label = new JLabel("Drop to COPY to here:");
    label.setAlignmentX(0f);
    p.add(label);
    sp = new JScrollPane(moveTo);
    sp.setAlignmentX(0f);
    p.add(sp);
    label = new JLabel("Drop to MOVE to here:");
    label.setAlignmentX(0f);
    p.add(label);
    sp = new JScrollPane(copyTo);
    sp.setAlignmentX(0f);
    p.add(sp);
    p.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 0));
    add(p, BorderLayout.CENTER);

    ((JPanel) getContentPane()).setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));

    getContentPane().setPreferredSize(new Dimension(320, 315));
}

From source file:com.intuit.tank.tools.debugger.SelectDialog.java

/**
 * @param arg0//w  ww.  jav  a  2  s.c om
 */
public SelectDialog(Frame f, List<SELECTION_TYPE> items, String itemType, boolean singleSelection) {
    super(f, true);
    setLayout(new BorderLayout());
    this.items = items;
    filterField = new JTextField();
    filterField.addKeyListener(new KeyHandler());
    list = new JList(items.toArray());
    list.setSelectionMode(singleSelection ? ListSelectionModel.SINGLE_SELECTION
            : ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    list.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
            okBT.setEnabled(list.getSelectedIndex() != -1);
        }
    });
    list.addMouseListener(new MouseAdapter() {

        /**
         * @{inheritDoc
         */
        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                select();
            }
        }

    });
    JPanel labelPanel = new JPanel(new GridLayout(singleSelection ? 1 : 2, 1, 0, 5));
    labelPanel.add(new JLabel("Select a " + itemType + "."));
    if (!singleSelection) {
        String key = System.getProperty("os.name").toLowerCase().indexOf("mac") != -1 ? "" : "control";
        System.out.println(key);
        labelPanel.add(new JLabel("Hold down the " + key + " key to select multiple " + itemType + "."));
    }
    add(labelPanel, BorderLayout.NORTH);
    JScrollPane sp = new JScrollPane(list);
    JPanel centerPanel = new JPanel(new BorderLayout());
    centerPanel.add(filterField, BorderLayout.NORTH);
    centerPanel.add(sp, BorderLayout.CENTER);
    add(centerPanel, BorderLayout.CENTER);
    add(createButtonPanel(), BorderLayout.SOUTH);
    setSize(new Dimension(400, 500));
    setBounds(new Rectangle(getSize()));
    setPreferredSize(getSize());
    WindowUtil.centerOnParent(this);
}

From source file:feedsplugin.FeedsSettingsTab.java

public JPanel createSettingsPanel() {
    final EnhancedPanelBuilder panelBuilder = new EnhancedPanelBuilder(
            FormFactory.RELATED_GAP_COLSPEC.encode() + ", fill:default:grow");
    final CellConstraints cc = new CellConstraints();

    mListModel = new DefaultListModel();
    for (String feed : mSettings.getFeeds()) {
        mListModel.addElement(feed);//  w w w .  j  av a  2  s .c  o  m
    }
    mFeeds = new JList(mListModel);
    mFeeds.setSelectedIndex(0);
    mFeeds.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    mFeeds.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(final ListSelectionEvent e) {
            listSelectionChanged();
        }
    });

    panelBuilder.addGrowingRow();
    panelBuilder.add(new JScrollPane(mFeeds),
            cc.xyw(2, panelBuilder.getRow(), panelBuilder.getColumnCount() - 1));

    mAdd = new JButton(mLocalizer.msg("add", "Add feed"));
    mAdd.addActionListener(new ActionListener() {

        public void actionPerformed(final ActionEvent e) {
            String genre = JOptionPane.showInputDialog(mLocalizer.msg("addMessage", "Add feed URL"), "");
            if (genre != null) {
                genre = genre.trim();
                if (genre.length() > 0) {
                    mListModel.addElement(genre);
                }
            }
        }
    });

    mRemove = new JButton(mLocalizer.msg("remove", "Remove feed"));
    mRemove.addActionListener(new ActionListener() {

        public void actionPerformed(final ActionEvent e) {
            final int index = mFeeds.getSelectedIndex();
            if (index >= 0) {
                mListModel.remove(index);
            }
        }
    });

    panelBuilder.addRow();
    ButtonBarBuilder2 buttonBar = new ButtonBarBuilder2();
    buttonBar.addButton(new JButton[] { mAdd, mRemove });
    panelBuilder.add(buttonBar.getPanel(), cc.xyw(2, panelBuilder.getRow(), panelBuilder.getColumnCount() - 1));

    mFeeds.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(final ListSelectionEvent e) {
            mRemove.setEnabled(mFeeds.getSelectedIndex() >= 0);
        }
    });

    panelBuilder.addParagraph(mLocalizer.msg("moreFeeds", "Get more feeds"));
    panelBuilder.addRow();
    JEditorPane help = UiUtilities.createHtmlHelpTextArea(mLocalizer.msg("help",
            "You can find more news feeds on the <a href=\"{0}\">plugin help page</a>. If you know more interesting feeds, feel free to add them on that page.",
            StringUtils.replace(PluginInfo.getHelpUrl(FeedsPlugin.getInstance().getId()), "&", "&amp;")));
    panelBuilder.add(help, cc.xyw(2, panelBuilder.getRow(), panelBuilder.getColumnCount() - 1));

    // force update of enabled states
    listSelectionChanged();

    return panelBuilder.getPanel();
}

From source file:components.ListDemo.java

public ListDemo() {
    super(new BorderLayout());

    listModel = new DefaultListModel();
    listModel.addElement("Jane Doe");
    listModel.addElement("John Smith");
    listModel.addElement("Kathy Green");

    //Create the list and put it in a scroll pane.
    list = new JList(listModel);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setSelectedIndex(0);//from  w  w w. j  ava2s .  c  o m
    list.addListSelectionListener(this);
    list.setVisibleRowCount(5);
    JScrollPane listScrollPane = new JScrollPane(list);

    JButton hireButton = new JButton(hireString);
    HireListener hireListener = new HireListener(hireButton);
    hireButton.setActionCommand(hireString);
    hireButton.addActionListener(hireListener);
    hireButton.setEnabled(false);

    fireButton = new JButton(fireString);
    fireButton.setActionCommand(fireString);
    fireButton.addActionListener(new FireListener());

    employeeName = new JTextField(10);
    employeeName.addActionListener(hireListener);
    employeeName.getDocument().addDocumentListener(hireListener);
    String name = listModel.getElementAt(list.getSelectedIndex()).toString();

    //Create a panel that uses BoxLayout.
    JPanel buttonPane = new JPanel();
    buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
    buttonPane.add(fireButton);
    buttonPane.add(Box.createHorizontalStrut(5));
    buttonPane.add(new JSeparator(SwingConstants.VERTICAL));
    buttonPane.add(Box.createHorizontalStrut(5));
    buttonPane.add(employeeName);
    buttonPane.add(hireButton);
    buttonPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

    add(listScrollPane, BorderLayout.CENTER);
    add(buttonPane, BorderLayout.PAGE_END);
}

From source file:JListDemo.java

JListDemo(String s) {
    super(s);/* w w  w . ja  va 2s.c  o m*/
    Container cp = getContentPane();
    cp.setLayout(new FlowLayout());
    ArrayList data = new ArrayList();
    data.add("Hi");
    data.add("Hello");
    data.add("Goodbye");
    data.add("Adieu");
    data.add("Adios");
    list = new JList(data.toArray());
    list.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent evt) {
            if (evt.getValueIsAdjusting())
                return;
            System.out.println("Selected from " + evt.getFirstIndex() + " to " + evt.getLastIndex());
        }
    });
    cp.add(list, BorderLayout.CENTER);
}