JTable SelectionModel

SelectionModel

表格的选择模式是依据我们前面所讲的ListSelectionModel而来,因此它的操作模式与事件处理跟JList没什么分别!我们稍微复习一 下ListSelectionModel这个Interface,它包含了3个常数值,如下:

  • static int SINGLE_SELECTION
  • static int SINGLE_INTERVAL_SELECTION
  • static int MULTIPLE_INTERVAL_SELECTION

分别可让用户作单一选择,连续区间选择与多重选择.当用户作后面两个模式的操作时,应配合[Shift]键或[Ctrl]键.

要使用ListSelectionModel可利用JTable的getSelectionModel()方法取得ListSelectionModel对象,再利用ListSelectionModel界面所 定义的setSelectionModel()来设置选择模式.

如同JList一般,当用户对表格作数据域位的选取时会产生ListSelectionEvent事件,要处理这个事件就必须实现ListSelectionListener 这个界面,此界面定义了一个方法,那就是valueChanged().

我们来看下面的例子,用户可在按钮上选择哪种选择模式,当用户选取表格数据时,程序会将用户选取的数据显示在表格下面的JLabel中.

SelectionModelDemo.java
 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进来. 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值