List of usage examples for javax.swing ListSelectionModel addListSelectionListener
void addListSelectionListener(ListSelectionListener x);
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 o m*/ } System.out.println("Selected: " + selectedData); } }); frame.add(new JScrollPane(table)); frame.setSize(300, 200); frame.setVisible(true); }
From source file:Main.java
public Main() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); String items[] = { "A", "B", "C", "D" }; JList list = new JList(items); ListSelectionModel selModel = list.getSelectionModel(); selModel.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { if (!e.getValueIsAdjusting()) { System.out.println("selection changed: " + e.getFirstIndex()); }//from ww w. j a v a 2 s .c o m } }); getContentPane().add(list); pack(); setVisible(true); }
From source file:Main.java
Main() { JFrame jfrm = new JFrame("JTable Event Demo"); jfrm.setSize(400, 200);/*from w w w. j a v a 2 s .co m*/ jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jtabOrders.setPreferredScrollableViewportSize(new Dimension(420, 62)); ListSelectionModel rowSelMod = jtabOrders.getSelectionModel(); ListSelectionModel colSelMod = jtabOrders.getColumnModel().getSelectionModel(); rowSelMod.addListSelectionListener(this); colSelMod.addListSelectionListener(this); tm = jtabOrders.getModel(); tm.addTableModelListener(new TableModelListener() { public void tableChanged(TableModelEvent tme) { if (tme.getType() == TableModelEvent.UPDATE) { System.out.println("Cell " + tme.getFirstRow() + ", " + tme.getColumn() + " changed." + " The new value: " + tm.getValueAt(tme.getFirstRow(), tme.getColumn())); } } }); jfrm.add(new JScrollPane(jtabOrders)); jfrm.setVisible(true); }
From source file:Main.java
public Main() { super(new GridLayout(1, 0)); final String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years", "Vegetarian" }; final Object[][] data = { { "Mary", "Campione", "Snowboarding", new Integer(5), new Boolean(false) }, { "Alison", "Huml", "Rowing", new Integer(3), new Boolean(true) }, { "Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false) }, { "Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true) }, { "Philip", "Milne", "Pool", new Integer(10), new Boolean(false) } }; final JTable table = new JTable(data, columnNames); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); table.setFillsViewportHeight(true);//w ww. java2s. c o m table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); if (ALLOW_ROW_SELECTION) { // true by default ListSelectionModel rowSM = table.getSelectionModel(); rowSM.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { // Ignore extra messages. if (e.getValueIsAdjusting()) return; ListSelectionModel lsm = (ListSelectionModel) e.getSource(); if (lsm.isSelectionEmpty()) { System.out.println("No rows are selected."); } else { int selectedRow = lsm.getMinSelectionIndex(); System.out.println("Row " + selectedRow + " is now selected."); } } }); } else { table.setRowSelectionAllowed(false); } if (ALLOW_COLUMN_SELECTION) { // false by default if (ALLOW_ROW_SELECTION) { // We allow both row and column selection, which // implies that we *really* want to allow individual // cell selection. table.setCellSelectionEnabled(true); } table.setColumnSelectionAllowed(true); ListSelectionModel colSM = table.getColumnModel().getSelectionModel(); colSM.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { // Ignore extra messages. if (e.getValueIsAdjusting()) return; ListSelectionModel lsm = (ListSelectionModel) e.getSource(); if (lsm.isSelectionEmpty()) { System.out.println("No columns are selected."); } else { int selectedCol = lsm.getMinSelectionIndex(); System.out.println("Column " + selectedCol + " is now selected."); } } }); } if (DEBUG) { table.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { printDebugData(table); } }); } // Create the scroll pane and add the table to it. JScrollPane scrollPane = new JScrollPane(table); // Add the scroll pane to this panel. add(scrollPane); }
From source file:SimpleTableSelectionDemo.java
public SimpleTableSelectionDemo() { super(new GridLayout(1, 0)); final String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years", "Vegetarian" }; final Object[][] data = { { "Mary", "Campione", "Snowboarding", new Integer(5), new Boolean(false) }, { "Alison", "Huml", "Rowing", new Integer(3), new Boolean(true) }, { "Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false) }, { "Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true) }, { "Philip", "Milne", "Pool", new Integer(10), new Boolean(false) } }; final JTable table = new JTable(data, columnNames); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); table.setFillsViewportHeight(true);/* www . j a v a 2 s .c o m*/ table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); if (ALLOW_ROW_SELECTION) { // true by default ListSelectionModel rowSM = table.getSelectionModel(); rowSM.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { // Ignore extra messages. if (e.getValueIsAdjusting()) return; ListSelectionModel lsm = (ListSelectionModel) e.getSource(); if (lsm.isSelectionEmpty()) { System.out.println("No rows are selected."); } else { int selectedRow = lsm.getMinSelectionIndex(); System.out.println("Row " + selectedRow + " is now selected."); } } }); } else { table.setRowSelectionAllowed(false); } if (ALLOW_COLUMN_SELECTION) { // false by default if (ALLOW_ROW_SELECTION) { // We allow both row and column selection, which // implies that we *really* want to allow individual // cell selection. table.setCellSelectionEnabled(true); } table.setColumnSelectionAllowed(true); ListSelectionModel colSM = table.getColumnModel().getSelectionModel(); colSM.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { // Ignore extra messages. if (e.getValueIsAdjusting()) return; ListSelectionModel lsm = (ListSelectionModel) e.getSource(); if (lsm.isSelectionEmpty()) { System.out.println("No columns are selected."); } else { int selectedCol = lsm.getMinSelectionIndex(); System.out.println("Column " + selectedCol + " is now selected."); } } }); } if (DEBUG) { table.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { printDebugData(table); } }); } // Create the scroll pane and add the table to it. JScrollPane scrollPane = new JScrollPane(table); // Add the scroll pane to this panel. add(scrollPane); }
From source file:components.SimpleTableSelectionDemo.java
public SimpleTableSelectionDemo() { super(new GridLayout(1, 0)); final String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years", "Vegetarian" }; final Object[][] data = { { "Kathy", "Smith", "Snowboarding", new Integer(5), new Boolean(false) }, { "John", "Doe", "Rowing", new Integer(3), new Boolean(true) }, { "Sue", "Black", "Knitting", new Integer(2), new Boolean(false) }, { "Jane", "White", "Speed reading", new Integer(20), new Boolean(true) }, { "Joe", "Brown", "Pool", new Integer(10), new Boolean(false) } }; final JTable table = new JTable(data, columnNames); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); table.setFillsViewportHeight(true);/*from w w w . j av a2 s .c om*/ table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); if (ALLOW_ROW_SELECTION) { // true by default ListSelectionModel rowSM = table.getSelectionModel(); rowSM.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { //Ignore extra messages. if (e.getValueIsAdjusting()) return; ListSelectionModel lsm = (ListSelectionModel) e.getSource(); if (lsm.isSelectionEmpty()) { System.out.println("No rows are selected."); } else { int selectedRow = lsm.getMinSelectionIndex(); System.out.println("Row " + selectedRow + " is now selected."); } } }); } else { table.setRowSelectionAllowed(false); } if (ALLOW_COLUMN_SELECTION) { // false by default if (ALLOW_ROW_SELECTION) { //We allow both row and column selection, which //implies that we *really* want to allow individual //cell selection. table.setCellSelectionEnabled(true); } table.setColumnSelectionAllowed(true); ListSelectionModel colSM = table.getColumnModel().getSelectionModel(); colSM.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { //Ignore extra messages. if (e.getValueIsAdjusting()) return; ListSelectionModel lsm = (ListSelectionModel) e.getSource(); if (lsm.isSelectionEmpty()) { System.out.println("No columns are selected."); } else { int selectedCol = lsm.getMinSelectionIndex(); System.out.println("Column " + selectedCol + " is now selected."); } } }); } if (DEBUG) { table.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { printDebugData(table); } }); } //Create the scroll pane and add the table to it. JScrollPane scrollPane = new JScrollPane(table); //Add the scroll pane to this panel. add(scrollPane); }
From source file:SimpleTableSelectionDemo.java
public SimpleTableSelectionDemo() { super(new GridLayout(1, 0)); final String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years", "Vegetarian" }; final Object[][] data = { { "Mary", "Campione", "Snowboarding", new Integer(5), new Boolean(false) }, { "Alison", "Huml", "Rowing", new Integer(3), new Boolean(true) }, { "Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false) }, { "Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true) }, { "Philip", "Milne", "Pool", new Integer(10), new Boolean(false) } }; final JTable table = new JTable(data, columnNames); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); if (ALLOW_ROW_SELECTION) { // true by default ListSelectionModel rowSM = table.getSelectionModel(); rowSM.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { //Ignore extra messages. if (e.getValueIsAdjusting()) return; ListSelectionModel lsm = (ListSelectionModel) e.getSource(); if (lsm.isSelectionEmpty()) { System.out.println("No rows are selected."); } else { int selectedRow = lsm.getMinSelectionIndex(); System.out.println("Row " + selectedRow + " is now selected."); }//from w w w .j a va2 s .c o m } }); } else { table.setRowSelectionAllowed(false); } if (ALLOW_COLUMN_SELECTION) { // false by default if (ALLOW_ROW_SELECTION) { //We allow both row and column selection, which //implies that we *really* want to allow individual //cell selection. table.setCellSelectionEnabled(true); } table.setColumnSelectionAllowed(true); ListSelectionModel colSM = table.getColumnModel().getSelectionModel(); colSM.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { //Ignore extra messages. if (e.getValueIsAdjusting()) return; ListSelectionModel lsm = (ListSelectionModel) e.getSource(); if (lsm.isSelectionEmpty()) { System.out.println("No columns are selected."); } else { int selectedCol = lsm.getMinSelectionIndex(); System.out.println("Column " + selectedCol + " is now selected."); } } }); } if (DEBUG) { table.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { printDebugData(table); } }); } //Create the scroll pane and add the table to it. JScrollPane scrollPane = new JScrollPane(table); //Add the scroll pane to this panel. add(scrollPane); }
From source file:com.oracle.cch.swingtest.MainJFrame.java
/** * Creates new form MainJFrame//from w w w .j a va 2s . co m */ public MainJFrame() { initComponents(); ListSelectionModel model = jTable1.getSelectionModel(); model.addListSelectionListener(this); }
From source file:MainClass.java
public MainClass() { super("Selection Model Test"); setSize(450, 350);//from w ww .j a v a 2s . c o m setDefaultCloseOperation(EXIT_ON_CLOSE); TableModel tm = new AbstractTableModel() { public int getRowCount() { return 10; } public int getColumnCount() { return 10; } public Object getValueAt(int r, int c) { return "0"; } }; final JTable jt = new JTable(tm); JScrollPane jsp = new JScrollPane(jt); getContentPane().add(jsp, BorderLayout.CENTER); JPanel controlPanel, buttonPanel, columnPanel, rowPanel; buttonPanel = new JPanel(); final JCheckBox cellBox, columnBox, rowBox; cellBox = new JCheckBox("Cells", jt.getCellSelectionEnabled()); columnBox = new JCheckBox("Columns", jt.getColumnSelectionAllowed()); rowBox = new JCheckBox("Rows", jt.getRowSelectionAllowed()); cellBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { jt.setCellSelectionEnabled(cellBox.isSelected()); columnBox.setSelected(jt.getColumnSelectionAllowed()); rowBox.setSelected(jt.getRowSelectionAllowed()); } }); columnBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { jt.setColumnSelectionAllowed(columnBox.isSelected()); cellBox.setSelected(jt.getCellSelectionEnabled()); } }); rowBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { jt.setRowSelectionAllowed(rowBox.isSelected()); cellBox.setSelected(jt.getCellSelectionEnabled()); } }); buttonPanel.add(new JLabel("Selections allowed:")); buttonPanel.add(cellBox); buttonPanel.add(columnBox); buttonPanel.add(rowBox); columnPanel = new JPanel(); ListSelectionModel csm = jt.getColumnModel().getSelectionModel(); JLabel columnCounter = new JLabel("Selected Column Indices:"); csm.addListSelectionListener(new SelectionDebugger(columnCounter, csm)); columnPanel.add(new JLabel("Selected columns:")); columnPanel.add(columnCounter); rowPanel = new JPanel(); ListSelectionModel rsm = jt.getSelectionModel(); JLabel rowCounter = new JLabel("Selected Row Indices:"); rsm.addListSelectionListener(new SelectionDebugger(rowCounter, rsm)); rowPanel.add(new JLabel("Selected rows:")); rowPanel.add(rowCounter); controlPanel = new JPanel(new GridLayout(0, 1)); controlPanel.add(buttonPanel); controlPanel.add(columnPanel); controlPanel.add(rowPanel); getContentPane().add(controlPanel, BorderLayout.SOUTH); }
From source file:com.cch.aj.entryrecorder.frame.SearchJFrame.java
private void init() { this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); ListSelectionModel model = tblSearch.getSelectionModel(); model.addListSelectionListener(this); }