Java Swing JTable 表格【8:表格使用选择器SelectionModel】

来源:互联网 发布:淘宝商城质检报告 编辑:程序博客网 时间:2024/06/06 05:06

选择器是指表格的选择模式SelectionModel,选择器的最大用处就是使用户能够以不同的方式选择表中的数据,例如平时处理Excel表时,可以一次性选择一个数据,也可以一次性选择多个数据等。选择器的操作方式与JList操作方式极为相似,包括其事件驱动。具体如下图:

这里写图片描述

下面通过一个例子来说明:

package com.acconsys.swing.chapter14;import java.awt.BorderLayout;import java.awt.Container;import java.awt.Dimension;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.ListSelectionModel;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;/** * 这段代码展示如何创建选择器,和使用选择器来以不同的方式选择表格中的单元格 *  * @author Administrator *  */public class Test10 implements ActionListener, ListSelectionListener {    JTable table = null;    ListSelectionModel selectionMode = null;    JLabel label = null;// 显示用户选取表格之用    public Test10() {        JFrame f = new JFrame();        Object[][] p = { { "王鹏", "90", "100", "190" },                { "王鹏1", "91", "101", "191" }, { "王鹏2", "92", "102", "192" },                { "王鹏3", "93", "103", "193" }, { "王鹏4", "94", "104", "194" }, };        String[] n = { "姓名", "语文", "数学", "总分" };        table = new JTable(p, n);        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("选择器的测试实例");        f.pack();        f.setVisible(true);        f.addWindowListener(new WindowAdapter() {            @Override            public void windowClosing(WindowEvent e) {                System.exit(0);            }        });    }    @Override    public void valueChanged(ListSelectionEvent e) {        /**         * 当用户选取表格数据时会出发ListSelectionEvent,使用ListSelectionListener界面来处理这一事件         */        String tempString = "";        // JTable的getSelectedRows()与getSelectedColumns()方法会返回        // 已取表格cell的index Array数据        int[] rows = table.getSelectedRows();        int[] columns = table.getSelectedColumns();        for (int i = 0; i < rows.length; i++) {            // JTable的getValueAt()方法会返回某行的cell数据,返回值是Object数据类型            // 一次要自动转成String数据类型            for (int j = 0; j < columns.length; j++) {                tempString = tempString + ""                        + (String) table.getValueAt(rows[i], columns[j]);                System.out.println("第" + i + "行,第" + j + "列数据为: " + tempString);            }        }        label.setText("你选取的数据是:" + tempString);    }    @Override    public void actionPerformed(ActionEvent e) {        /**         * 处理按钮事件,利用ListSelectionModel界面所定义的setSelectionModel()方法来设置表格选取模式         */        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();    }    public static void main(String[] args) {        new Test10();    }}

截图如下:
这里写图片描述
说明如下:
1. 当单击“单一选择”按钮时,按住Ctrl或者Shift键,一次性只能选择一个单元格数据,
2. 当单击“连续区间选择”按钮时,按住Ctrl或者Shift键,一次性可以选择连续区间的数据。
3. 当单击“多重选择”按钮时,按住Ctrl或者Shift键,一次性可以选择所有的数据。
注意:当进行或两种模式的操作时,应配合Shift键或Ctrl键

1 0