如何等待选择输入

时间:2017-01-25 10:43:05

标签: java swing jtree

我有一个带有JTree的JFrame(在函数" get_Classification()"中创建)。此函数应将所选节点的名称(单击双精度)返回为String。

一旦我运行应用程序,该方法返回null,如果我然后双击一个节点,该值将按预期打印到控制台。

我认为方法在用户可以选择一个值之前完成(实际上是3-4级,大约需要5秒)。如果我有一个" Thread.sleep(1000)" JTree直到第二次通过才显示......

如何在方法返回值之前等待用户输入并预先看到树?

以下一些功能:

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();
    }
}

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

您的工作流程如下: 打造UI - &gt;调用方法 - &gt;当方法返回时执行某些操作

在GUI应用程序中,工作流程需要不同: 打造UI - &gt;当用户执行某些操作时,附加侦听器以获取回调 - &GT;当你回电话时做点什么

如果你仍然需要按照以前的方式,你需要添加某种标志,当UI收到回叫时,标志将被设置,你的等待输入将睡眠很短的时间并检查标志是否设置在循环中。但是,这种方法很糟糕,因为您将从另一个线程访问UI。

答案 1 :(得分:0)

询问我公司的开发负责人,他在2分钟内完成了编码......

以下是解决方案:

文件&#34; Main.java&#34;:

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;
    }

}

文件&#34; MyFrame.java&#34;:

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();
        }
    }

}

感谢您的帮助