[HELP] Problem on JTree node mouse event !

I implement a JTree component in my application. Now, I want it acts like
windows XP's file explorer:
When mouse cursor hovers over some tree node, that node's text became underlined,
(font may be also changed as well) and the cursor changes to a 'hand'.
After mouse moves out, that node return to normal display status.
But I don't know how to do.
Any ideas?
Thanks a lot!

anybody out there?

Similar Messages

  • Problem with TrayIcon and mouse events

    Hi,
    I am trying to listen for a mouseEntered event on a TrayIcon.
    This is the code i am using:
    final TrayIcon icon = new TrayIcon(getImage(), "tray icon");
    final SystemTray tray = SystemTray.getSystemTray();
    try {
       tray.add(icon);
    }catch (AWTException e) {System.out.println("TrayIcon could not be added.");return;}
    icon.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseEntered(MouseEvent e){
       System.out.println("works");
    }});The icon is correctly shown in the tray bar, but the mouse entered event is not working.
    I have to say that i am using the exactly same code to achieve the same effect on a JButton and it works without any problem.
    Is there anything wrong?
    Thanks,
    Nite

    is there a way to request my thread to be moved into Swing section? Well, the idea is to post the question in the proper forum in the first place.
    yes, but i didnt find anything related to my problem.Really, I found posted examples of using a MouseListener.
    Does your code work for other events or is the problem only related to mouseEntered() events (which may not be supported, I don't know since I don't have the proper API to use TrayIcons)?
    If you need further help then you need to create a [Short, Self Contained, Compilable and Executable, Example Program (SSCCE)|http://homepage1.nifty.com/algafield/sscce.html], that demonstrates the incorrect behaviour.
    Then maybe others can test your code or see something else wrong.

  • Problem with JScrollPane and Mouse Event in JDK 1.4

    The folowing code works fine with JDK 1.3. But not with JDK 1.4. It has got a JPanel(main panel) which hosts JScrollPane which hosts another JPanel (drawing Panel). If I remove(do not add) JScrollPane to the main Panel and ad drawing panel directly to the main panel, it works.
    Thanks.
    In order to replicate the exact scenario, I have modified Sun's tutorial ScrollDemo2.java
    * This code is based on an example provided by John Vella,
    * a tutorial reader.
    import javax.swing.*;
    import javax.swing.event.MouseInputAdapter;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    public class ScrollDemo2 extends JPanel {
    private Dimension size; // indicates size taken up by graphics
    private Vector objects; // rectangular coordinates used to draw graphics
    private final Color colors[] = {
    Color.red, Color.blue, Color.green, Color.orange,
    Color.cyan, Color.magenta, Color.darkGray, Color.yellow};
    private final int color_n = colors.length;
    JPanel drawingArea;
    public ScrollDemo2() {
    setOpaque(true);
    size = new Dimension(0,0);
    objects = new Vector();
    //Set up the instructions.
    JLabel instructionsLeft = new JLabel(
    "Click left mouse button to place a circle.");
    JLabel instructionsRight = new JLabel(
    "Click right mouse button to clear drawing area.");
    JPanel instructionPanel = new JPanel(new GridLayout(0,1));
    instructionPanel.add(instructionsLeft);
    instructionPanel.add(instructionsRight);
    class MyScrollPane extends JScrollPane
    MyScrollPane(JPanel drawingArea)
    super(drawingArea);
    public void grabFocus()
    super.grabFocus();
    public void requestFocus()
    super.requestFocus();
    protected void processFocusEvent(FocusEvent e)
    if ( e.getID() == FocusEvent.FOCUS_GAINED )
    int i = 0;
    else
    if( e.getID() == FocusEvent.FOCUS_LOST )
    int i = 0;
    super.processFocusEvent(e);
    //Set up the drawing area.
    drawingArea = new JPanel() {
    protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Rectangle rect;
    for (int i = 0; i < objects.size(); i++) {
    rect = (Rectangle)objects.elementAt(i);
    g.setColor(colors[(i % color_n)]);
    g.fillOval(rect.x, rect.y, rect.width, rect.height);
    g.drawString("Hello",100,100);
    protected void processMouseEvent(MouseEvent pEvent)
    if(pEvent.getID() == pEvent.MOUSE_PRESSED)
    super.processMouseEvent(pEvent);
    else
    super.processMouseEvent(pEvent);
    drawingArea.setBackground(Color.LIGHT_GRAY);
    addMouseListener(new MyMouseListener());
    //Put the drawing area in a scroll pane.
    JScrollPane scroller = new MyScrollPane(drawingArea);
    scroller.setPreferredSize(new Dimension(200,200));
    setLayout(new BorderLayout());
    add(scroller, BorderLayout.CENTER);
    //If the above line is commented and the line bellow will be uncommented it works.
    //add(drawingArea, BorderLayout.CENTER);
    protected void processMouseEvent(MouseEvent pEvent)
    if(pEvent.getID() == pEvent.MOUSE_PRESSED)
    super.processMouseEvent(pEvent);
    else
    super.processMouseEvent(pEvent);
    class MyMouseListener extends MouseInputAdapter {
    final int W = 100;
    final int H = 100;
    public void mouseReleased(MouseEvent e) {
    boolean changed = false;
    if (SwingUtilities.isRightMouseButton(e)) {
    // This will clear the graphic objects.
    objects.removeAllElements();
    size.width=0;
    size.height=0;
    changed = true;
    } else {
    int x = e.getX() - W/2;
    int y = e.getY() - H/2;
    if (x < 0) x = 0;
    if (y < 0) y = 0;
    Rectangle rect = new Rectangle(x, y, W, H);
    objects.addElement(rect);
    drawingArea.scrollRectToVisible(rect);
    int this_width = (x + W + 2);
    if (this_width > size.width)
    {size.width = this_width; changed=true;}
    int this_height = (y + H + 2);
    if (this_height > size.height)
    {size.height = this_height; changed=true;}
    if (changed) {
    //Update client's preferred size because
    //the area taken up by the graphics has
    //gotten larger or smaller (if cleared).
    drawingArea.setPreferredSize(size);
    //Let the scroll pane know to update itself
    //and its scrollbars.
    drawingArea.revalidate();
    drawingArea.repaint();
    public static void main (String args[]) {
    JFrame frame = new JFrame("ScrollDemo2");
    frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {System.exit(0);}
    frame.setContentPane(new ScrollDemo2());
    frame.pack();
    frame.setVisible(true);

    I tried it . It didn't work.
    Thanks for the suggestionI've got it... I know that inside the paitComponet method you can't call setSize() in jdk1.4, but you could in previous versions... that has caused al lot of problems to me...
    Abraham

  • How to post an Event (mouse left click) to JTree node

    Hi,
    I am using JTree for my application just like the windows explorer tree. By default, Jtree node will got focus using left click. But, I'd like that my tree will detect a right click so that I can copy any file to the Jtree. I've added a mouselistener to it but the node is not selected by the right click. Anyboby can teach me how to post a mouse left click event to the Jtree so that it will become selected using left click.
    Thanks a million

    See if this is useful
    public void mouseClicked(MouseEvent event)
    Object object = event.getSource();
    if (object == mTree)
    mTree_mouseClicked(event);
    private void mTree_mouseClicked(MouseEvent event)
    if(event.getModifiers()==event.BUTTON3_MASK)
    //your logic goes here
    }//close method

  • Hi..............I Stuk up with a problem in JTree Please, help me any one..

    Hi,
    I created a JTree where the DefaultMutableTreeNode is created at run time and for this node, the leaf items are added dynamically.....Now my problem is, I should get a popup menu when i click on the treenode particularly-----
    * Node1
    -------Node11
    -------Child1
    In this I want a popup only for the Node11 & its child elements if there is any Node2 also i don't want a popup and also for Node1 i don't want, when I am trying this I am getting the Popup to all nodes and child elements, please, help me any one who cross over this problem.............
    Thanks In Advance,
    Regards,
    Sarikakiran

    Firstly, do you know how to detect the right click? (Use a MouseListener.)
    When you receive the MouseEvent for the right-click, you can get the coordinates of the click (i.e. where on the JTree the mouse was clicked).
    You can then use JTree.getPathForLocation to get the path for the clicked node, which is a TreePath object. Use TreePath.getLastPathComponent to find which node was actually clicked on.
    Having determined which node was clicked, you can then make a decision about whether to show a popup menu.

  • PopupMenu on Jtree nodes Please [b]Help me][/b]

    how can I set my JpopupMenu to work on Jtree nodes and not on the tree?

    how can I set my JpopupMenu to work on Jtree nodes
    and not on the tree?Could you elaborate? Are you asking that popup events are ignored if the mouse click is not actually on the rendered area of a node or are you asking something else?

  • Mouse event problem (NEW problem)

    Sorry guys...
    The last problem was solved, yes... but another one has shown
    up!
    Remember the code:
    txt82.visible = false;
    botao82.addEventListener(MouseEvent.MOUSE_OVER, botao82over);
    botao82.addEventListener(MouseEvent.MOUSE_OUT, botao82out);
    botao82.addEventListener(MouseEvent.CLICK, botao82click);
    function botao82over(event: MouseEvent)
    txt82.visible = true;
    function botao82out(event: MouseEvent)
    txt82.visible = false;
    function botao82click(event: MouseEvent)
    gotoAndPlay("video82");
    where i wanted a texto to show or hide depending on the mouse
    event?
    Ok...
    Now, when i CLICK, i'll go over to "video82" allright, but
    i'll ALSO get an error!
    "Cannot access a property or method of a null object
    reference" - on botao82out
    which means that: when i click and move up to "video82", the
    mouseout is also called to make the text dissapear but the function
    is no more available because we've moved up to another time.
    Any possibilities?
    Erik.

    Hi Haran,
    bota82 IS the movieclip, which has to be like that because of
    the naming of it's instance, used to get called in the code.
    But anyway, guys, i found the solution! YEP! Increases
    tremendously the code, but... well... it works!
    i had the button layer extended all over the time.
    The problem with this is that when i clicked and went to the
    frame video82, for example, all the buttons still showed as
    clickable.
    How did i solve that?
    More magic! Disappearing magic!
    at the beginning of the code i certify the buttons to be
    visible:
    botao82.visible = true;
    and so on with all of them...
    then, right before the click function, i turned them all OFF
    ! :D
    function botao82click(event: MouseEvent)
    botao82.visible = false;
    botao83.visible = false;
    botao84.visible = false;
    botao85.visible = false;
    botao86.visible = false;
    botao87.visible = false;
    botao88.visible = false;
    botao89.visible = false;
    botao90.visible = false;
    gotoAndPlay("video82");
    Phew! It worked!
    Know what? I think clearer when i discuss the problem with my
    fellows here in the forum! Thanks everyone.
    Hope this helps others in the future!
    Erik.

  • Block input events after selecting JTree node

    Hi,
    I've got a common problem but didn't found any solution for it.
    I try to block input events (key and mouse events) after the user clicks on a tree node in the TreeSelectionListener.
    For normal clicks it works fine.
    If the user make a double click than the blocking mechanism also blocks the mouse events to handle the additional expansion of the tree node.
    Any solutions for this?
    Thanks,
    Steffen

    I use the following code to blocking input events:
          waitTimer = new Timer(350, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              Toolkit.getDefaultToolkit().addAWTEventListener(awtEventListener,
                  AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_WHEEL_EVENT_MASK | AWTEvent.KEY_EVENT_MASK);
          waitTimer.setRepeats(false);
          waitTimer.start();The event listener looks like:
        awtEventListener = new AWTEventListener() {
          public void eventDispatched(AWTEvent anEvent) {
            if(anEvent instanceof InputEvent && anEvent.getSource() instanceof Component)  {
                ((InputEvent) anEvent).consume();
        };

  • Disable JTree and stop it from receiving mouse events

    Hi,
    I have a JTree which has a mouse listener associated with (which is used for right-click operations)..,..anyway I am trying to disable the tree, so I call setEnabled(false)...which, as documented, disables the tree but it will still receive mouse events. I also tried, setEditable(false)..... the only way I can think of is to remove the mouse listener, and then when I want to re-enable the tree, I have to add it back.
    is there some other way?
    thanks

    Hi,
    I have a JTree which has a mouse listener
    ner associated with (which is used for right-click
    operations)..,..anyway I am trying to disable the
    tree, so I call setEnabled(false)...which, as
    documented, disables the tree but it will still
    receive mouse events. I also tried,
    setEditable(false)..... the only way I can think of
    is to remove the mouse listener, and then when I want
    to re-enable the tree, I have to add it back.
    is there some other way?Why do you want another way? This seems like a perfectly viable way to accomplish what you want.

  • Please help with JTree Node Duplication

    Iam building a JTree which should not allow node duplication.
    1.)First i construct a ArrayList which stores all the previous nodes.
    treeNodeNames=new ArrayList();
    2.)i call the method
              readTreeNodeNames((DefaultMutableTreeNode)dtm.getRoot());
    3.)The method readTreeNodeNames is
    public void readTreeNodeNames(DefaultMutableTreeNode node) {
              if(node.toString().toLowerCase().length()!=0) {
              treeNodeNames.add(node.toString().toLowerCase());
              for(Enumeration en=node.children();en.hasMoreElements();) {
                   DefaultMutableTreeNode childNode=(DefaultMutableTreeNode)en.nextElement();      
                   if(childNode.toString().toLowerCase().length()!=0) {
                        treeNodeNames.add(childNode.toString().toLowerCase());
                   if(!childNode.isLeaf()) {
                        readTreeNodeNames(childNode);
    4.)i have four menu when right click a node add,modify,add subnode,delete.
    node duplication fails in my case.when should i call this method?should i call while doing every above mentioned operations or in TreeModelEvent implementations?
    i have mailed this problem already two times.but no one has not replied.Kinldy consider it and give me a solution.Or anyone having code for JTree Node duplication please send me to [email protected]

    Hi all,
    Any ideas to overcome this problem.?
    Thanks,
    Krish

  • Getting mouse events outside of nodes

    Does anyone know how to get a mouse event to trigger outside of a nodes clip area? I am trying to write a game and follow the model/viewer/controller outline. I want the controller to handle the mouse events and the controller have no substance on the screen. Mouse events only seen to work when they are inside a node. My current fix is to make the controller appear on the screen as a giant rectangle of the background color. However if there is a better way I would appreciate it.

    My current fix is to make the controller appear on the screen as a giant rectangle of the background color.That's indeed the classical and recommended way. Or make it transparent. Can be in the background (let the nodes handle the clicks (they can block mouse or not), get all "lost" clicks) or in the foreground (get all clicks, letting them pass through to be handled also by nodes).

  • How to do SelectAll() when during JTree node edit?

    This question's been asked a few times but the answers haven't been very clear ( or the code that was posted was broken, etc. )
    I have a JTree -- when the user edits a node, I want to initially select all the text in the .that node.
    Does anyone have a SIMPLE, COMPLETE extension of a DefaultTreeCellEditor which does this? If so, please post. -- No fancy bells or whistles.
    Thanks much.

    ...well, there is a very easy, but not clean way to do it:
             * Configures the editor.  Passed onto the <code>realEditor</code>.
            public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) {
                DefaultTreeCellEditor.EditorContainer comp= (EditorContainer) super.getTreeCellEditorComponent(tree, value, isSelected, expanded, leaf, row);
                JTextField textField= (JTextField) comp.getComponent(0);
                textField.setSelectionStart(0);
                textField.setSelectionEnd(textField.getText().length());
                return comp;
            }This will work if you click the node twice and wait to call the editor, however most users will start editing a TreeNode by clicking multiplely on the node - as the Editor is a normal TextField, it will lose it's selection when being clicked upon. If this is a problem for you, just overwrite DefaultTreeCellEditor.DefaultTextField's processMouseEvent(MouseEvent) method and decide whether the click should remove the selection or not.
    I guess if you ask for a piece of code which will keep the text selected you have something special in mind to do with the editor, so sample code wouldn't do you much good here. Get back to me if you have trouble intercepting the mouse event.

  • Capture mouse events

    Hi all,
    i want to capture mouse events like mousedown, mouseup and mousemove by clicking on the tree node of <htmlb:tree>.
    Have any one some Idee?
    Greeting,
    ag

    Hi all,  
    i solved with your help the problem as follows:
    thank you all.
    PS: how can i Reward and close this topic?
    <%data l_replace type string.%>
    <%concatenate 'onSubmit(' '''' 'OnInputProcessing(this.id)' '''' ',this.id' ')' into l_replace.%>
    <bsp:findAndReplace find = 'onclick="' replace = 'onMouseDown="<%=l_replace%>;' >
    <bsp:findAndReplace find1 = "<td" replace1 = "<td bgcolor=green" >
          <htmlb:tree id     = "HDrag"
                      table  = "<%=p_0costtree %>"
                      title  = "Tree Left"
                      width  = "100%"
                      height = "100%"
                      onTreeClick = "TreeClicked"/>
        </bsp:findAndReplace>
        </bsp:findAndReplace>

  • Mouse events don't work on a button from a panel ran in a DLL

    Hi.
    I have a DLL that loads a panel.
    Since it's a DLL I can't do the RunUserInterface() function, because the DLL would be stuck waiting for the panel events.
    I only do the LoadPanel().
    When I click with the mouse on one of the buttons, the pushing down event (simulating the button being pressed) doesn't happen and the callback doesn't run.
    But if I press the tab button until the focus reaches the button and press Enter, the callback of the button is executed.
    An even more interesting aspect is that this happens when I call the DLL on TestStand, but on an application of mine that calls the DLL just for debug everything works fine.
    How can I enable the mouse events?
    Thanks for your help.
    Daniel Coelho
    VISToolkit - http://www.vistoolkit.com - Your Real Virtual Instrument Solution
    Controlar - Electronica Industrial e Sistemas, Lda
    Solved!
    Go to Solution.

    Hello Daniel,
    I got surprised when I read your post, because I am experiencing something almost the same as you do.
    I have a DLL (generated by an .fp instrument driver I wrote) which performs continious TCP communication with the UUT.
    It starts 2 threads, each for reading from one of the 2 different IPs the UUT has.
    Another DLL tests this UUT (with the help of the communication DLL above) by exporting functions to be called by TestStand steps.
    If the second DLL wants to display a CVI panel in order to wait for the user response or displays a popup (MessagePopup, ConfirmPopup, etc), user cannot press the buttons.
    Actually, there is a point your program and mine differ. I call RunUserInterface and the button's callbacks call QuitUserInterface, so the execution continues after a button is pressed. The problem is buttons cannot be pressed. I also tried looping on GetUserEvent  but it did not solve the problem.
    There are 2 findings:
    1. I had a small exe to test my instrument driver DLL and the popups in it worked pretty well (this coincides with Daniel's findings).
    2. We workedaround the problem by shutting down the communication threads in the instrument driver DLL before the popup appears and restrating them afterwards. Although they are separate threads in another DLL, they were somehow blocking user events on another DLL running.
    S. Eren BALCI
    www.aselsan.com.tr

  • Selection problem in JTree

    Hi,
    I am setting the selection mode for a JTree to be single selection. Suppose the node which I selected was in expanded state, after making the selection it is in collapsed state. Now suppose I want to resume the expanded state and again try to select the same node without making any intermediate selection the listener doesn't listen. But if I make make a different node selection in between the 2 consecutive selections made to the same node the 2nd selection is listened.
    Is it something like if two similar selections are made one after the other the second one is discarded or not listened. If so how can I overcome this? If not please tell me where am I making the mistake.

    selection will be fired only when the selection property changed. so, collapsing and expanding should be done in mouse event handlers.

Maybe you are looking for