Null parent while trying to use JPopupMenu in a JTable
841470Dec 17 2011 — edited Dec 18 2011I have been trying to get a popup menu to pop when the right mouse button is clicked inside a JTable, but it keeps throwing me the "parent is null" exception and I have no idea why.
Here is the mouse code:
public void mouseClicked(MouseEvent me) {
if (SwingUtilities.isRightMouseButton(me)) {
JTable table = (JTable)me.getSource();
int tRow = table.rowAtPoint( me.getPoint() );
int tColumn = table.columnAtPoint( me.getPoint() );
if (! table.isRowSelected(tRow))
table.changeSelection(tRow, tColumn, false, false);
pop.show(table, me.getX(), me.getY());
}
}
Here is the Exception:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: parent is null
at java.awt.PopupMenu.show(PopupMenu.java:132)
at database.UserPanel.mouseClicked(UserPanel.java:409)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:253)
at java.awt.Component.processMouseEvent(Component.java:6266)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4247)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
The parent is not null, Here is the creation:
listScroll = new JScrollPane();
this.add(listScroll, new GridBagConstraints(0, 3, 4, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
listScroll.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED), "Registered Numbers", TitledBorder.LEADING, TitledBorder.DEFAULT_POSITION));
{
tableModel = new NumberCellModel();
numbers = new JTable(tableModel);
listScroll.setViewportView(numbers);
numbers.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
numbers.addMouseListener(this);
}
I have tried to use me.GetComponent() and directly calling numbers in the pop.show command they all return that exception.
I have had no issues using a popup menu when it was a JList, but because of the data I ended up using a JTable, now the popup will not work. The table displays just fine and the selection works. Thanks for the Help! - Jeremy
Edited by: kb9mfd on Dec 17, 2011 8:40 PM