【问题标题】:How to select a column of a JTable in JDialog by selecting a header如何通过选择标题在JDialog中选择JTable的列
【发布时间】:2015-01-29 20:17:05
【问题描述】:

我一直在尝试使用JDialog 做的是......

  1. 通过单击标题选择JTable
  2. 检查用户选择了哪一列
  3. 获取列内单元格的值

根据this postthis page,可以通过点击标题来选择一列,通过设置JTableHeader

但是,它们似乎都不适用于我正在尝试做的事情。

首先,我不确定将JTableHeader 放在哪里。上面的示例似乎已将其用于初始化,但我在编码中没有看到任何合适的空间来执行此操作。

至少我知道第二个例子是JPanel。因此,为了在JDialog 中有一个JTableHeaderJTableHeader 需要设置在一个完全不同的位置,因为默认情况下无法手动修改 JDialog 的initComponents()

此外,我找不到如何选择标题(与单个单元格不同)。我假设我需要事先设置一个JTableHeader

最后,我没有看到任何方法来检测选择了哪一列。至少我找到了jTable.getValueAt(int, int) 方法,但是这种方法似乎是为了获取单个单元格。

现在我怀疑使用 JTable 和 JDialog 可能无法做到这一点。如果您能提供任何见解,我将不胜感激。

我添加了initComponents()的一部分,以便您轻松理解。

private void initComponents() {

    //here are irrelevant codes
    jTable1 = new javax.swing.JTable();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    jLabel1.setFont(new java.awt.Font("MS UI Gothic", 3, 18)); // NOI18N
    jLabel1.setText("Choose level(s) or unit(s)");

    //irrelevant codes

    jTable1.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {
            {"EN6", "EN3", "EN5", "IN1"},
            {"EN2", "EN3", null, "IN4"},
            {null, null, null, "IN1"},
            {null, null, null, "IN2"},

        new String [] {
            "EN2", "EN3", "EN5", "IN1"
        }
    ) {
        Class[] types = new Class [] {
            java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
        };
public Class getColumnClass(int columnIndex) {
            return types [columnIndex];
        }
    });
//lots of lines, seem to be irrelevant
pack();
}

【问题讨论】:

  • 我想你在找jTable1.getModel().getValueAt(x,y) 和相关函数。
  • 选择可设置,有行、列、单元选择的选项

标签: java swing jtable selection jtableheader


【解决方案1】:

“[...]可以通过单击标题来选择列,通过设置 JTableHeader。”

根据您的要求,我认为您不需要提供自己的表头,而是将MouseListener 附加到默认表头。通过这种方式并使用行和列选择模型,您可以轻松实现目标。

片段

final JTable table = new JTable(tableModel);
table.setColumnSelectionAllowed(true);
table.getTableHeader().addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) {
        // Get the right column based on MouseEvent#getPoint()
        int columnIndex = table.columnAtPoint(e.getPoint());
        // Set this column as the selected one in the columns selection model
        table.getColumnModel().getSelectionModel().setSelectionInterval(columnIndex, columnIndex);
        // Set all the rows as the selected ones in the rows selection model
        table.getSelectionModel().setSelectionInterval(0, table.getRowCount() - 1);
        // Print the values in selected column
        for (int rowIndex = 0; rowIndex < table.getRowCount(); rowIndex++) {
            System.out.println(table.getValueAt(rowIndex, columnIndex));
        }
    }
});

注意:不要忘记允许选择列。

见:

【讨论】:

    【解决方案2】:

    您可以使用它来获取选择单元格的值。但这是你想要的吗?

    table.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
            if (arg0.getClickCount() == 1) { // 1 : one click, 2 : double click, 3 : right click
    
                int column = table.getSelectedColumn();
    
                int row = table.getSelectedRow();
                String str = (String) table.getValueAt(row, column);
    
                int[] rows = table.getSelectedRows();
                String str2 = (String) table.getValueAt(rows[0], column);
            }
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-20
      • 1970-01-01
      • 1970-01-01
      • 2021-02-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-03
      相关资源
      最近更新 更多