I am currently attempting to display a JPopupMenu
when right clicking a JTable
.
I have achieved this in different ways already, but none of them allow me to do what this question is about: executing some code after I right click on the table, but the popup menu is on screen.
When the popup menu is displayed and I right click outside of it, it opens up again on a different location. So there must be a listener inside it that tells it to do that, but I can't find it. Ideally, I would @Override
it and execute the same code I'm executing when I right click on the JTable.
So, to sum up the question, how do I trigger some code when I right click outside the menu (or on a table cell, for example) when the menu still has focus (or is displaying)?
EDIT: After looking more carefully, I identified the problem, but not the solution. the JPopupMenu
is not the issue, but the UIManager
. Here is the code of my test form that displays the issue I'm describing:
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.UnsupportedLookAndFeelException;
public class PopupTesting extends javax.swing.JFrame {
public PopupTesting() {
initComponents();
}
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mousePressed(java.awt.event.MouseEvent evt) {
jTable1MousePressed(evt);
}
});
jScrollPane1.setViewportView(jTable1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(61, 61, 61)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(83, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(69, 69, 69)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 267, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(121, Short.MAX_VALUE))
);
pack();
}
private void jTable1MousePressed(java.awt.event.MouseEvent evt) {
if (SwingUtilities.isRightMouseButton(evt)) {
jTable1.setRowSelectionInterval(jTable1.rowAtPoint(evt.getPoint()), jTable1.rowAtPoint(evt.getPoint()));
getPopup().show(jTable1, evt.getX(), evt.getY());
}
}
public static void main(String args[]) {
try {
javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
Logger.getLogger(PopupTesting.class.getName()).log(Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(() -> {
new PopupTesting().setVisible(true);
});
}
private JPopupMenu getPopup() {
if (jTable1.getSelectedRow() > -1) {
JPopupMenu popup = new JPopupMenu();
MouseListener listener = (new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
System.out.println("I'm a menu Item.");
}
});
JMenuItem m = new JMenuItem("Regular Menu Item");
m.addMouseListener(listener);
popup.add(m);
return popup;
} return null;
}
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
}
If you remove the line:
javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
and the surrounding try
and catch
, it works flawlessly.