Removing focus from a node

I have a frame in which a scrollpane hold a jtree.
I picked up the following code from the forum for making sure that only when the right click is on a node, the popupmenu is displayed.
public void mouseReleased(MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON3) {
                TreePath closestTreePath = tree.getClosestPathForLocation(e.getPoint(), e.getPoint());
                if (closestTreePath != null) {
                    tree.setSelectionPath(closestTreePath);
                     // select the row in the tree for the path
                    selectedRow = tree.getRowForPath(closestTreePath);
                    YourNode node = (YourNode) closestTreePath.getLastPathComponent();
                   // assuming your popup menu is already created somewhere else
                   popupMenu.show(deviceTree, pt.x, pt.y);
                    selectedRow = -1;
}by codecraig
But now the problem is that whenever i do right click anywhere on the tree a node gets selected and the popupmenu is displyed. i want to display the right click is on the node.
here is my code
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.event.*;
public class tree_right_click
     //JPopupMenu popupmenu = new JPopupMenu();
     JTree mytree;
     SysPopupListener s = new SysPopupListener();
     public tree_right_click()
          DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
          DefaultMutableTreeNode l1 = new DefaultMutableTreeNode("level1");
          DefaultMutableTreeNode l2 = new DefaultMutableTreeNode("level2");
          mytree=new JTree(root);
          mytree.addMouseListener(s);
          root.add(l1);
          l1.add(l2);
          JScrollPane treeview = new JScrollPane(mytree);
          JFrame fr = new JFrame("My Frame");
          fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          fr.getContentPane().add(treeview);
          fr.setSize(300,300);
          fr.setVisible(true);
          public static void main(String args[])
          new tree_right_click();
class SysPopupListener extends MouseAdapter
     JPopupMenu popupmenu = new JPopupMenu();
     SysPopupListener()
          JMenuItem part = new JMenuItem("Add detail 1");
          JMenuItem part2 = new JMenuItem("Add detail 2");
          part.addMouseListener(this);
          part2.addMouseListener(this);
          popupmenu.add(part);
          popupmenu.add(part2);
     public void mousePressed(MouseEvent e)
          maybeshowpopup(e);
     public void mouseReleased(MouseEvent e)
          maybeshowpopup(e);
     void maybeshowpopup(MouseEvent e)
          if(e.isPopupTrigger())
               JTree t = (JTree)e.getComponent();
               TreePath closestTreePath = t.getClosestPathForLocation(e.getX(), e.getY());
            if (closestTreePath != null) {
                t.setSelectionPath(closestTreePath);
                 // select the row in the tree for the path
                int selectedRow = t.getRowForPath(closestTreePath);
                DefaultMutableTreeNode n = (DefaultMutableTreeNode) closestTreePath.getLastPathComponent();
                System.out.print(n);
               // assuming your popup menu is already created somewhere else
                popupmenu.show(e.getComponent(),e.getX(),e.getY());
                selectedRow = -1;
}Please help,
thanks in advance,
Puneet

I made some changes to your code and seems to be working like this:
          if(e.isPopupTrigger()) {
               JTree t = (JTree)e.getComponent();
               TreePath closestTreePath = t.getClosestPathForLocation(e.getX(), e.getY());
               if (closestTreePath != null) {
                    t.setSelectionPath(closestTreePath);
                    // select the row in the tree for the path
                    int selectedRow = t.getRowForPath(closestTreePath);
                    DefaultMutableTreeNode n = (DefaultMutableTreeNode) closestTreePath.getLastPathComponent();
                    System.out.print(n);
                    // assuming your popup menu is already created somewhere else
                    popupmenu.show(e.getComponent(),e.getX(),e.getY());
                    selectedRow = -1;
               JTree t = (JTree)e.getComponent();
               TreePath path = t.getPathForLocation(e.getX(), e.getY());
               if (path != null) {
                    popupmenu.show(e.getComponent(),e.getX(),e.getY());
          }

Similar Messages

  • How to remove focus from a Ring Control in a PDA application?

    Hello,
        In a PDA application, i have observed that when we select a pull-down Ring control, the focus is retained in the control and the text of the control appears highlighted. The only way out is to tap an empty portion of the PDA application screen to remove focus from the control. Is there any way to avoid this? Is there a method to remove focus from the control programatically?
    Note: Version of LabVIEW that we are using: LabVIEW 8.5 Professional Development System for Windows Vista/XP/2000 and LabVIEW 8.5 PDA Module for Windows Mobile.
    Thanks & Regards,
    Subhashini

    Hi Vsh,
        Thanks for ur response. Setting the key focus property node of the ring control to false removed the focus from the control. I guess it was a silly question (have started LabVIEW devp. recently).
    Thanks & Regards,
    Subhashini

  • [Flex 4.5.1] How to remove focus from TextInput on mouse click outside of it?

    For me clicking outside of a TextInput should always remove focus from it, only only if you click on another TextInput or Button.
    I don't see any simple event like MouseEvent.MOUSE_CLICK_OUTSIDE - which would definitely simplify things.. I wonder why there isn't such event. Well anyway I can't find any other similar event and I can't figure out an easy way to do this. I also wonder why I couldn't find a solution to this on the web easily... Well anyway...
    Does someone know how to do that in a convenient way?
    Thanks!

    ok I understand why is that. For example I have a TextInput now where the user enters number through buttons which have mouseFocusEnabled = false, so the TextInput doesn't lose focus. But on a TabBar I had to set mouseFocusEnabled = true or when I switched between tabs -> switches between states, I could still type in the TextInput in the previous tab cause it didn't lose focus. Maybe TabBar's default value of that property is wrongly set to false.
    Anyway, not losing focus when clicking outside is still weird. Take for example this forum, if I click outside of the box I am currently writing this, I lose focus. It's how things usually work. And flex focus is designed to work backwards to what people are used to, no matter as I already pointed out I understand there are cases it comes in handly. I hope I don't sound bad but take it just as a suggestion please that maybe if it is redesigned like this: clicking on component gets focus, clicking outside loses focus. But if you click on a button for example and you want to keep the focus on a TextInput cause you add some text, you should be able to set a property on the Button like maintainCurrentFocus = true (false by default), which would make clicking on the Button not shift the focus to it or set it to null if the component is a group that has some rect background for example, but maintain the focus on the TextInput.
    I could be missing something about the current design of how the focus works in flex, but from my point of view at the moment, the design I describe to use is just like how I am usually used to be working with focus as a user, not as developer.
    Maybe you could agree or maybe you know some reason by which things are how they are at the moment that I don't see. But if you think I make sense please let me know, maybe I could fill a minor enhancement request for that ?

  • Remove focus from JButton?

    I am starting a new thread related to my previous thread.
    I came to realize that when I call a game object from the GUI menu by clicking on "New Game" button, the focus is set to the button. I found it by using the method, if(newButton.hasFocus()).
    However, my problem is not yet finished. I tried game.requestFocus(); also.
    But still, the focus is not removed.
    Can n e one pls let me know if there is a way to remove focus from the damn button?
    Thanks.                         

    button.setFocusable( false );Will pevent the button from maintaining focus.

  • Remove focus from combobox

    Hi,
    Iam working with SAP B1 2007 .
    A combobox is populated with items.After the user selects the item in it,the focus must be removed from that combobox control to next textbox control.Can anyone suggest me what can I do for this.
    Thanks In Advance

    Hi ,
    Catch the event combo select and then emulate a click on the text control
    using
    oform.Items.Item("textControl").Click
    regards
    Edy

  • Hiding Context Element without removing them from Context Node

    Hi,
    my application uses an ALV table. I'm implementing Complex Filter that are used to hide some rows that match certain conditions, like the regular filter in ALV does. At the end, I want to save all rows, even the hidden ones. It would be simpler for me to keep all elements in my context, but just to set an attribute to hide the element. Is there a way to do it or I have to remove the element from context and put it aside in a temporary buffer?
    Thanks
    Davy

    Hi ,
    Get the column reference of the alv and loop the alv columns ...get the column which you want to hide and use the method
    CALL METHOD lr_column->set_visible
            EXPORTING
              value = cl_wd_uielement=>e_visible-none.
    Regards,
    Shailaja Ainala.

  • Remove Focus from Main Frame after Jinternal Frame is opened.

    Hi,
    I want to restrict the user to change any thing on main frame once the JInternalFrame is opened (you can see same effect when we execute JOptionPane where focus on main is not retain untill you close the JOptionPane).
    I have written following code to listen the JInternalFrame event
    conn_prop.addInternalFrameListener(new InternalFrameListener(){
    @Override
    public void internalFrameActivated(InternalFrameEvent e) {
    // TODO Auto-generated method stub
    @Override
    public void internalFrameClosed(InternalFrameEvent e) {
    // TODO Auto-generated method stub
    @Override
    public void internalFrameClosing(InternalFrameEvent e) {
    // TODO Auto-generated method stub
    @Override
    public void internalFrameDeactivated(InternalFrameEvent e) {
    // TODO Auto-generated method stub
    @Override
    public void internalFrameDeiconified(InternalFrameEvent e) {
    // TODO Auto-generated method stub
    @Override
    public void internalFrameIconified(InternalFrameEvent e) {
    // TODO Auto-generated method stub
    @Override
    public void internalFrameOpened(InternalFrameEvent e) {
    // TODO Auto-generated method stub
    }}); I tried setEnabled(false) but this is paralyzing entire application
    what piece of code should i write to do that........
    thanks in advance.

    That is not how a JInternalFrame is meant to be used. Read the Swing tutorial on "Using Internal Frames".
    Use a modal JDialog.

  • StopCellEditing () removes focus from table

    Hi,
    i have created a custom cellEditor, when the user presses enter a popupwindow is shown which shows a list of possible values.
    When the popup closes i call stopCellEditing (), in a DefaultCellEditor the focus then remains on the table...but in this case the focus just disappears...
    Anybody with the same focusproblems?
    Nick

    Hi,
    i have created a custom cellEditor, when the user presses enter a popupwindow is shown which shows a list of possible values.
    When the popup closes i call stopCellEditing (), in a DefaultCellEditor the focus then remains on the table...but in this case the focus just disappears...
    Anybody with the same focusproblems?
    Nick

  • Removing attributes from node

    Hi,
    Im creating a node and its attributes dynamically and binding it to a table to show a table dynamically. The problem is that when i load a new item, i have to rebuild the node and table because it might have different columns. The node allways has 3 fixed columns (id, name, description) so first i have to remove all of the attributes except the id, name and description. Same for the table columns. Can anyone tell me how to remove attributes from a node or how to differently solve my problem? All i see is how to remove an element but is a data right, not the model?
    Much thanks,
    Hugo Hendriks

    Hi,
    I had a similar situation, where 2 table columns were always present, and the name and type of other columns were only available runtime.
    I simply deleted all data (invalidate()) and recreated the context from scratch after each model execution that impacts the table structure.
    Otherwise, use the API to find the right methods for deleting Nodes and Elements.
    https://media.sdn.sap.com/javadocs/NW04/SPS15/wd/index.html
    Good luck,
    Roelof

  • Strange problem with removing element from node

    Hi,
    I have a problem when I removed elements from a node.
    here is the code:
    String text = "123456";
    IANode nodeA= wdContext.nodeA();
    wdContext.nodeB().invalidate();
    IBNode nodeB = wdContext.nodeB();
    for(int i=0; i<nodeA.size(); i++)
    IAElement e = nodeA.getEt_Emp_RespElementAt(i);
    if (e.getID().compareTo(text)!= 0)
         nodeB.removeElement(e);
    else
    wdComponentAPI.getMessageManager().reportSuccess(e.getID() + " was not removed");
    The node A does have a row with field "ID" equals to 123456.
    When running the application, it does write that "123456" was not removed...but it removes it....In fact my table is now empty.
    How is it possible if it didn't perform the remove operation for ID = 123456 ?
    Thanks in advance.

    David,
    Because you are iterating node in wrong direction: see my reply to your post Re: Loop problem (seems that you assigns me 2 points without reading reply, and mark it as solved just because you stop solving it
    So, in example in this thread, you must iterate node B in reverse direction, get element from B and remove it from B if it has some specific ID.
    Valery Silaev
    EPAM Systems
    http://www.NetWeaverTeam.com

  • How to keep notification window from stealing focus from main AIR window

    Hi,
    We have an AIR (JavaScript based) chat application that uses the technique posted at the following link to display a notiifcation window each time the user receives a new message.  The notification window gradually fades away after 5 seconds.
    http://cookbooks.adobe.com/post_Creating_a_transparent_notification_window-8226.html
    The technique uses HTMLLoader.createRootWindow to create and display the notification window.
    The issue we are having is that the notification window steals/removes focus from the main AIR window, which is annoying if you are in the middle of typing a message that you want to send to other users.  Once the notification fades away, focus is returned to the main AIR window.
    I searched the AIR documentation to see if there is a way to keep the notification window from stealing focus, but came up empty.  I'm hoping I'm overlooking something.  Can anyone help out?
    Thanks,
    Denis

    Ahmed if i did recognize ur question then...
    Pls Follow this...
    1 - Create New Menu Item Called Window it's properties > Menu Item Type = Magic
    2- Menu Item Type = Magic.
    3- Change Magic Item = Window
    4- Command Type = Null.
    5.Visible In Menu = Yes.
    6.Under Physical 's node >Visible = No.
    Hope this helps...
    Regards,
    Amatu Allah

  • Combobox removing focus!? help!

    Why when I use the combobox component does it remove focus
    from other movieclips?? For instance...I have a button with
    onRollOver and onRollOut functions. These work fine, but once I
    click on a combobox, they break...and won't work.
    I've looked in other places but I haven't found a fix for
    this yet. Doing setfocus(this) only puts a ugly yellow highlight
    box around my button, and then kills the swf with an infinite loop.
    Any ideas?

    hi, hold your data in a Hashtable with the value as key
    and the visible text as value,use a renderer for the combobox like here
    class MyCellRenderer extends JLabel implements ListCellRenderer {
    public MyCellRenderer() {
    setOpaque(true);
    public Component getListCellRendererComponent(
    JList list,
    Object value,
    int index,
    boolean isSelected,
    boolean cellHasFocus)
    setText(value.toString());
    setBackground(isSelected ? Color.red : Color.white);
    setForeground(isSelected ? Color.white : Color.black);
    return this;
    in the method getListCellRendererComponent you get your hashtable as object parameter,get the visible text from the hashtable and call setText (..)
    you can also make a combined String with value and text
    and take only the visible text in the renderer(this solution is not so bombastic as before)

  • Combobox removing focus?

    Why when I use the combobox component does it remove focus
    from other movieclips?? For instance...I have a button with
    onRollOver and onRollOut functions. These work fine, but once I
    click on a combobox, they break...and won't work.
    I've looked in other places but I haven't found a fix for
    this yet. Doing setfocus(this) only puts a ugly yellow highlight
    box around my button, and then kills the swf with an infinite loop.
    Any ideas?

    While it depends partly on what is outside the textfield, one way would be to add an event listener to the stage and then check what was clicked.  If it happened to be the stage, set the focus onto the stage.
    stage.addEventListener(MouseEvent.MOUSE_UP, focusStage);
    function focusStage(evt:MouseEvent):void {
        if(evt.target == stage){
           stage.focus = stage;

  • How to remove the focus from a tree?

    Hello,
    how can I remove the focus from a tree, so that
    none of the elements in the tree is selected?
    I have tried using the following methods:
    setTreeSelection(null);
    setLeadSelection(IWDNode.NO_SELECTION);
    None of them worked.
    Any help highly appreciated.
    Greetings,
    Tomek.

    Hello,
    it seems I have found a (dirty) workaround to this
    problem. If anyone is interested, details below:
    When the user clicks on a tree node (say A tree),
    I do the following:
    1. request focus on the tree selected by the user
       (say B tree):
      IWDTree foldersTree = 
        (IWDTree)thisView.getElement("TreeFolders");
      foldersTree.requestFocus();
    2. invalidate the A tree (which causes the tree
       to disappear)
      wdThis.wdGetContext().nodeInbox().invalidate();
    3. recreate the A tree (filling the context nodes)
    4. unselect the A tree:
    wdContext.nodeInbox().setLeadSelection(IWDNode.NO_SELECTION);
    Thanks a lot to all of you who have responded.
    Greetings,
    Tomek.

  • Problem removing focused object from a table

    Hi.
    I have a problem removing a component that is focused from a table. The
    whole row is removed except a single component (JSpinner) in one row.
    My table has three columns, the first two are String fields
    (not editable) and the last is a JSpinner. Since there is no
    Default cell renderer or cell editor for JSpinner I had to
    create them.
    So, if no element of the row is focused then calling the function:
    public void clearTable() {
    setVisible(false);
    dataVector.removeAllElements(); // clear data vector
    setVisible(true);
    works just fine
    but if a spinner in a row is focused them that spinner is left alone "floating" on an otherwise empty table.
    Questions:
    1) Any one has an ideia of what might be the problem?
    2) Do I have to play with Focus to solve this? (hate the idea,
    since always been told to avoid messing with focus, because
    of the problems it normally brings)
    Thanks in advance for the help,
    Rgds,
    Jorge

    The problem is that you need to properly stop the editing operation first.if( myTable.isEditing() ) {
        // If you want to throw away any edits use...
        myTable.getCellEditor().cancelCellEditing();
        // If you want to apply any edits, then end use...
        myTable.getCellEditor().stopCellEditing();

Maybe you are looking for

  • Formatting text (bold, italics, etc) in webmail client does not work in Firefox; works in IE and Chrome

    I use the Smarsh webmail client for my company's email service. I have been using Firefox in the office for quite some time, and only recently has this issue come up. When composing emails using the Smarsh client, I cannot format text (using bold, it

  • Deployment Rule Set broken with Java 7u55

    Hello! I'm using Deployment Rule Set in my company environment, its signed by code signing certificate that is given out by internal CA. After I upgraded to Java 7u55, the Deployment Rule Set does not recognize older statically installed Java version

  • DOCFILE_SERVER_TO_CLIENT in ECC6

    What is alternative function module of DOCFILE_SERVER_TO_CLIENT in ECC6 ?

  • Bw to r3 data extraction problem.

    Hi all BW experts 1. For BW3.5  system my logical system name is  B3CLNT800 2. R3 4.7  Logical System name is T90CLNT090 3. I am able connect R3 system to BW from SM59 and authorization are also working fine Test connections also working fine from bo

  • 10.5.3? soon?

    after reading the replies to the question I posted about not being able to post on yahoo and several other replies in related threads, I'm going to guess there's a problem with 10.5.2. well, at least I can still get to the trading page at my online b