双击时,如何定位(在 JXTable 中)来自不同行的特定列值以打开新框架?
我现在有这个代码:
myTable.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
JXTable target = (JXTable)e.getSource();
int row = target.getSelectedRow();
int column = target.getTableColumn(3); //for example the third column
new Detail().setVisible(true);
}
}
});
使用此代码解决:
final JOptionPane pane = new JOptionPane((Frame) null, JOptionPane.PLAIN_MESSAGE, JOptionPane.DEFAULT_OPTION);
final JDialog d = pane.createDialog((Frame) null, "Comments");
d.setModal(false);
d.setAlwaysOnTop(true);
d.pack();
myTable.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
int row = myTable.rowAtPoint(e.getPoint());
Object value1 = myTable.getValueAt(row, 4);
pane.setMessage(value1);
if (!d.isVisible())
d.setLocationRelativeTo(myTable);
d.setVisible(true);
}
}
});
更新 1:
正如@mKorbel 所说,我想使用像这个例子一样的JPopupMenu (由@mKorbel 提供)。但是我怎样才能实现一个在单击 JMenuItem 时打开的 JDialog。
private void createPopupMenu() {
JPopupMenu popup = new JPopupMenu();
JMenuItem MenuItemComments = new JMenuItem("Show Comments");
JMenuItem MenuItemReference = new JMenuItem("Show Reference");
popup.add(MenuItemComments);
popup.add(MenuItemReference);
MouseListener popupListener = new PopupListener(popup);
Table_Employee.addMouseListener(popupListener);
}
private class PopupListener extends MouseAdapter {
private JPopupMenu popup;
PopupListener(JPopupMenu popupMenu) {
popup = popupMenu;
}
@Override
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}
@Override
public void mouseReleased(MouseEvent e) {
if (Table_Employee.getSelectedRow() != -1) {
maybeShowPopup(e);
}
}
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
// get row that pointer is over
int row = Table_Employee.rowAtPoint(e.getPoint());
// if pointer is over a selected row, show popup
if (Table_Employee.isRowSelected(row)) {
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
}
}