Java:从popupmenu actionListener事件获取行数据 [英] java: Get row data from popupmenu actionListener event
问题描述
我这里有一个小场景,也许是重复的.我有JTable,我在其中显示一些数据,我有一个鼠标侦听器,该侦听器监听表上的右键单击并显示带有一个menuitem的弹出菜单.我的目标是,当用户单击菜单项时,我从表中获取值并将其输入到具有一些填充字段的自定义对话框中,因此用户不必手动输入整个对话框,因为我将向对话框提供表格中所选的值.但是我的问题是menuitem actionevent没有办法指出要点,所以我可以使用table.getRowAt()
.我已经阅读了另一条评论(请在此处 https://stackoverflow.com/a/4122082/1788917 )告诉我可以将行号保存在一个变量中,然后菜单操作项可以通过该变量进行访问.
I'm having a little scenario here that maybe a duplicate. I have JTable where i show some data, i have a mouselistener that listens for right clicks on the table and displays a popup menu with one menuitem. My goal is that when the user clicks the menuitem, i get the values from the table and feed them into a custom dialog which has some fill in fields, so that the user doesn't have to feed the whole dialog by hand since i will feed the dialog with the values selected on the table. but my problem is that the menuitem actionevent doesn't have a way of getting the point so that i can use table.getRowAt()
. i have read another comment (check here https://stackoverflow.com/a/4122082/1788917) where i have been told that i can save the row number in a variable that can then be accessed by the actionlistener for the menuitem.
我希望它看起来像这样
theScrollPane.getViewport().add(myJTable, null);
JMenuItem menuItem = new JMenuItem(clickMe);
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
menuItemActionPerformed(e);
}
});
MouseListener popupListener = new PopupListener();
popupMenu.add(menuItem);
myJTable.addMouseListener(popupListener);
protected void menuItemActionPerformed (ActionEvent e) {
// what to do here to get the table row data
// and send it to my custom dialog via it's constructor ???
}
// a simple nested class for the popup
class PopupListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
int row = myJTable.rowAtPoint(e.getPoint());
int selectedRow = myJTable.getSelectedRow();
// just to make sure the popup appears only where the row is
// selected
if (row == selectedRow) {
showPopup(e);
}
}
public void mouseReleased(MouseEvent e) {
int row = myJTable.rowAtPoint(e.getPoint());
int selectedRow = myJTable.getSelectedRow();
if (row == selectedRow) {
showPopup(e);
}
}
private void showPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
popupMenu.show(e.getComponent(), e.getX(), e.getY());
}
}
}
所以我的问题是,保存行号是我可以做到这一点的唯一方法,还是有更好的方法?
so my question is, is saving the row number the only way that i can do this or is there a better way?
推荐答案
我还阅读了另一条评论(请在此处 https://stackoverflow.com/a/4122082/1788917 ),其中有人告诉我可以将行号保存在变量
i have read another comment (check here https://stackoverflow.com/a/4122082/1788917) where i have been told that i can save the row number in a variable
您读错了评论.
如果您阅读该链接上方的答案(即具有7票的答案),您将看到解决方案是选择在显示弹出菜单之前单击的行.然后在菜单项操作"中可以引用
If you read the answer above that link (ie. the one with 7 votes) you will see the solution is to select the row you clicked on BEFORE showing the popup menu. Then in your menu item Action you can reference
table.getSelectedRow();
这篇关于Java:从popupmenu actionListener事件获取行数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!