该程序向您展示如何以JTable编程方式选择一些行。下面我们演示了如何使用setRowSelectionInterval()方法选择行,如何使用方法删除或清除行选择removeRowSelectionInterval()以及如何使用addRowSelectionInterval()将更多行添加到先前选择的行。
package org.nhooo.example.swing; import javax.swing.*; import javax.swing.table.AbstractTableModel; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class JTableRowSelectProgrammatically extends JPanel { public JTableRowSelectProgrammatically() { initializePanel(); } private void initializePanel() { setLayout(new BorderLayout()); setPreferredSize(new Dimension(475, 250)); final JTable table = new JTable(new MyTableModel()); table.setFillsViewportHeight(true); JScrollPane pane = new JScrollPane(table); JLabel label1 = new JLabel("From row: "); final JTextField field1 = new JTextField(3); JLabel label2 = new JLabel("To row: "); final JTextField field2 = new JTextField(3); JButton select = new JButton("Select"); JButton clear = new JButton("Clear"); JButton add = new JButton("Add another one"); // 启用行选择模式并禁用列选择 // 模式。 table.setRowSelectionAllowed(true); table.setColumnSelectionAllowed(false); // 根据输入选择行。 select.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { int index1 = 0; int index2 = 0; try { index1 = Integer.valueOf(field1.getText()); index2 = Integer.valueOf(field2.getText()); } catch (NumberFormatException e) { e.printStackTrace(); } if (index1 < 0 || index2 < 0 || index1 >= table.getRowCount() || index2 >= table.getRowCount()) { JOptionPane.showMessageDialog(table, "Selection out of range!"); } else { table.setRowSelectionInterval(index1, index2); } } }); // 清除行选择 clear.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { table.removeRowSelectionInterval(0, table.getRowCount() - 1); field1.setText(""); field2.setText(""); } }); // 在最后选择的行之后再添加一行。 add.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { int index2 = 0; try { index2 = Integer.valueOf(field2.getText()); } catch (NumberFormatException e) { e.printStackTrace(); } index2 = index2 + 1; table.addRowSelectionInterval(index2, index2); field2.setText(String.valueOf(index2)); } }); JPanel command = new JPanel(new FlowLayout()); command.add(label1); command.add(field1); command.add(label2); command.add(field2); command.add(select); command.add(clear); command.add(add); add(pane, BorderLayout.CENTER); add(command, BorderLayout.SOUTH); } public static void showFrame() { JPanel panel = new JTableRowSelectProgrammatically(); panel.setOpaque(true); JFrame frame = new JFrame("JTable Row Selection"); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setContentPane(panel); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JTableRowSelectProgrammatically.showFrame(); } }); } public class MyTableModel extends AbstractTableModel { private String[] columns = {"ID", "NAME", "AGE", "A STUDENT?"}; private Object[][] data = { {1, "Alice", 20, Boolean.FALSE}, {2, "Bob", 10, Boolean.TRUE}, {3, "Carol", 15, Boolean.TRUE}, {4, "Ignasia", 20, Boolean.TRUE}, {5, "Mallory", 25, Boolean.FALSE} }; public int getRowCount() { return data.length; } public int getColumnCount() { return columns.length; } public Object getValueAt(int rowIndex, int columnIndex) { return data[rowIndex][columnIndex]; } @Override public String getColumnName(int column) { return columns[column]; } // JTable使用此方法定义默认值 //每个单元格的渲染器或编辑器。例如,如果您有 //布尔数据,它将呈现为复选框。一种 // 数字值右对齐。 @Override public Class<?> getColumnClass(int columnIndex) { return data[0][columnIndex].getClass(); } } }
下面是该程序的屏幕截图。