简体   繁体   English

JTable选择其他行并在单击某些单元格时获取其数据

[英]JTable choose other rows and get their data when certain cell is clicked

I have a JTable. 我有一个JTable。 I want to create an event for one cell that if the user clicks on it, he is able to choose one or more rows from the table and the corresponding IDs are saved in that cell. 我想为一个单元格创建一个事件,如果用户单击该事件,他可以从表中选择一个或多个行,并将相应的ID保存在该单元格中。

在此处输入图片说明

So in the example the user would click on "Click here to choose" in row 2 and then click on eg row 1 and row 3. The cell "click here to choose" should then be overwritten with something like 1 and 3 afterwards: 因此,在该示例中,用户将在第2行中单击“单击此处进行选择”,然后在第1行和第3行上单击。然后,应将“单击此处进行选择”单元格覆盖为1和3:

在此处输入图片说明

I'm thinking of somehow creating a MouseAdapter Event on click on the cell but I have no real idea how to do it. 我正在考虑以某种方式在单元格上单击时创建MouseAdapter事件,但我不知道如何执行此操作。 Any idea how I can approach this? 知道我该如何处理吗?

Use a ListSelectionListener with MULTIPLE_INTERVAL_SELECTION . ListSelectionListenerMULTIPLE_INTERVAL_SELECTION In the handler, update the table's model using setValueAt() to reflect the change. 在处理程序中,使用setValueAt()更新表的模型以反映更改。

yes you definitely need to use MouseAdapter as below(you have the cell if "if condition become true"): 是的,您肯定需要使用MouseAdapter,如下所示(如果“如果条件成立,则具有单元格”):

jt.addMouseListener(new java.awt.event.MouseAdapter() {
    @Override
    public void mouseClicked(java.awt.event.MouseEvent evt) {
        int r = jt.rowAtPoint(evt.getPoint());
        int c = jt.columnAtPoint(evt.getPoint());
        if (r >= 0 && c >= 0) {
            ......

        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM