My main class is a window containing graphical components including a JTable
.
I have created a public class ContextMenu
which is a custom implementation of a JPopupMenu
and containing multiple JMenuItem
.
I have registered a mouseListener on my JTable
to show an instance of ContextMenu
when a right-click is detected.
The problem is the following: "How to pass the selected rows to different function according to the chosen JMenuItem
?"
The obvious answer would be to set ActionListener on my JMenuItem
but remember that JTable
is in a different class/object than the JPopupMenu
.
Some code snipets are worth a thousand words.
public class Tab implements ITab {
private ContextMenu contextMenu;
private JTable table;
private List<SomeObject> toProcess;
--- code --
private JScrollPane drawScrollTable() {
Object columns[] = {
"something",
"somethingElse"
};
Object rows[][] = {};
table = new JTable(new DefaultTableModel(rows, columns));
JScrollPane scrollPane = new JScrollPane(table);
table.setSelectionForeground(Color.BLACK);
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
int selectedRow = table.rowAtPoint(e.getPoint());
if (selectedRow >= 0 && selectedRow < table.getRowCount()) {
if (!table.getSelectionModel().isSelectedIndex(selectedRow)) {
table.setRowSelectionInterval(selectedRow, selectedRow);
}
}
if (e.isPopupTrigger() && e.getComponent() instanceof JTable) {
this.show(e);
}
}
private void show(MouseEvent e){
contextMenu.show(e.getComponent(), e.getX(), e.getY());
}
});
return scrollPane;
}
-- code --
}
public class ContextMenu extends JPopupMenu {
JMenuItem item;
public ContextMenu(IBurpExtenderCallbacks callbacks){
this.item= new JMenuItem("item");
this.item(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Do action involving the selected row, even better if possible action involving the value hold in the column 0 of the selected row and the toProcess private field
}
});
add(item);
}
}
I do not know if what I ask is possible.