Example usage for javax.swing ListSelectionModel SINGLE_SELECTION

List of usage examples for javax.swing ListSelectionModel SINGLE_SELECTION

Introduction

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

Prototype

int SINGLE_SELECTION

To view the source code for javax.swing ListSelectionModel SINGLE_SELECTION.

Click Source Link

Document

A value for the selectionMode property: select one list index at a time.

Usage

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);//  w w w.  j a  va 2s.  co  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: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);/*from   w w  w .  java  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:net.sf.housekeeper.swing.CategoriesView.java

protected JComponent createControl() {
    tree = new JTree();

    tree.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    final BeanTreeCellRenderer renderer = new BeanTreeCellRenderer(Category.class, "name");
    renderer.setOpenIcon(null);//from  w  w w.  j  a va 2  s .co  m
    renderer.setClosedIcon(null);
    renderer.setLeafIcon(null);
    tree.setCellRenderer(renderer);
    tree.setRootVisible(true);

    tree.addTreeSelectionListener(new TreeSelectionListener() {

        public void valueChanged(TreeSelectionEvent e) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

            if (node == null)
                return;

            Object nodeInfo = node.getUserObject();
            Category cat = (Category) nodeInfo;
            publishSelectionEvent(cat);
        }
    });

    refresh();
    return tree;
}

From source file:com.sec.ose.osi.ui.frm.main.identification.codematch.table.JTableInfoForCMMultipleFile.java

public JTableInfoForCMMultipleFile() {

    this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    this.setModel(MULTIPLE_FILE_TABLE_MODEL);
    this.setDefaultRenderer(this.getColumnClass(0), renderer);

    initSorter();/*from w ww.jav a 2s . co m*/
}

From source file:Main.java

public ListRenderingFrame() {
    setTitle("ListRendering");
    setSize(400, 300);//from  w  w w .ja v  a2s  .  c o  m
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

    Vector fonts = new Vector();
    fonts.add(new Font("Serif", Font.PLAIN, 8));
    fonts.add(new Font("SansSerif", Font.BOLD, 12));
    fonts.add(new Font("Monospaced", Font.PLAIN, 16));
    fonts.add(new Font("Dialog", Font.ITALIC, 12));
    fonts.add(new Font("DialogInput", Font.PLAIN, 12));
    JList fontList = new JList(fonts);
    fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    fontList.setCellRenderer(new FontCellRenderer());
    JScrollPane scrollPane = new JScrollPane(fontList);

    JPanel p = new JPanel();
    p.add(scrollPane);
    fontList.addListSelectionListener(this);

    getContentPane().add(p, "Center");

    getContentPane().add(label, "South");
}

From source file:dnd.ChooseDropActionDemo.java

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

    for (int i = 15; i >= 0; i--) {
        from.add(0, "Source item " + i);
    }/*  w w  w. ja  va2 s.c  o 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:components.TableFilterDemo.java

public TableFilterDemo() {
    super();//from  w w  w  .  ja v  a  2 s.co  m
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

    //Create a table with a sorter.
    MyTableModel model = new MyTableModel();
    sorter = new TableRowSorter<MyTableModel>(model);
    table = new JTable(model);
    table.setRowSorter(sorter);
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);

    //For the purposes of this example, better to have a single
    //selection.
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    //When selection changes, provide user with row numbers for
    //both view and model.
    table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent event) {
            int viewRow = table.getSelectedRow();
            if (viewRow < 0) {
                //Selection got filtered away.
                statusText.setText("");
            } else {
                int modelRow = table.convertRowIndexToModel(viewRow);
                statusText.setText(String.format("Selected Row in view: %d. " + "Selected Row in model: %d.",
                        viewRow, modelRow));
            }
        }
    });

    //Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

    //Add the scroll pane to this panel.
    add(scrollPane);

    //Create a separate form for filterText and statusText
    JPanel form = new JPanel(new SpringLayout());
    JLabel l1 = new JLabel("Filter Text:", SwingConstants.TRAILING);
    form.add(l1);
    filterText = new JTextField();
    //Whenever filterText changes, invoke newFilter.
    filterText.getDocument().addDocumentListener(new DocumentListener() {
        public void changedUpdate(DocumentEvent e) {
            newFilter();
        }

        public void insertUpdate(DocumentEvent e) {
            newFilter();
        }

        public void removeUpdate(DocumentEvent e) {
            newFilter();
        }
    });
    l1.setLabelFor(filterText);
    form.add(filterText);
    JLabel l2 = new JLabel("Status:", SwingConstants.TRAILING);
    form.add(l2);
    statusText = new JTextField();
    l2.setLabelFor(statusText);
    form.add(statusText);
    SpringUtilities.makeCompactGrid(form, 2, 2, 6, 6, 6, 6);
    add(form);
}

From source file:com.emr.schemas.DestinationTables.java

/**
 * Creates a new DestinationTables form.
 * @param sourceQuery {@link String} SQL Query for getting the source data.
 * @param selected_columns {@link List} List containing the selected source columns
 * @param sourceTables {@link List} List containing the source tables
 * @param relations {@link String} Relationship between the source tables
 * @param emrConn {@link Connection} KenyaEMR Database Connection object
 *//*from w  ww. j  a  va 2 s.  c  o m*/
public DestinationTables(String sourceQuery, List selected_columns, List sourceTables, String relations,
        Connection emrConn, TableRelationsForm parent) {
    fileManager = null;
    dbManager = null;
    mpiConn = null;

    this.parent = parent;
    this.sourceQuery = sourceQuery;
    this.selected_columns = selected_columns;
    this.sourceTables = sourceTables;
    this.relations = relations;
    this.emrConn = emrConn;

    listModel = new DefaultListModel<String>();
    initComponents();
    this.setClosable(true);

    SourceTablesListener listSelectionListener = new SourceTablesListener(new JTextArea(), listOfTables);

    destinationTablesList.setCellRenderer(new CheckboxListCellRenderer());
    destinationTablesList.addListSelectionListener(listSelectionListener);
    destinationTablesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    //Create KenyaEMR DB connection
    fileManager = new FileManager();
    String[] settings = fileManager.getConnectionSettings("mpi_database.properties", "mpi");
    if (settings == null) {
        //Connection settings not found
        //Open MPIConnectionForm
        JOptionPane.showMessageDialog(null,
                "Database Settings not found. Please set the connection settings for the database first.",
                "MPI Database settings", JOptionPane.ERROR_MESSAGE);

    } else {
        if (settings.length < 1) {
            //Open MPIConnectionForm
            JOptionPane.showMessageDialog(null,
                    "Database Settings not found. Please set the connection settings for the database first.",
                    "MPI Database settings", JOptionPane.ERROR_MESSAGE);

        } else {
            //Connection settings are ok
            //We establish a connection
            dbManager = new DatabaseManager(settings[0], settings[1], settings[3], settings[4], settings[5]);
            mpiConn = dbManager.getConnection();
            if (mpiConn == null) {
                JOptionPane.showMessageDialog(null, "Test Connection Failed", "Connection Test",
                        JOptionPane.ERROR_MESSAGE);
            } else {
                //get emr schema
                getDatabaseMetaData();
            }
        }
    }
}

From source file:com.emental.mindraider.ui.outline.OutlineSorterJPanel.java

public OutlineSorterJPanel() {
    setLayout(new BorderLayout());

    // table with archived concepts (title)
    // let table model to load discarded concepts itself
    tableModel = new SorterTableModel();
    table = new JTable(tableModel);
    table.getSelectionModel().addListSelectionListener(new SorterListSelectionListener(table, tableModel));
    table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    table.setFillsViewportHeight(true);/*from   www.  j  a va 2 s .  c  o  m*/
    table.setShowHorizontalLines(false);
    table.setShowVerticalLines(false);
    table.getColumnModel().getColumn(COLUMN_NAME).setPreferredWidth(150);
    table.getColumnModel().getColumn(COLUMN_ANNOTATION).setPreferredWidth(220);
    table.getColumnModel().getColumn(COLUMN_CREATED).setPreferredWidth(60);
    table.getColumnModel().getColumn(COLUMN_MODIFIED).setPreferredWidth(60);
    table.getColumnModel().getColumn(COLUMN_REVISION).setPreferredWidth(35);

    TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel());
    table.setRowSorter(sorter);
    sorter.setComparator(COLUMN_REVISION, new Comparator<Long>() {
        @Override
        public int compare(Long o1, Long o2) {
            return o2.intValue() - o1.intValue();
        }
    });
    final Comparator<String> timestampComparator = new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            if (OutlineTreeInstance.getCreatedTimestampFromHtml(o2) > OutlineTreeInstance
                    .getCreatedTimestampFromHtml(o1)) {
                return 1;
            } else {
                if (OutlineTreeInstance.getCreatedTimestampFromHtml(o2) == OutlineTreeInstance
                        .getCreatedTimestampFromHtml(o1)) {
                    return 0;
                } else {
                    return -1;
                }
            }
        }
    };
    sorter.setComparator(COLUMN_MODIFIED, timestampComparator);
    sorter.setComparator(COLUMN_CREATED, timestampComparator);

    JScrollPane scroll = new JScrollPane(table);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    add(scroll, BorderLayout.CENTER);
}

From source file:com.microsoft.alm.plugin.idea.common.ui.common.ServerContextTableModel.java

public ServerContextTableModel(Column[] columns) {
    assert columns != null;
    this.columns = columns.clone();
    selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
}