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:SortedListModel.java

private void initScreen() {
    setLayout(new GridLayout(0, 2));
    sourceListModel = new SortedListModel();
    sourceList = new JList(sourceListModel);

    addButton = new JButton(">>");
    addButton.addActionListener(new AddListener());
    removeButton = new JButton("<<");
    removeButton.addActionListener(new RemoveListener());

    destListModel = new SortedListModel();
    destList = new JList(destListModel);

    JPanel leftPanel = new JPanel(new BorderLayout());
    leftPanel.add(new JLabel("Available Elements:"), BorderLayout.NORTH);
    leftPanel.add(new JScrollPane(sourceList), BorderLayout.CENTER);
    leftPanel.add(addButton, BorderLayout.SOUTH);

    JPanel rightPanel = new JPanel(new BorderLayout());

    rightPanel.add(new JLabel("Selected Elements:"), BorderLayout.NORTH);
    rightPanel.add(new JScrollPane(destList), BorderLayout.CENTER);
    rightPanel.add(removeButton, BorderLayout.SOUTH);

    add(leftPanel);/*  w w w  . j ava2s  .com*/
    add(rightPanel);
}

From source file:com.k42b3.aletheia.response.html.Images.java

public Images() {
    super();/*from w  w  w.  ja va2 s .  c  o m*/

    executorService = Executors.newFixedThreadPool(6);

    // settings
    this.setTitle("Images");
    this.setLocation(100, 100);
    this.setPreferredSize(new Dimension(360, 600));
    this.setMinimumSize(this.getSize());
    this.setResizable(false);
    this.setLayout(new BorderLayout());

    // list
    model = new DefaultListModel<URL>();
    list = new JList<URL>(model);
    list.addListSelectionListener(new ListSelectionListener() {

        public void valueChanged(ListSelectionEvent e) {
            btnDownload.setEnabled(list.getSelectedIndex() != -1);
        }

    });
    list.setCellRenderer(new ImageCellRenderer());

    scp = new JScrollPane(list);
    scp.setBorder(new EmptyBorder(4, 4, 4, 4));

    this.add(scp, BorderLayout.CENTER);

    // buttons
    JPanel panelButtons = new JPanel();

    FlowLayout fl = new FlowLayout();
    fl.setAlignment(FlowLayout.LEFT);

    panelButtons.setLayout(fl);

    btnDownload = new JButton("Download");
    btnDownload.addActionListener(new DownloadHandler());
    btnDownload.setEnabled(false);
    JButton btnCancel = new JButton("Cancel");
    btnCancel.addActionListener(new CloseHandler());
    lblInfo = new JLabel("");

    panelButtons.add(btnDownload);
    panelButtons.add(btnCancel);
    panelButtons.add(lblInfo);

    this.add(panelButtons, BorderLayout.SOUTH);

    this.pack();
}

From source file:edu.ku.brc.af.tasks.subpane.formeditor.RowColDefPanel.java

protected void createUI(final int numInUse, @SuppressWarnings("hiding") final boolean isRow) {
    CellConstraints cc = new CellConstraints();
    PanelBuilder pb = new PanelBuilder(new FormLayout("max(125px;p):g,16px,p", "p,2px,p,2px,p:g")); //$NON-NLS-1$ //$NON-NLS-2$

    propsPanel = new DefItemPropPanel(numInUse, isRow);

    ActionListener addAL = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            addItem();//from w w  w . ja  v  a  2  s .  c  om
        }
    };

    ActionListener delAL = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            delItem();
        }
    };

    controlPanel = new EditDeleteAddPanel(null, delAL, addAL);
    controlPanel.getAddBtn().setEnabled(true);

    itemList = new JList(new DefaultListModel());
    itemList.setCellRenderer(new DefItemRenderer(IconManager.IconSize.Std16));
    JScrollPane sp = UIHelper.createScrollPane(itemList);
    pb.addSeparator((isRow ? "Row" : "Column") + " Items", cc.xy(1, 1)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    pb.add(sp, cc.xy(1, 3));
    pb.add(controlPanel, cc.xy(1, 5));

    pb.addSeparator("Properties", cc.xy(3, 1)); //$NON-NLS-1$
    pb.add(propsPanel, cc.xywh(3, 3, 1, 3));

    itemList.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
                itemSelected();
            }
        }
    });

    add(pb.getPanel(), BorderLayout.CENTER);
}

From source file:components.ListDialog.java

private ListDialog(Frame frame, Component locationComp, String labelText, String title, Object[] data,
        String initialValue, String longValue) {
    super(frame, title, true);

    //Create and initialize the buttons.
    JButton cancelButton = new JButton("Cancel");
    cancelButton.addActionListener(this);
    ////from www  . j  a va 2  s .com
    final JButton setButton = new JButton("Set");
    setButton.setActionCommand("Set");
    setButton.addActionListener(this);
    getRootPane().setDefaultButton(setButton);

    //main part of the dialog
    list = new JList(data) {
        //Subclass JList to workaround bug 4832765, which can cause the
        //scroll pane to not let the user easily scroll up to the beginning
        //of the list.  An alternative would be to set the unitIncrement
        //of the JScrollBar to a fixed value. You wouldn't get the nice
        //aligned scrolling, but it should work.
        public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
            int row;
            if (orientation == SwingConstants.VERTICAL && direction < 0
                    && (row = getFirstVisibleIndex()) != -1) {
                Rectangle r = getCellBounds(row, row);
                if ((r.y == visibleRect.y) && (row != 0)) {
                    Point loc = r.getLocation();
                    loc.y--;
                    int prevIndex = locationToIndex(loc);
                    Rectangle prevR = getCellBounds(prevIndex, prevIndex);

                    if (prevR == null || prevR.y >= r.y) {
                        return 0;
                    }
                    return prevR.height;
                }
            }
            return super.getScrollableUnitIncrement(visibleRect, orientation, direction);
        }
    };

    list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    if (longValue != null) {
        list.setPrototypeCellValue(longValue); //get extra space
    }
    list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
    list.setVisibleRowCount(-1);
    list.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                setButton.doClick(); //emulate button click
            }
        }
    });
    JScrollPane listScroller = new JScrollPane(list);
    listScroller.setPreferredSize(new Dimension(250, 80));
    listScroller.setAlignmentX(LEFT_ALIGNMENT);

    //Create a container so that we can add a title around
    //the scroll pane.  Can't add a title directly to the
    //scroll pane because its background would be white.
    //Lay out the label and scroll pane from top to bottom.
    JPanel listPane = new JPanel();
    listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
    JLabel label = new JLabel(labelText);
    label.setLabelFor(list);
    listPane.add(label);
    listPane.add(Box.createRigidArea(new Dimension(0, 5)));
    listPane.add(listScroller);
    listPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

    //Lay out the buttons from left to right.
    JPanel buttonPane = new JPanel();
    buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
    buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
    buttonPane.add(Box.createHorizontalGlue());
    buttonPane.add(cancelButton);
    buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
    buttonPane.add(setButton);

    //Put everything together, using the content pane's BorderLayout.
    Container contentPane = getContentPane();
    contentPane.add(listPane, BorderLayout.CENTER);
    contentPane.add(buttonPane, BorderLayout.PAGE_END);

    //Initialize values.
    setValue(initialValue);
    pack();
    setLocationRelativeTo(locationComp);
}

From source file:FocusEventDemo.java

public void addComponentsToPane(final Container pane) {
    GridBagLayout gridbag = new GridBagLayout();
    pane.setLayout(gridbag);//  www .j  av a  2s  .  c om

    GridBagConstraints c = new GridBagConstraints();

    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 1.0; // Make column as wide as possible.
    JTextField textField = new JTextField("A TextField");
    textField.setMargin(new Insets(0, 2, 0, 2));
    textField.addFocusListener(this);
    gridbag.setConstraints(textField, c);
    add(textField);

    c.weightx = 0.1; // Widen every other column a bit, when possible.
    c.fill = GridBagConstraints.NONE;
    JLabel label = new JLabel("A Label");
    label.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
    label.addFocusListener(this);
    gridbag.setConstraints(label, c);
    add(label);

    String comboPrefix = "ComboBox Item #";
    final int numItems = 15;
    Vector<String> vector = new Vector<String>(numItems);
    for (int i = 0; i < numItems; i++) {
        vector.addElement(comboPrefix + i);
    }
    JComboBox comboBox = new JComboBox(vector);
    comboBox.addFocusListener(this);
    gridbag.setConstraints(comboBox, c);
    add(comboBox);

    c.gridwidth = GridBagConstraints.REMAINDER;
    JButton button = new JButton("A Button");
    button.addFocusListener(this);
    gridbag.setConstraints(button, c);
    add(button);

    c.weightx = 0.0;
    c.weighty = 0.1;
    c.fill = GridBagConstraints.BOTH;
    String listPrefix = "List Item #";
    Vector<String> listVector = new Vector<String>(numItems);
    for (int i = 0; i < numItems; i++) {
        listVector.addElement(listPrefix + i);
    }
    JList list = new JList(listVector);
    list.setSelectedIndex(1); // It's easier to see the focus change
    // if an item is selected.
    list.addFocusListener(this);
    JScrollPane listScrollPane = new JScrollPane(list);

    gridbag.setConstraints(listScrollPane, c);
    add(listScrollPane);

    c.weighty = 1.0; // Make this row as tall as possible.
    c.gridheight = GridBagConstraints.REMAINDER;
    // Set up the area that reports focus-gained and focus-lost events.
    display = new JTextArea();
    display.setEditable(false);
    // The setRequestFocusEnabled method prevents a
    // component from being clickable, but it can still
    // get the focus through the keyboard - this ensures
    // user accessibility.
    display.setRequestFocusEnabled(false);
    display.addFocusListener(this);
    JScrollPane displayScrollPane = new JScrollPane(display);

    gridbag.setConstraints(displayScrollPane, c);
    add(displayScrollPane);
    setPreferredSize(new Dimension(450, 450));
    ((JPanel) pane).setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
}

From source file:de.pavloff.spark4knime.jsnippet.ui.JarListPanel.java

/** Inits GUI. */
public JarListPanel() {
    super(new BorderLayout());
    m_addJarList = new JList<String>(new DefaultListModel<String>()) {
        /** {@inheritDoc} */
        @Override/*from  www  .  j  a va2s. c o m*/
        protected void processComponentKeyEvent(final KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_A && e.isControlDown()) {
                int end = getModel().getSize() - 1;
                getSelectionModel().setSelectionInterval(0, end);
            } else if (e.getKeyCode() == KeyEvent.VK_DELETE) {
                onJarRemove();
            }
        }
    };
    m_addJarList.setCellRenderer(new ConvenientComboBoxRenderer());
    add(new JScrollPane(m_addJarList), BorderLayout.CENTER);
    JPanel southP = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 5));
    m_addJarFilesButton = new JButton("Add File(s)...");
    m_addJarFilesButton.addActionListener(new ActionListener() {
        /** {@inheritDoc} */
        @Override
        public void actionPerformed(final ActionEvent e) {
            onJarFileAdd();
        }
    });
    m_addJarURLsButton = new JButton("Add KNIME URL...");
    m_addJarURLsButton.setToolTipText("Add 'knime' URLs that resolve to local paths");
    m_addJarURLsButton.addActionListener(new ActionListener() {
        /** {@inheritDoc} */
        @Override
        public void actionPerformed(final ActionEvent e) {
            onJarURLAdd();
        }
    });
    m_removeButton = new JButton("Remove");
    m_removeButton.addActionListener(new ActionListener() {
        /** {@inheritDoc} */
        @Override
        public void actionPerformed(final ActionEvent e) {
            onJarRemove();
        }
    });
    m_addJarList.addListSelectionListener(new ListSelectionListener() {
        /** {@inheritDoc} */
        @Override
        public void valueChanged(final ListSelectionEvent e) {
            m_removeButton.setEnabled(!m_addJarList.isSelectionEmpty());
        }
    });
    m_removeButton.setEnabled(!m_addJarList.isSelectionEmpty());
    southP.add(m_addJarFilesButton);
    southP.add(m_addJarURLsButton);
    southP.add(m_removeButton);
    add(southP, BorderLayout.SOUTH);

    JPanel northP = new JPanel(new FlowLayout());
    JLabel label = new JLabel("<html><body>Specify additional jar files "
            + "that are necessary for the snippet to run</body></html>");
    northP.add(label);
    add(northP, BorderLayout.NORTH);
}

From source file:FocusEventDemo.java

public FocusEventDemo() {
    super(new GridBagLayout());
    GridBagLayout gridbag = (GridBagLayout) getLayout();
    GridBagConstraints c = new GridBagConstraints();

    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 1.0; //Make column as wide as possible.
    JTextField textField = new JTextField("A TextField");
    textField.setMargin(new Insets(0, 2, 0, 2));
    textField.addFocusListener(this);
    gridbag.setConstraints(textField, c);
    add(textField);//  ww  w. ja  v a  2  s .co  m

    c.weightx = 0.1; //Widen every other column a bit, when possible.
    c.fill = GridBagConstraints.NONE;
    JLabel label = new JLabel("A Label");
    label.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
    label.addFocusListener(this);
    gridbag.setConstraints(label, c);
    add(label);

    String comboPrefix = "ComboBox Item #";
    final int numItems = 15;
    Vector vector = new Vector(numItems);
    for (int i = 0; i < numItems; i++) {
        vector.addElement(comboPrefix + i);
    }
    JComboBox comboBox = new JComboBox(vector);
    comboBox.addFocusListener(this);
    gridbag.setConstraints(comboBox, c);
    add(comboBox);

    c.gridwidth = GridBagConstraints.REMAINDER;
    JButton button = new JButton("A Button");
    button.addFocusListener(this);
    gridbag.setConstraints(button, c);
    add(button);

    c.weightx = 0.0;
    c.weighty = 0.1;
    c.fill = GridBagConstraints.BOTH;
    String listPrefix = "List Item #";
    Vector listVector = new Vector(numItems);
    for (int i = 0; i < numItems; i++) {
        listVector.addElement(listPrefix + i);
    }
    JList list = new JList(listVector);
    list.setSelectedIndex(1); //It's easier to see the focus change
    //if an item is selected.
    list.addFocusListener(this);
    JScrollPane listScrollPane = new JScrollPane(list);
    //We want to prevent the list's scroll bars
    //from getting the focus - even with the keyboard.
    //Note that in general we prefer setRequestFocusable
    //over setFocusable for reasons of accessibility,
    //but this is to work around bug #4866958.
    listScrollPane.getVerticalScrollBar().setFocusable(false);
    listScrollPane.getHorizontalScrollBar().setFocusable(false);
    gridbag.setConstraints(listScrollPane, c);
    add(listScrollPane);

    c.weighty = 1.0; //Make this row as tall as possible.
    c.gridheight = GridBagConstraints.REMAINDER;
    //Set up the area that reports focus-gained and focus-lost events.
    display = new JTextArea();
    display.setEditable(false);
    //The method setRequestFocusEnabled prevents a
    //component from being clickable, but it can still
    //get the focus through the keyboard - this ensures
    //user accessibility.
    display.setRequestFocusEnabled(false);
    display.addFocusListener(this);
    JScrollPane displayScrollPane = new JScrollPane(display);

    //Work around for bug #4866958.
    displayScrollPane.getHorizontalScrollBar().setFocusable(false);
    displayScrollPane.getVerticalScrollBar().setFocusable(false);
    gridbag.setConstraints(displayScrollPane, c);
    add(displayScrollPane);

    setPreferredSize(new Dimension(450, 450));
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
}

From source file:SwingDnDTest.java

public static JList list() {
    String[] words = { "quick", "brown", "hungry", "wild", "silent", "huge", "private", "abstract", "static",
            "final" };

    DefaultListModel model = new DefaultListModel();
    for (String word : words)
        model.addElement(word);//from   www.  ja  v  a 2s .c o  m
    return new JList(model);
}

From source file:com.orange.atk.graphAnalyser.RealtimeGraph.java

private void initComponents() {

    jListGraph = new JList(listModelGraph);
    jPanelroot = new JPanel();
    jScrollPaneListGraph = new JScrollPane();
    jListPerfGraph = new JList(listModelGraph);
    jComboBoxLeft = new JComboBox(comboModelLeft);
    jComboBoxRight = new JComboBox(comboModelRight);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);

    jScrollPaneListGraph.setViewportView(jListPerfGraph);

    jComboBoxLeft.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jComboBoxLeftActionPerformed(evt);
        }//  w w w.j  av a2s. c om
    });

    jComboBoxRight.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jComboBoxRightActionPerformed(evt);
        }
    });

    jPanelroot.setLayout(new BorderLayout());
    jPanelroot.add(chartPanel, BorderLayout.CENTER);

    toolPane = new JPanel();
    toolPane.setLayout(new FlowLayout());

    toolPane.add(jComboBoxLeft);

    Box graphlistbox = Box.createVerticalBox();
    graphlistbox.add(new JLabel("List of Graph"));
    graphlistbox.add(jScrollPaneListGraph);
    toolPane.add(graphlistbox);

    toolPane.add(jComboBoxRight);

    jPanelroot.add(toolPane, BorderLayout.NORTH);

    setContentPane(jPanelroot);

    //a small size for small screen
    //  setMaximumSize(new Dimension(600,500));
    pack();

}

From source file:Main.java

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

    // Create and populate the list model.
    listModel = new DefaultListModel();
    listModel.addElement("Whistler, Canada");
    listModel.addElement("Jackson Hole, Wyoming");
    listModel.addElement("Squaw Valley, California");
    listModel.addElement("Telluride, Colorado");
    listModel.addElement("Taos, New Mexico");
    listModel.addElement("Snowbird, Utah");
    listModel.addElement("Chamonix, France");
    listModel.addElement("Banff, Canada");
    listModel.addElement("Arapahoe Basin, Colorado");
    listModel.addElement("Kirkwood, California");
    listModel.addElement("Sun Valley, Idaho");
    listModel.addListDataListener(new MyListDataListener());

    // Create the list and put it in a scroll pane.
    list = new JList(listModel);
    list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    list.setSelectedIndex(0);//w  w  w .j av a 2  s .co  m
    list.addListSelectionListener(this);
    JScrollPane listScrollPane = new JScrollPane(list);

    // Create the list-modifying buttons.
    addButton = new JButton(addString);
    addButton.setActionCommand(addString);
    addButton.addActionListener(new AddButtonListener());

    deleteButton = new JButton(deleteString);
    deleteButton.setActionCommand(deleteString);
    deleteButton.addActionListener(new DeleteButtonListener());

    ImageIcon icon = createImageIcon("Up16");
    if (icon != null) {
        upButton = new JButton(icon);
        upButton.setMargin(new Insets(0, 0, 0, 0));
    } else {
        upButton = new JButton("Move up");
    }
    upButton.setToolTipText("Move the currently selected list item higher.");
    upButton.setActionCommand(upString);
    upButton.addActionListener(new UpDownListener());

    icon = createImageIcon("Down16");
    if (icon != null) {
        downButton = new JButton(icon);
        downButton.setMargin(new Insets(0, 0, 0, 0));
    } else {
        downButton = new JButton("Move down");
    }
    downButton.setToolTipText("Move the currently selected list item lower.");
    downButton.setActionCommand(downString);
    downButton.addActionListener(new UpDownListener());

    JPanel upDownPanel = new JPanel(new GridLayout(2, 1));
    upDownPanel.add(upButton);
    upDownPanel.add(downButton);

    // Create the text field for entering new names.
    nameField = new JTextField(15);
    nameField.addActionListener(new AddButtonListener());
    String name = listModel.getElementAt(list.getSelectedIndex()).toString();
    nameField.setText(name);

    // Create a control panel, using the default FlowLayout.
    JPanel buttonPane = new JPanel();
    buttonPane.add(nameField);
    buttonPane.add(addButton);
    buttonPane.add(deleteButton);
    buttonPane.add(upDownPanel);

    // Create the log for reporting list data events.
    log = new JTextArea(10, 20);
    JScrollPane logScrollPane = new JScrollPane(log);

    // Create a split pane for the log and the list.
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, listScrollPane, logScrollPane);
    splitPane.setResizeWeight(0.5);

    // Put everything together.
    add(buttonPane, BorderLayout.PAGE_START);
    add(splitPane, BorderLayout.CENTER);
}