Getting JTree's jlabel component

Hi everyone,

I have problems getting jtree 's jlabel component. I implemented my own TreeCellRenderer and I add every label with right-click menus. These menus are special to the label's current situation. So my problem is when I call jtree's Mouse Adapter, I need to get every cell (jlabel) to revoke right-click menu of them. I used findComponentAt(x,y) to do this but it return only the jtree.

Ex: this.jTree.findComponentAt(x,y);// returns jTree

Is there any way to get the cell as a component when mouse clicked on it?

Thanks for advance :)

[581 byte] By [Alaz4a] at [2007-11-27 10:46:07]
# 1

Renderers are not real components. You can't add a MouseListener to the label that is used as a renderer because its not a real component so it doesn't receive mouse clicks.

You can add a MouseListener the the JTree and then determine which leaf was clicked on and then display the appopriate menu.

camickra at 2007-7-28 20:17:08 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks for your attention :) I was kind of desparate :D

But I didn't add mouse listeners to every label, I only added JPopupMenus to them. I added mouse listener to jtree and in this listener, I want to get which label was clicked. Then I can call the right menu. So my problem is findComponentAt(x,y) doesn't return labels, only the tree's itself.

Here is my JTree mouse listener looks like;

public void mouseReleased(MouseEvent e) {

int x = e.getX();

int y = e.getY();

if (e.isPopupTrigger()){

TreePath path = this.displayTree.getPathForLocation(x, y);

this.displayTree.setSelectionPath(path);

MyJLabel label = (MyJLabel)this.displayTree.findComponentAt(x,y);

label.getMenu().show(this.displayTree, x, y);

}

}

Message was edited by:

Alaz4

null

Alaz4a at 2007-7-28 20:17:08 > top of Java-index,Desktop,Core GUI APIs...
# 3

> So my problem is findComponentAt(x,y) doesn't return labels, only the tree's itself.

Thats what I just said. You can't do that. There are not labels only the tree. The label is not a real component. It is just used to draw an image on the tree.

> I added mouse listner to jtree and in this listener, I want to get which label was clicked.

You don't get the label that was clicked. You get the path or node that was clicked and then use that information to display your popup.

I'm don't know much about trees but you can use something like:

tree.addMouseListener( new MouseAdapter()

{

public void mousePressed(MouseEvent e)

{

int selRow = tree.getClosestRowForLocation(e.getX(), e.getY());

System.out.println(e.getPoint());

System.out.println(e.getX());

System.out.println(selRow);

TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());

System.out.println(selPath);

}

});

camickra at 2007-7-28 20:17:08 > top of Java-index,Desktop,Core GUI APIs...
# 4

Is this situation only valid for components like JTree? or it is valid for containers like JPanel too. I mean, for example, if we add labels to jpanel then we want to find out mouse cursor is on which label, you say we cannot do that or is JTree just an exception? Are you saying, because jtree is so complex component, java developers encouraged us to use getPathForLocation(x,y) function, instead of using findCamponentAt(x,y) directly?

Alaz4a at 2007-7-28 20:17:08 > top of Java-index,Desktop,Core GUI APIs...
# 5

If you use the add(...) method to add a component to a panel then yes it is a real component.

For efficiency purposes more complex components like JTree, JTable, JList use renderers. For example if you had a table with 10 rows and 1000, columns and used a JLabel to represent each cell then you would need to create 10,000 labels. Intead when you use a renderer a single label is used to paint the contents of every cell. Much more efficient.

Read the JTree API which has a link to the Swing tutorial on "How to Use Trees" which will explain renderers in more detail.

camickra at 2007-7-28 20:17:08 > top of Java-index,Desktop,Core GUI APIs...
# 6

Thanks for millions of time :)

I am starting to change my datastructure right now.

www.virtualdataexplorer.com

Alaz4a at 2007-7-28 20:17:08 > top of Java-index,Desktop,Core GUI APIs...