Search code examples
javaswingjtree

How to wait for selection input


I have a JFrame with a JTree on it (created within the function "get_Classification()"). This function should return the name of the selected Node (clicked double) as String.

As soon as i run the application the method returns null, if I then double click on a node the value is printed to console as expected.

I assume the method is finished before the user can select an value (actually 3-4 levels, taking about 5 seconds). If I had a "Thread.sleep(1000)" the JTree is not showing until the second passed...

How can I wait for the user input before the method returns the value and also see the tree beforehand?

Some functions below:

public String ret = null;
private DefaultTreeModel model = new DefaultTreeModel(top);
private JTree baum = null;

MouseListener ml = new MouseAdapter() {
    public void mousePressed(MouseEvent e) {
        TreePath selPath = baum.getPathForLocation(e.getX(), e.getY());
        if (selPath != null) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) selPath.getLastPathComponent();
            if (e.getClickCount() == 2 && model.isLeaf(node)) {
                ret = node.toString();
                System.out.println(ret);
            }
        }
    }
};

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Frame window = new Frame();
                window.frame.setVisible(true);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public Frame() {
    initialize();
}


private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 500, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    Map<String, String> tree = new HashMap<String, String>();
    tree.put("Klebebänder", "Hilfsstoffe und Beschichtungsstoffe");
    tree.put("Lacke", "Hilfsstoffe und Beschichtungsstoffe");
    tree.put("Pulver", "Hilfsstoffe und Beschichtungsstoffe");

    String k = get_Klassifizierung(tree);
    System.out.println(k);

}

private String get_Klassifizierung(Map<String, String> tree) {
    setupTree(tree); // creates the tree
    waitForInput();

    return ret;

}

private void waitForInput() {
    try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

Thank you for your help


Solution

  • Asked the development leader in my company and he had the coding within 2 minutes...

    Here is the solution:

    File "Main.java":

    public class Main {
    
    public static void main(String[] args) throws InterruptedException {
    
        Map<String, String> tree = new HashMap<String, String>();
        tree.put("Klebebänder", "Hilfsstoffe und Beschichtungsstoffe");
        tree.put("Lacke", "Hilfsstoffe und Beschichtungsstoffe");
        tree.put("Pulver", "Hilfsstoffe und Beschichtungsstoffe");
    
        String k = get_Klassifizierung(tree);
    
        System.out.println(k);
    
        }
    
    public static String get_Klassifizierung(Map<String, String> tree) throws InterruptedException {
        MyFrame m = new MyFrame(tree);
        while (m.myReturnValue == null) {
            Thread.sleep(1000);
        }
    
        m.dispose();
        return m.myReturnValue;
        }
    
    }
    

    File "MyFrame.java":

    public class MyFrame extends JFrame implements MouseListener {
    private static final long serialVersionUID = 1L;
    String myReturnValue = null;
    
    private static String firstn = "Kategorien";
    private static String del = "\\|";
    private DefaultMutableTreeNode top = new DefaultMutableTreeNode(firstn);
    private DefaultTreeModel model = new DefaultTreeModel(top);
    private JTree baum = null;
    public String ret = null;
    
    public MyFrame(Map<String, String> tree) {
    
        this.setBounds(100, 100, 500, 500);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        setupTree(tree);
    
        baum.addMouseListener(this);
        this.setVisible(true);
    }
    
    private void setupTree(Map<String, String> tree) {
        baum = new JTree(createNodes(tree));
        baum.setEditable(false);
        baum.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        baum.setToggleClickCount(2);
        baum.setRootVisible(true);
        JScrollPane treeView = new JScrollPane(baum);
        this.getContentPane().add(treeView);
        this.setVisible(true);
    }
    private DefaultMutableTreeNode createNodes(Map<String, String> file) {
        /*
         * Iterate over the Map
         */
        for (Map.Entry<String, String> entry : file.entrySet()) {
            String key = entry.getKey();
            String values[] = entry.getValue().split(del);
    
            DefaultMutableTreeNode parent = top;
    
            /*
             * Iterate over the values (Path)
             */
            for (String k : values) {
                DefaultMutableTreeNode n = null;
    
                /*
                 * Check if Node already exists
                 */
                Enumeration<?> e = parent.children();
                while (e.hasMoreElements()) {
                    n = (DefaultMutableTreeNode) e.nextElement();
                    if (k.equals(n.getUserObject())) {
                        // Existing node matches; use that one.
                        break;
                    }
                    n = null;
                }
                if (n == null) {
                    // No existing node matches; add it.
                    n = new DefaultMutableTreeNode(k);
                    parent.add(n);
                }
                parent = n;
            }
            parent.add(new DefaultMutableTreeNode(key));
        }
        return top;
    }
    // ... other @Overrides
    @Override
        public void mousePressed(MouseEvent e) {
        TreePath selPath = baum.getPathForLocation(e.getX(), e.getY());
        if (selPath != null) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) selPath.getLastPathComponent();
            if (e.getClickCount() == 2 && model.isLeaf(node)) {
                this.myReturnValue = node.toString();
            }
        }
    
    }
    

    Thank you for your help