对于一个表格,我们可以设置其选择模式:设置每次选择一行或者连续多行,或者任意连续多行。我们也可以设置其列的选择模型。
先创建表格
JTable table=new JTable();
对于行选择方式,应该调用
getSelectionModel().setSelectionMode(model);函数
其中model是ListSelectionModel接口定义的选择模式:
常量名 | 作用 |
---|---|
MULTIPLE_INTERVAL_SELECTION | 一次选择任意多行 |
SINGLE_INTERVAL_SELECTION | 一次选择连续多行 |
SINGLE_SELECTION | 一次选择一行 |
对于列选择方式,应该调用
setColumnSelectionAllowed(boolean columnSelectionAllowed) 方法,该方法默认情况下是禁用的。
下面看看具体的例子:
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 ; //单选框
private JCheckBox checkBox ; //复选框
private ButtonGroup buttonGroup ;
@SuppressWarnings("serial")
public void init()
{
/*
* 初始化成员变量
*/
String[] radioButtonString =new String[] {"单行","连续多行","任意多行"};
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("禁用列选择");
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);
}
});
/*
* 窗口面板设置
*/
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("行选择模式:");
label1.setFont(new Font("微软雅黑",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);
/*
* 设置窗口属性
*/
this.setTitle("设置表格的选择模式");
this.setSize(600,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setVisible(true);
}
/*
* 单选按钮 监听函数
*/
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("启用列选择");
this.table.setColumnSelectionAllowed(true);
}
else
{
this.checkBox.setText("禁用列选择");
this.table.setColumnSelectionAllowed(false);
}
}
protected void do_windowOpened(WindowEvent e)
{
/*
* 初始化表格
*/
DefaultTableModel defaultTableModel=(DefaultTableModel) this.table.getModel();
defaultTableModel.setColumnIdentifiers(new Object[] {"书名", "出版社", "出版时间", "丛书类别", "定价" });
defaultTableModel.addRow(new Object[] { "Java从入门到精通(第2版)", "清华大学出版社", "2010-07-01", "软件工程师入门丛书", "59.8元" });
defaultTableModel.addRow(new Object[] { "PHP从入门到精通(第2版)", "清华大学出版社", "2010-07-01", "软件工程师入门丛书", "69.8元" });
defaultTableModel.addRow(new Object[] { "Visual Basic从入门到精通(第2版)", "清华大学出版社", "2010-07-01", "软件工程师入门丛书", "69.8元" });
defaultTableModel.addRow(new Object[] { "Visual C++从入门到精通(第2版)", "清华大学出版社", "2010-07-01", "软件工程师入门丛书", "69.8元" });
this.table.setModel(defaultTableModel);
JTableHeader header=this.table.getTableHeader();
header.setFont(new Font("微软雅黑",Font.PLAIN,20));
header.setPreferredSize(new Dimension(header.getWidth(),25));
}
public TableSelectModeTest()
{
init();
}
public static void main(String args[])
{
TableSelectModeTest winTableSelectModeTest =new TableSelectModeTest();
}
}
看看效果吧: