如何在JTable中选择行或列

如何在JTable中选择行或列

本文介绍了如何在JTable中选择行或列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,在JTable中,如果选择一个单元格,则将选择该单元格的整个行.我想保留此功能.

By default, in a JTable, if you select a cell, that cell's entire row is selected. I would like to keep this functionality.

但是,默认情况下,标题(每列上方)中的按钮不起作用.我希望能够单击其中之一,并突出显示整个列(并且我不想摆脱通过执行此操作选择整个行的功能)

However, the buttons in the header (above each column) do nothing by default. I want to be able to click one of them and have that entire column be highlighted (and I dont want to get rid of the ability to select entire rows by doing this)

我该怎么做?

推荐答案

https://kodejava.org/how-do-i-allow-row-or-column-selection-in-jtable/:

要在JTable组件中允许行选择或列选择或行和列选择,我们可以通过调用JTable的 setRowSelectionAllowed()和JTable的 setColumnSelectionAllowed()方法.

"To allow a row selection or a column selection or both row and column selection in JTable component we can turn it on and off by calling the JTable‘s setRowSelectionAllowed() and JTable‘s setColumnSelectionAllowed() methods.

这两种方法均接受布尔值值,指示是否允许选择. 将它们都设置为true允许我们从JTable中选择行和列."

Both of these method accept a boolean value indicating whether the selection is allowed or not allowed. Setting both of them to true allow us to select rows and columns from JTable."

package org.kodejava.example.swing;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;

public class TableAllowColumnSelection extends JPanel {
public TableAllowColumnSelection() {
    initializePanel();
}

private void initializePanel() {
    this.setLayout(new BorderLayout());
    this.setPreferredSize(new Dimension(500, 150));

    JTable table = new JTable(new PremiereLeagueTableModel());
    // sets to false to disallow row selection in the table
    // model.
    table.setRowSelectionAllowed(false);

    // Sets to true to allow column selection in the table
    // model.
    table.setColumnSelectionAllowed(true);

    JScrollPane pane = new JScrollPane(table);
    this.add(pane, BorderLayout.CENTER);
}

public static void showFrame() {
    JPanel panel = new TableAllowColumnSelection();
    panel.setOpaque(true);

    JFrame frame = new JFrame("JTable Column Selection");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setContentPane(panel);
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            TableAllowColumnSelection.showFrame();
        }
    });
}

class PremiereLeagueTableModel extends AbstractTableModel {
    // TableModel's column names
    private String[] columnNames = {
        "TEAM", "P", "W", "D", "L", "GS", "GA", "GD", "PTS"
    };

    // TableModel's data
    private Object[][] data = {
        { "Liverpool", 3, 3, 0, 0, 7, 0, 7, 9 },
        { "Tottenham", 3, 3, 0, 0, 8, 2, 6, 9 },
        { "Chelsea", 3, 3, 0, 0, 8, 3, 5, 9 },
        { "Watford", 3, 3, 0, 0, 7, 2, 5, 9 },
        { "Manchester City", 3, 2, 1, 0, 9, 2, 7, 7 }
    };

    public int getRowCount() {
        return data.length;
    }

    public int getColumnCount() {
        return columnNames.length;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        return data[rowIndex][columnIndex];
    }
 }
}

这篇关于如何在JTable中选择行或列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 01:34