Cannot add popup to JTree

Have implemented JTree treePanel using DefaultTreeModel.

Created popupMenu.

Created JTextArea output.

In the end of createPopupMenu() method add following:

treePanel.addMouseListener(popupListener);

output.addMouseListener(popupListener);

Result: Popup added to JTextArea and didn't add to JTree.

(When I right mouse click on JTextArea, popup appears and when on JTree -- not)

What is the problem? What is difference between JTextArea and JTree?

[501 byte] By [BrotherFlamea] at [2007-11-27 11:25:10]
# 1

We're going to need to see more of your code so we can see exactly where the problem lies.

c0demonk3ya at 2007-7-29 16:02:50 > top of Java-index,Desktop,Core GUI APIs...
# 2

Ok, the problem is following:

class PopupListener extends MouseAdapter {

JPopupMenu popup;

PopupListener (JPopupMenu popupMenu) {

popup = popupMenu;

}

public void mousePressed(MouseEvent e) {

maybeShowPopup(e);

}

public void mouseReleased(MouseEvent e) {

maybeShowPopup(e);

}

public void maybeShowPopup(MouseEvent e) {

if (e.isPopupTregger()) {

popup.show(e.getComponent(), e.getX(), e.getY());

}

}

}

In GUImain class:

public Container createContentPane() {

...

tree = new MyTree();

contentPane.add(tree, BorderLayout.PAGE_START);

output = new JTextArea(5, 30);

contentPane.add(output, BorderLayout.PAGE_END);

...

}

public void createPopupMenu() {

JPopupMenu popup = new JPopupMenu();

// Create menu items

...

MouseListener popupListener = new PopupListener(popup);

output.addMouseListener(popupListener);

tree.addMouseListener(popupListener);

}

Popup menu appear on right click on output.

tree doesn't react on right click.

How to bind popup menu to JTree?

BrotherFlamea at 2007-7-29 16:02:50 > top of Java-index,Desktop,Core GUI APIs...
# 3

Argh! Unformatted code!! Bad Developer!!!

Please repost using the code formatting tags so your code retains ity's originall formatting

c0demonk3ya at 2007-7-29 16:02:50 > top of Java-index,Desktop,Core GUI APIs...
# 4

> Please repost using the code formatting tags so your

> code retains ity's originall formatting

class PopupListener extends MouseAdapter {

JPopupMenu popup;

PopupListener (JPopupMenu popupMenu) {

popup = popupMenu;

}

public void mousePressed(MouseEvent e) {

maybeShowPopup(e);

}

public void mouseReleased(MouseEvent e) {

maybeShowPopup(e);

}

public void maybeShowPopup(MouseEvent e) {

if (e.isPopupTregger()) {

popup.show(e.getComponent(), e.getX(), e.getY());

}

}

}

In GUImain class:

public Container createContentPane() {

...

tree = new MyTree();

contentPane.add(tree, BorderLayout.PAGE_START);

output = new JTextArea(5, 30);

contentPane.add(output, BorderLayout.PAGE_END);

...

}

public void createPopupMenu() {

JPopupMenu popup = new JPopupMenu();

// Create menu items

...

MouseListener popupListener = new PopupListener(popup);

output.addMouseListener(popupListener);

tree.addMouseListener(popupListener);

}

javakicksassa at 2007-7-29 16:02:50 > top of Java-index,Desktop,Core GUI APIs...