How can I target (in JXTable) a specific column values from different rows to open a new frame when I double click?

I have this code for now:
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);
}
}
});
Solved with this code:
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);
}
}
});
UPDATE 1:
As @mKorbel said, I want to use a JPopupMenu like this example (provided by @mKorbel). But how can I implement a JDialog which opens when the JMenuItem is clicked.
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());
}
}
}
}
don’t create a new
J(X)Frameon runtime, contents of this container will be the same, no reason for that, create that once time, reuse this container, and for visibility on the screen (tohide / show)usesetVisible(false / true)don’t create a
J(X)Frame, create aJ(X)Dialogwith parent to theJXTable, then dialog will be centered to theJXTablesyou can to use
ListSelectionListenertoo, but with single selection modelread Oracle tutorial about
JList,JTableandListSelectionListener, logics will be the same and forSwingX componentstoo,I’d be use JPopup as control for displaying the JDialog, just to avoids annoying events came from mouse or keyboard
EDIT
have to add
d.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE), thenJDialognever will be closed only changed to thesetVisible(false);have to code value from selected
JTables row,create a new, separate void that fills value from
JTables row to theJComponents placed intoJDialogbe ensure that
J(X)Tablehas changedselectionModeto thesingle selection(not required) now you can possitioning
JDialogon the screen,JDialogstill is not visible on the screenthenafter to call
d.setVisble(true), I’d suggest wrap this code line to theinvokeLater(), thenJDialogwill be visible after all changes are done, move this event to the end of theEvent Dispatch Thread