表格的选择模式是依据我们前面所讲的ListSelectionModel而来,因此它的操作模式与事件处理跟JList没什么分别!我们稍微复习一 下ListSelectionModel这个Interface,它包含了3个常数值,如下:
分别可让用户作单一选择,连续区间选择与多重选择.当用户作后面两个模式的操作时,应配合[Shift]键或[Ctrl]键.
要使用ListSelectionModel可利用JTable的getSelectionModel()方法取得ListSelectionModel对象,再利用ListSelectionModel界面所 定义的setSelectionModel()来设置选择模式.
如同JList一般,当用户对表格作数据域位的选取时会产生ListSelectionEvent事件,要处理这个事件就必须实现ListSelectionListener 这个界面,此界面定义了一个方法,那就是valueChanged().
我们来看下面的例子,用户可在按钮上选择哪种选择模式,当用户选取表格数据时,程序会将用户选取的数据显示在表格下面的JLabel中.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class SelectionModelDemo implements ActionListener,ListSelectionListener{
JTable table=null;
ListSelectionModel selectionMode=null;
JLabel label=null;//显示用户选取表格之用
public SelectionModelDemo(){
JFrame f=new JFrame();
String[] name={"字段1","字段2","字段3","字段4","字段5"};
String[][] data=new String[5][5];
int value=1;
for(int i=0;i< data.length;i++){
for (int j=0;j< data[i].length;j++){
data[i][j]=String.valueOf(value++);
}
}
table=new JTable(data,name);
table.setPreferredScrollableViewportSize(new Dimension(400,80));
table.setCellSelectionEnabled(true);//使得表格的选取是以cell为单位,而不是以列为单位.若你没有写此行,则在选取表格数
//据时以整列为单位.
selectionMode=table.getSelectionModel();//取得table的ListSelectionModel.
selectionMode.addListSelectionListener(this);
JScrollPane s=new JScrollPane(table);
JPanel panel=new JPanel();
JButton b=new JButton("单一选择");
panel.add(b);
b.addActionListener(this);
b=new JButton("连续区间选择");
panel.add(b);
b.addActionListener(this);
b=new JButton("多重选择");
panel.add(b);
b.addActionListener(this);
label=new JLabel("你选取:");
Container contentPane=f.getContentPane();
contentPane.add(panel,BorderLayout.NORTH);
contentPane.add(s,BorderLayout.CENTER);
contentPane.add(label,BorderLayout.SOUTH);
f.setTitle("SelectionModelDemo");
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
/*处理按钮事件,利用ListSelectionModel界面所定义的setSelectionMode()方法来设置表格选取模式.*/
public void actionPerformed(ActionEvent e){
if (e.getActionCommand().equals("单一选择"))
selectionMode.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
if (e.getActionCommand().equals("连续区间选择"))
selectionMode.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
if (e.getActionCommand().equals("多重选择"))
selectionMode.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
table.revalidate();
}
/*当用户选取表格数据时会触发ListSelectionEvent,我们实现ListSelectionListener界面来处理这一事件.ListSelectionListener界
*面只定义一个方法,那就是valueChanged().
*/
public void valueChanged(ListSelectionEvent el){
String tempString="";
//JTable的getSelectedRows()与getSelectedColumns()方法会返回已选取表格cell的index Array数据.
int[] rows=table.getSelectedRows();
int[] columns=table.getSelectedColumns();
//JTable的getValueAt()方法会返回某行的cell数据,返回值是Object数据类型,因此我们要自行转成String数据类型.
for (int i=0;i< rows.length;i++){
for (int j=0;j< columns.length;j++)
tempString = tempString+" "+(String)table.getValueAt(rows[i], columns[j]);
}
label.setText("你选取:"+tempString);
}
public static void main(String[] args){
new SelectionModelDemo();
}
}
说明:
在此范例中,我们要处理ActionEvent与ListSelectionEvent,因此在程序中我们要实现ActionListenrer与ListSelectionListener界 面,而ListSelectionEvent是属于Swing事件,因此程序中我们要import javax.swing.event package进来.