【发布时间】:2015-07-03 12:59:07
【问题描述】:
我有一个标签在同一个 GUI 实例中没有更新。 如果我单击应该更新 jLabel 上的值的 jButton(代码块中的“testLabel”),我必须再次运行 java 程序才能看到更改出现。我怎样才能让它出现在同一个实例中的按钮点击上? 我知道invokelater,我一直在尝试让它实时更新,但没有运气。我已经坚持了一段时间了,所以感谢您的帮助。 使用下面列出的代码块,我仍然需要运行一个新的 GUI 实例来获取要更新的值。
相关代码:
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MISControlPanel window = new MISControlPanel();
window.frame.setVisible(true);
// testLabel.setText(CN);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
JButton searchComputerButton = new JButton("Search");
searchComputerButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String line;
BufferedWriter bw = null;
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(tempFile));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// String lineToRemove = "OU=Workstations";
String s = null;
Process p = null;
/*
* try { // p = Runtime.getRuntime().exec(
* "cmd /c start c:\\computerQuery.bat computerName"); } catch
* (IOException e1) { // TODO Auto-generated catch block
* e1.printStackTrace(); }
*/
try {
p = Runtime.getRuntime().exec("c:\\computerQuery.bat");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
StringBuffer sbuffer = new StringBuffer();
BufferedReader in = new BufferedReader(new InputStreamReader(p
.getInputStream()));
try {
while ((line = in.readLine()) != null) {
System.out.println(line);
// textArea.append(line);
String dn = "CN=FDCD111304,OU=Workstations,OU=SIM,OU=Accounts,DC=FL,DC=NET";
LdapName ldapName = new LdapName(dn);
String commonName = (String) ldapName.getRdn(
ldapName.size() - 1).getValue();
}
ComputerQuery.sendParam();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InvalidNameException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} finally
{
try {
fw.close();
}
catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
try {
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ComputerQuery.sendParam();
}
});
try (BufferedReader br = new BufferedReader(new FileReader(
"resultofbatch.txt"))) {
final Pattern PATTERN = Pattern.compile("CN=([^,]+).*");
try {
while ((sCurrentLine = br.readLine()) != null) {
String[] tokens = PATTERN.split(","); // This will return
// you a array,
// containing the
// string array
// splitted by what
// you write inside
// it.
// should be in your case the split, since they are
// seperated by ","
// System.out.println(sCurrentLine);
CN = sCurrentLine.split("CN=", -1)[1].split(",", -1)[0];
System.out.println(CN);
testLabel.setText(CN);
}
全类代码 http://pastebin.com/havyqMxP
计算机查询类(小班) http://pastebin.com/Q89BCjya
【问题讨论】:
-
您好 user6680...我不知道您打算在执行流程的哪个部分执行此操作,我看到您正在使用 LDAP。这意味着您正在连接到 LDAP服务器并可能执行一个长时间运行的任务...您是否尝试在任何类型的组件侦听器的回调中执行此操作(因此在事件调度线程中)?或者您正在另一个线程上执行此操作? Swing.invokelater 对此无济于事......我建议开始阅读 SwingWorker 并在 UI 上划分表示的操作(处理摆动组件的代码)
-
我认为我需要它在不同的线程上运行,以便值将在 GUI 的同一实例中更新。我查看了您提供的教程链接,但我很困惑。它使用了在 done 方法中加载图像的示例,但我不确定如何将其实现到我的代码中。我一直在努力让它工作一个多星期。您是否有机会向我展示我正在努力完成的工作示例?
-
我现在正在工作.. 但是当我有时间时,我会尝试详细说明一些代码以帮助您... 我的意思是,一个非常基本的窗口示例,在后台执行请求(通过 Web 获取一些数据.. 应该类似于通过 LDAP 服务器执行查询的操作)并更新组件以反映状态.....所有这些都使用SwingWorker 类。
标签: java swing concurrency swingworker