如何等待选择输入
How to wait for selection input
我有一个带有 JTree 的 JFrame(在函数 "get_Classification()" 中创建)。此函数应 return selected 节点(单击两次)的名称作为字符串。
一旦我 运行 应用程序方法 return 为 null,如果我随后双击一个节点,该值将按预期打印到控制台。
我假设该方法在用户可以 select 一个值之前完成(实际上是 3-4 个级别,大约需要 5 秒)。如果我有一个 "Thread.sleep(1000)" JTree 直到第二个过去才显示...
如何在方法 return 值之前等待用户输入并预先查看树?
下面的一些函数:
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();
}
}
感谢您的帮助
您的工作流程类似于:
调出 UI -> 调用一个方法 -> 当方法 returns 做某事时
在 GUI 应用程序中,工作流程需要有所不同:
调出 UI -> 附加侦听器以在用户执行某些操作时获得回调
-> 接到回电后做点什么
如果您仍然需要按照以前的方式,您将需要添加某种标志,该标志将在 UI 收到回调时设置,您的等待输入将休眠一小段时间并且检查标志是否设置在循环中。但是,这种方法很糟糕,因为您将从另一个线程访问 UI。
问我公司的开发负责人,他在2分钟内完成了编码...
解决方法如下:
文件"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;
}
}
文件"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();
}
}
}
感谢您的帮助
我有一个带有 JTree 的 JFrame(在函数 "get_Classification()" 中创建)。此函数应 return selected 节点(单击两次)的名称作为字符串。
一旦我 运行 应用程序方法 return 为 null,如果我随后双击一个节点,该值将按预期打印到控制台。
我假设该方法在用户可以 select 一个值之前完成(实际上是 3-4 个级别,大约需要 5 秒)。如果我有一个 "Thread.sleep(1000)" JTree 直到第二个过去才显示...
如何在方法 return 值之前等待用户输入并预先查看树?
下面的一些函数:
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();
}
}
感谢您的帮助
您的工作流程类似于: 调出 UI -> 调用一个方法 -> 当方法 returns 做某事时
在 GUI 应用程序中,工作流程需要有所不同: 调出 UI -> 附加侦听器以在用户执行某些操作时获得回调 -> 接到回电后做点什么
如果您仍然需要按照以前的方式,您将需要添加某种标志,该标志将在 UI 收到回调时设置,您的等待输入将休眠一小段时间并且检查标志是否设置在循环中。但是,这种方法很糟糕,因为您将从另一个线程访问 UI。
问我公司的开发负责人,他在2分钟内完成了编码...
解决方法如下:
文件"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;
}
}
文件"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();
}
}
}
感谢您的帮助