首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何等待选择输入

如何等待选择输入
EN

Stack Overflow用户
提问于 2017-01-25 18:43:05
回答 2查看 173关注 0票数 0

我有一个带有JTree的JFrame (在函数“get_Classification()”中创建)。此函数应以字符串形式返回所选节点的名称(双击)。

只要我运行应用程序,该方法就会返回null,如果我随后双击一个节点,该值就会像预期的那样打印到控制台。

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

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

下面是一些函数:

代码语言:javascript
代码运行次数:0
运行
复制
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();
    }
}

谢谢你的帮助

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-01-25 20:52:56

问我公司的开发领导,他在2分钟内就有了代码……

以下是解决方案:

Main.java文件:

代码语言:javascript
代码运行次数:0
运行
复制
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;
    }

}

MyFrame.java文件:

代码语言:javascript
代码运行次数:0
运行
复制
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();
        }
    }

}

谢谢你的帮助

票数 0
EN

Stack Overflow用户

发布于 2017-01-25 18:59:36

您的工作流程类似于:当方法返回时调用方法->调用UI ->执行某些操作

在图形用户界面应用程序中,工作流程需要有所不同:当用户执行某些操作时,调用UI -> attach listener以获取回调;当您获得回调时,->执行某些操作

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41849471

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档