java - 使用 JTable 选择表中的所有行

标签 java swing jtable

如何在用户不使用鼠标选择的情况下选择表格中的所有行?例如,我有一个名为 InputTable 的表。使用 ActionListener/TableModelListener,我可以通过这种方式在表格中获取选定的行(当用户单击它们时):

int[] rows = inputTable.getSelectedRows();

我现在想选择输入表中的所有行并将其分配给 say,int [] rows1。是否有像 getSelectedRows() 这样的命令,我可以在其中选择所有行而无需用户交互?我知道有一个 SelectAll() 但我只想要特定于行的东西。

最佳答案

我认为您正在尝试以编程方式选择JTable

JTable 只是一种显示机制。您不是选择表( View )中的行,而是选择 SelectionModel 中的行,所以看看我做的这个小例子:

enter image description here

import java.util.ArrayList;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class Test extends JFrame {

    public static void main(String[] args) throws Exception {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Test().createAndShowUI();
            }
        });
    }

    private void createAndShowUI() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        initComponents(frame);
        frame.pack();
        frame.setVisible(true);
    }

    private void initComponents(JFrame frame) {

        String data[][] = {
            {"1", "2", "3"},
            {"4", "5", "6"},
            {"7", "8", "9"},
            {"10", "11", "12"}
        };

        String col[] = {"Col 1", "Col 2", "Col 3"};

        DefaultTableModel model = new DefaultTableModel(data, col);
        JTable table = new JTable(model);

        //call method to select rows (select all rows)
        selectRows(table, 0, table.getRowCount());

        //call method to return values of selected rows
        ArrayList<Integer> values = getSelectedRowValues(table);

        //prints out each values of the selected rows
        for (Integer integer : values) {
            System.out.println(integer);
        }

        frame.getContentPane().add(new JScrollPane(table));
    }

    private void selectRows(JTable table, int start, int end) {
        // Use this mode to demonstrate the following examples
        table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        // Needs to be set or rows cannot be selected
        table.setRowSelectionAllowed(true);
        // Select rows from start to end if start is 0 we change to 1 or leave it (used to preserve coloums headers)
        table.setRowSelectionInterval(start, end - 1);
    }

    /**
     * Will return all selected rows values
     *
     * @param table
     * @return ArrayList<Intger> values of each selected row for all coloumns
     */
    private ArrayList<Integer> getSelectedRowValues(JTable table) {
        ArrayList<Integer> values = new ArrayList<>();
        int[] vals = table.getSelectedRows();
        for (int i = 0; i < vals.length; i++) {
            for (int x = 0; x < table.getColumnCount(); x++) {
                System.out.println(table.getValueAt(i, x));
                values.add(Integer.parseInt((String) table.getValueAt(i, x)));
            }
        }
        return values;
    }
}

魔法在这里发生:

    private void selectRows(JTable table, int start, int end) {
        // Use this mode to demonstrate the following examples
        table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        // Needs to be set or rows cannot be selected
        table.setRowSelectionAllowed(true);
        // Select rows from start to end if start is 0 we change to 1 or leave it (used to preserve coloums headers)
        table.setRowSelectionInterval(start, end - 1);
    }

有关更多示例,请查看 here向您展示如何在 JTable 上为行和列使用 SelectionModel

关于java - 使用 JTable 选择表中的所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11693190/

相关文章:

单击 jButton 时 Java GUI 卡住

java - 从 doInBackground 在 EDT 上运行函数

java - 如何在 Java 中创建垂直制表符?

java - 如何防止 JTable 的 ListSelectionListener 自行取消选择行?

java - Java Struts 中 HTML 下拉菜单的动态数量

java - 将 HashMap 与流一起使用时进行索引

java - 创建使用给定列表中的键初始化的映射

Java:如何重用SocketChannel

java - 无法使用 Jtextfields 添加焦点监听器或在 Jtable 中设置字体

java - Jtable 中具有一个键和多个值的 hashmap?