Skip to main content

JTreeTable: How to enable popup in the tree column?

1 reply [Last post]
siva
Offline
Joined: 2003-07-15

Hi, I would like to enable a popup in the tree column of the JTreeTable. The below code might be stupid, but I tried something like

Component c = ttable.prepareRenderer(he.getCellRenderer(0,0),0,0);
c.addMouseListener(popupListener);

This code seems to pickup the left mouse click but not the right mouse click. However, since the popup is activated by right-mouse-click, my code is not working.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mel
Offline
Joined: 2004-03-29

Hi siva,

I had the same problem, and now I find a solution. I do something like this:

...
JPopupMenu popup = new JPopupMenu();
treeTable.addMouseListener(new PopupMenuShower(popup));
...

private static class PopupMenuShower extends MouseAdapter {
private JPopupMenu popup;
public PopupMenuShower(JPopupMenu popup) {
this.popup = popup;
}

private void showIfPopupTrigger(MouseEvent mouseEvent) {
if (mouseEvent.isPopupTrigger()) {
try {
Dimension screenSize
= Toolkit.getDefaultToolkit().getScreenSize();
Dimension popupSize
= popup.getPreferredSize();
Point compLoc
= mouseEvent.getComponent().getLocationOnScreen();
popup.show
(mouseEvent.getComponent(),
Math.max
(-compLoc.x,
Math.min(mouseEvent.getX(),
screenSize.width - compLoc.x - popupSize.width)),
Math.max
(-compLoc.y,
Math.min(mouseEvent.getY(),
screenSize.height - compLoc.y - popupSize.height)));
} catch (java.awt.IllegalComponentStateException e) {
// Couldn't get screen location, component has just gone...
}
}
}

public void mousePressed(MouseEvent mouseEvent) {
showIfPopupTrigger(mouseEvent);
}

public void mouseReleased(MouseEvent mouseEvent) {
showIfPopupTrigger(mouseEvent);
}
}