For a table, we can set its selection mode: select one row or consecutive rows at a time, or any consecutive rows. We can also set the selection model for its columns.
Create table first
JTable table=new JTable();
For row selection, you should call
getSelectionModel().setSelectionMode(model); function
Where model is the selection mode defined by ListSelectionModel interface:
Constant name | Effect |
---|---|
MULTIPLE_INTERVAL_SELECTION | Select as many lines at a time |
SINGLE_INTERVAL_SELECTION | Select multiple consecutive lines at a time |
SINGLE_SELECTION | Select one row at a time |
For column selection, you should call
setColumnSelectionAllowed(boolean columnSelectionAllowed) method, which is disabled by default.
Here is an example:
package cn.com; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.ButtonGroup; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.ListSelectionModel; import javax.swing.table.DefaultTableModel; import javax.swing.table.JTableHeader; public class TableSelectModeTest extends JFrame { private JTable table; private JRadioButton[] radioButtons ; //Radio private JCheckBox checkBox ; //check box private ButtonGroup buttonGroup ; @SuppressWarnings("serial") public void init() { /* * Initialize member variables */ String[] radioButtonString =new String[] {"Single row","Continuous multiple rows","Arbitrary rows"}; this.radioButtons=new JRadioButton[radioButtonString.length]; this.buttonGroup=new ButtonGroup(); for (int i=0; i<radioButtonString.length; i++) { this.radioButtons[i]=new JRadioButton(radioButtonString[i]); this.radioButtons[i].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { do_radioButtons_actionPrformed(e,radioButtonString); } }); this.buttonGroup.add(this.radioButtons[i]); } this.checkBox=new JCheckBox("Disable column selection"); this.checkBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { do_checkBox_actionPrformed(e); } }); this.table=new JTable(new DefaultTableModel() { @Override public boolean isCellEditable(int rows,int column) { return false; } }); this.table.setRowHeight(30); this.addWindowListener(new WindowAdapter() { @Override public void windowOpened(WindowEvent e) { do_windowOpened(e); } }); /* * Window panel settings */ JScrollPane scrollPane =new JScrollPane(); scrollPane.setViewportView(this.table); JPanel panel1=new JPanel(new GridLayout(2, 1,5,5)); JPanel panel2=new JPanel(new FlowLayout(FlowLayout.CENTER, 7, 7)); JPanel panel3=new JPanel(); JLabel label1=new JLabel("Row selection mode:"); label1.setFont(new Font("Microsoft YaHei",Font.PLAIN,15)); panel2.add(label1); for (JRadioButton radioButton: this.radioButtons) { panel2.add(radioButton); } panel3.add(this.checkBox); panel1.add(panel2); panel1.add(panel3); this.add(scrollPane,BorderLayout.CENTER); this.add(panel1,BorderLayout.SOUTH); /* * Set window properties */ this.setTitle("Set the selection mode of the table"); this.setSize(600,300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.setResizable(false); this.setVisible(true); } /* * Radio button listening function */ protected void do_radioButtons_actionPrformed(ActionEvent e,String[] radioButtonString) { String actionString =e.getActionCommand(); if (actionString.equals(radioButtonString[0])) { this.table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION); } else if (actionString.equals(radioButtonString[1])) { this.table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); } else if (actionString.equals(radioButtonString[2])) { this.table.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); } } protected void do_checkBox_actionPrformed(ActionEvent e) { if (this.checkBox.isSelected()) { this.checkBox.setText("Enable column selection"); this.table.setColumnSelectionAllowed(true); } else { this.checkBox.setText("Disable column selection"); this.table.setColumnSelectionAllowed(false); } } protected void do_windowOpened(WindowEvent e) { /* * Initialize table */ DefaultTableModel defaultTableModel=(DefaultTableModel) this.table.getModel(); defaultTableModel.setColumnIdentifiers(new Object[] {"Title", "Press", "Publishing time", "Series category", "Price" }); defaultTableModel.addRow(new Object[] { "Java From introduction to mastery (2nd Edition)", "tsinghua university press ", "2010-07-01", "Introduction series of software engineers", "59.8 element" }); defaultTableModel.addRow(new Object[] { "PHP From introduction to mastery (2nd Edition)", "tsinghua university press ", "2010-07-01", "Introduction series of software engineers", "69.8 element" }); defaultTableModel.addRow(new Object[] { "Visual Basic From introduction to mastery (2nd Edition)", "tsinghua university press ", "2010-07-01", "Introduction series of software engineers", "69.8 element" }); defaultTableModel.addRow(new Object[] { "Visual C++From introduction to mastery (2nd Edition)", "tsinghua university press ", "2010-07-01", "Introduction series of software engineers", "69.8 element" }); this.table.setModel(defaultTableModel); JTableHeader header=this.table.getTableHeader(); header.setFont(new Font("Microsoft YaHei",Font.PLAIN,20)); header.setPreferredSize(new Dimension(header.getWidth(),25)); } public TableSelectModeTest() { init(); } public static void main(String args[]) { TableSelectModeTest winTableSelectModeTest =new TableSelectModeTest(); } }
Let's see the effect: