[英]JTable - Selected Row click event
I have a Jtable that is populated with a linkedlist through an AbstractTableModel.我有一个 Jtable,它通过 AbstractTableModel 填充了一个链表。
What I want to do is when I click (left-mouse click) on a row in the JTable, the linkedlist is search (in this case it contains movie titles) and displays the values in the linked list in Jtextboxes我想要做的是当我单击(鼠标左键单击)JTable 中的一行时,链接列表是搜索(在这种情况下它包含电影标题)并在 Jtextboxes 中显示链接列表中的值
How do I do this?我该怎么做呢?
Here is the code这是代码
My guess it retrieve the data from the selected row into an array, split it, and put it into the jtextareas.我猜它会将选定行中的数据检索到一个数组中,将其拆分,然后将其放入 jtextarea 中。 How can I do this ?
我怎样才能做到这一点 ?
Here's how I did it:这是我如何做到的:
table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent event) {
// do some actions here, for example
// print first column value from selected row
System.out.println(table.getValueAt(table.getSelectedRow(), 0).toString());
}
});
This code reacts on mouse click and item selection from keyboard.此代码对鼠标单击和键盘上的项目选择做出反应。
private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {
JTable source = (JTable)evt.getSource();
int row = source.rowAtPoint( evt.getPoint() );
int column = source.columnAtPoint( evt.getPoint() );
String s=source.getModel().getValueAt(row, column)+"";
JOptionPane.showMessageDialog(null, s);
}
if you want click cell or row in jtable use this way如果要单击 jtable 中的单元格或行,请使用这种方式
To learn what row was selected, add a ListSelectionListener
, as shown in How to Use Tables in the example SimpleTableSelectionDemo
.要了解选择了哪一行,请添加
ListSelectionListener
,如示例SimpleTableSelectionDemo
中的如何使用表中所示。 A JList
can be constructed directly from the linked list's toArray()
method, and you can add a suitable listener to it for details. JList
可以直接从链表的toArray()
方法构造,您可以向其添加合适的侦听器以了解详细信息。
I would recommend using Glazed Lists for this.我建议为此使用Glazed Lists 。 It makes it very easy to map a data structure to a table model.
它使得将数据结构映射到表模型变得非常容易。
To react to the mouseclick on the JTable, use an ActionListener: ActionListener on JLabel or JTable cell要对 JTable 上的鼠标单击做出反应,请在 JLabel 或 JTable 单元格上使用 ActionListener: ActionListener
From the source with some enhancement and edit:从带有一些增强和编辑的源代码:
public class SelectionListener implements ListSelectionListener {
@Override
public void valueChanged(ListSelectionEvent event) {
if (!event.getValueIsAdjusting() && table.getSelectedRow() != -1) {
int viewRow = table.getSelectedRow();
if (viewRow < 0) {
//Selection got filtered away.
} else {
int columnIndex = 1;
int modelRow = table.convertRowIndexToModel(viewRow);
Object modelvalue = table.getModel().getValueAt(modelRow, columnIndex);
Object tablevalue = table.getValueAt(viewRow, columnIndex);
System.out.println(modelvalue + "=" + tablevalue);
}
}
}
}
Then add ListSelectionListener
to JTable
:然后将
ListSelectionListener
添加到JTable
:
table.getSelectionModel().addListSelectionListener(new SelectionListener());
viewRow
and modelRow
will be effectively different when applying TableRowSorter
.应用
TableRowSorter
时, viewRow
和modelRow
将有效地不同。
You can use the MouseClicked
event:您可以使用
MouseClicked
事件:
private void tableMouseClicked(java.awt.event.MouseEvent evt) {
// Do something.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.