【问题标题】:highlight a specific row "person", a `TextArea` next to the table will show some text about that person "row".., and it's different from each row..!突出显示特定行“人”,表格旁边的 `TextArea` 将显示有关该人“行”的一些文本..,它与每一行不同..!
【发布时间】:2015-05-12 18:37:24
【问题描述】:

我使用来自TableModelTableModel 女巫查看数据Persons(name, age..ect) 制作了Jtable,我需要的是当我突出显示特定行“人”时,TextArea 旁边表格将显示有关该人“行”的一些文本..,它与每一行都不同..!

我创建了jTable1MousePressed 并尝试了一些代码,但我无法弄清楚如何选择确切的行.. 使用此getSelectedRow() 我无法指定我按下了哪一行..!我读到了ListSelectionListener,但我不明白!

【问题讨论】:

  • 对不起,问题标题的问题..我在发布之前发布了'_',我无法更改它,因为声誉低下!

标签: java swing jtable selecteditem defaulttablemodel


【解决方案1】:

您可以在表格中添加鼠标侦听器并获取列/行。显然,根据需要更改值以适应您的目的。我使用它来确定单击的行和列以及在何处显示弹出菜单。

table.addMouseListener(getMouseAdapter());

MouseAdaptor 的代码:

public MouseAdapter getMouseAdapter() {
        return new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                app.setLastClickedComponent(ADVTableOperations.this);
                rowClicked = rowAtPoint(e.getPoint());
                colClicked = columnAtPoint(e.getPoint());
                if (e.isPopupTrigger() && isPopUpEnabled()) {
                    popUpMenu.show(e.getComponent(), e.getX(), e.getY());
                }
            }

            @Override
            public void mouseClicked(MouseEvent e) {
                app.setLastClickedComponent(ADVTableOperations.this);
                rowClicked = rowAtPoint(e.getPoint());
                colClicked = columnAtPoint(e.getPoint());

                if (e.isPopupTrigger() && isPopUpEnabled()) {
                    popUpMenu.show(e.getComponent(), e.getX(), e.getY());
                }
            }
        };
    }

【讨论】:

  • ok .. 所以我不能只使用一个像 jTable1MousePressed 这样的事件吗?
  • 是的,您可以使用 MousePressed 事件。你可以使用任何你喜欢的。
  • 另外,您使用的是 JScrollPane 吗?
  • 我正在使用使用 NetBeans 的构建器 ..我无法将方法放入 private void jTable1MouseClicked(java.awt.event.MouseEvent evt) { //what should i put here ? }
  • 您可以使用 GUI 构建器的自定义代码部分,或者更好的是,您当然可以从侦听器中调用方法。
猜你喜欢
  • 1970-01-01
  • 2014-05-09
  • 1970-01-01
  • 1970-01-01
  • 2012-02-22
  • 2012-04-28
  • 1970-01-01
  • 2016-07-23
  • 2011-10-01
相关资源
最近更新 更多