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

public static void main(String[] argv) throws Exception {

    JTable table = new JTable();

    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTable table = new JTable();

    if (table.getCellSelectionEnabled()) {
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        int rowIndex = table.getSelectedRow();
        int colIndex = table.getSelectedColumn();

    }/*from w  w  w. ja  v a 2 s.c  om*/
}

From source file:Main.java

public static void main(String[] args) {
    JPanel panel = new JPanel();
    panel.setPreferredSize(new Dimension(800, 600));

    String test[] = { "alpha", "bravo", "charlie", "delta", "echo", "omega", "zeta" };
    JList<String> list = new JList<>(test);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setLayoutOrientation(JList.VERTICAL);
    list.setVisibleRowCount(5);//from w w  w  .j  ava  2  s.c o  m
    list.setBounds(50, 150, 75, 90);

    JScrollPane jScrollPane1 = new JScrollPane();
    jScrollPane1.setViewportView(list);

    panel.add(jScrollPane1);

    JFrame f = new JFrame();
    f.add(panel);
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setResizable(false);
    f.setFocusable(true);
}

From source file:Main.java

public static void main(String args[]) {
    String ITEMS[] = { "Black", "Blue", "Green", "Orange", "Purple", "Red", "White" };
    JList jList = new JList(ITEMS);
    jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    JScrollPane scroll = new JScrollPane(jList);
    scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

    JCheckBox chkEnable = new JCheckBox("Enable", true);
    chkEnable.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            jList.setEnabled(chkEnable.isSelected());
        }/*  w  ww  . j ava  2 s  .com*/
    });

    JFrame f = new JFrame("Colors");
    Container contentPane = f.getContentPane();
    contentPane.setLayout(new BorderLayout());
    contentPane.add(scroll, BorderLayout.CENTER);
    contentPane.add(chkEnable, BorderLayout.NORTH);

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(180, 220);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

From source file:Main.java

public static void main(final String args[]) {
    String labels[] = { "A", "B", "C", "D", "E" };
    JFrame frame = new JFrame("Sizing Samples");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JList jlist1 = new JList(labels);
    jlist1.setVisibleRowCount(4);// w  w  w.java  2s . co m
    JScrollPane scrollPane1 = new JScrollPane(jlist1);
    frame.add(scrollPane1, BorderLayout.NORTH);

    jlist1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

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

From source file:Main.java

public static void main(String args[]) {

    final Object rowData[][] = { { "1", "one", "I" }, { "2", "two", "II" }, { "3", "three", "III" } };
    final String columnNames[] = { "#", "English", "Roman" };

    final JTable table = new JTable(rowData, columnNames);
    JScrollPane scrollPane = new JScrollPane(table);

    JFrame frame = new JFrame("Resizing Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(scrollPane, BorderLayout.CENTER);

    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    frame.setSize(300, 150);//from   w  ww .  j a  va2s  . c om
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JTable table;

    String[] columnTitles = { "A", "B", "C", "D" };
    Object[][] rowData = { { "11", "12", "13", "14" }, { "21", "22", "23", "24" }, { "31", "32", "33", "34" },
            { "41", "42", "44", "44" } };

    table = new JTable(rowData, columnTitles);

    table.setCellSelectionEnabled(true);
    ListSelectionModel cellSelectionModel = table.getSelectionModel();
    cellSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    cellSelectionModel.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
            String selectedData = null;

            int[] selectedRow = table.getSelectedRows();
            int[] selectedColumns = table.getSelectedColumns();

            for (int i = 0; i < selectedRow.length; i++) {
                for (int j = 0; j < selectedColumns.length; j++) {
                    selectedData = (String) table.getValueAt(selectedRow[i], selectedColumns[j]);
                }/*from   w  w w .j  a  v a  2  s . c  om*/
            }
            System.out.println("Selected: " + selectedData);
        }

    });

    frame.add(new JScrollPane(table));

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

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JList list = new JList(new CheckListItem[] { new CheckListItem("apple"), new CheckListItem("orange"),
            new CheckListItem("mango"), new CheckListItem("mango"), new CheckListItem("mango"),
            new CheckListItem("mango"), new CheckListItem("mango"), new CheckListItem("mango"),
            new CheckListItem("mango"), new CheckListItem("mango"), new CheckListItem("mango"),

            new CheckListItem("paw paw"), new CheckListItem("banana") });
    list.setCellRenderer(new CheckListRenderer());
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.addMouseListener(new MouseAdapter() {
        @Override/*  w  w w.j a v a2 s  . c om*/
        public void mouseClicked(MouseEvent event) {
            JList list = (JList) event.getSource();
            int index = list.locationToIndex(event.getPoint());// Get index of item
                                                               // clicked
            CheckListItem item = (CheckListItem) list.getModel().getElementAt(index);
            item.setSelected(!item.isSelected()); // Toggle selected state
            list.repaint(list.getCellBounds(index, index));// Repaint cell
        }
    });
    frame.getContentPane().add(new JScrollPane(list));
    frame.pack();
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(final String args[]) {
    final Object rows[][] = { { "one", "1" }, { "two", "2" }, { "three", "3" } };
    final String headers[] = { "English", "Digit" };

    TableModel fixedColumnModel = new AbstractTableModel() {
        public int getColumnCount() {
            return 2;
        }/*from w w  w.j a  va2 s  . c  om*/

        public String getColumnName(int column) {
            return headers[column];
        }

        public int getRowCount() {
            return 3;
        }

        public Object getValueAt(int row, int column) {
            return rows[row][column];
        }
    };

    JFrame frame = new JFrame("Scrollless Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTable table = new JTable(fixedColumnModel);

    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    frame.add(new JScrollPane(table), BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JDialog dialog;/*w  w  w.  j  a  v a  2  s.  co m*/
    JList jlist;
    ActionListener otherListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("current");
        }
    };
    JButton okButton = new JButton("OK");
    okButton.addActionListener(e -> close(true));
    JButton cancelButton = new JButton("Cancel");
    cancelButton.addActionListener(e -> close(false));
    jlist = new JList(new String[] { "A", "B", "C", "D", "E", "F", "G" });
    jlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    jlist.setVisibleRowCount(5);
    JScrollPane scroll = new JScrollPane(jlist);
    JPanel buttonsPanel = new JPanel(new FlowLayout());
    buttonsPanel.add(okButton);
    buttonsPanel.add(cancelButton);
    JPanel content = new JPanel(new BorderLayout());
    content.add(scroll, BorderLayout.CENTER);
    content.add(buttonsPanel, BorderLayout.SOUTH);
    dialog = new JDialog((Frame) null, true);
    dialog.setContentPane(content);
    dialog.pack();
    dialog.getRootPane().registerKeyboardAction(otherListener, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
            JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    dialog.getRootPane().registerKeyboardAction(otherListener, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
            JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    dialog.getRootPane().getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "doSomething");
    dialog.getRootPane().getActionMap().put("doSomething", new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });
    dialog.setVisible(true);
}