[英]java Change content of JTextArea in JOptionPane with Timer
结果看起来像这样的问题: 读取日志文件并将其显示在jtextarea中
但不同的是我使用JOptionPane
。
我的过程是这样的:
用户点击“更新”按钮,
然后它将显示带有JTextArea
的JOptionPane
,
我应该使用Timer
从服务器获取更新状态(就像轮询一样)。
问题是,它仅在关闭JOptionPane
之后才更改JTextArea
的内容,所以我想我不能使用JOptionPane
否则我应该更改代码以实现自己的目标。
到目前为止的代码是:
在main.java中:
static JFrame demo = new JFrame();
static JPanel myPanel=new JPanel();
static JScrollPane pane = new JScrollPane(myPanel); // Just use to have scrollbar
public static void main(String[] args) {
// TODO Auto-generated method stub
demo.setSize(560, 300);
demo.setTitle("avaControlFinder");
demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myPanel.setLayout(new GridBagLayout());
JButton updt=new JButton("Update");
updt.addActionListener(new UpdateAction());
GridBagConstraints c2 = new GridBagConstraints();
c2.gridx = 1;
c2.gridy = (listA.size()-1)*2+1;
c2.gridwidth = 1;
c2.gridheight = 1;
c2.weightx = 0;
c2.weighty = 0;
c2.fill = GridBagConstraints.NONE;
c2.anchor = GridBagConstraints.WEST;
c2.insets = new Insets(10,10,0,0); //top padding
myPanel.add(updt,c2);
myPanel.validate();
myPanel.repaint();
demo.getContentPane().add(pane);
demo.setLocationRelativeTo(null);
demo.setVisible(true);
}
在UpdateAction.java中:
JPanel plWait = new JPanel();
plWait.setLayout(new GridBagLayout());
final JTextArea ta=new JTextArea(10,20);
ta.setEditable(false); // just want to show a content of log, so I don't let user edit it.
GridBagConstraints c4 = new GridBagConstraints();
c4.gridx = 0;
c4.gridy = 0;
c4.gridwidth = 1;
c4.gridheight = 1;
c4.weightx = 0;
c4.weighty = 0;
c4.fill = GridBagConstraints.NONE;
c4.anchor = GridBagConstraints.NORTHEAST ;
plWait.add(ta,c4);
JOptionPane.showOptionDialog(null, plWait,
"title", JOptionPane.NO_OPTION,
JOptionPane.PLAIN_MESSAGE, null, new Object[] {},
null);
Timer timer= new Timer();
TimerTask showtime= new TimerTask(){
int test=0;
@Override
public void run() {
// TODO Auto-generated method stub
test++;
ta.append(""+test); // test for change content of JTextArea, I even use System.out.println(""+test); and it only be changed after I close the JOptionPane.
}
};
timer.schedule(showtime, 1000, 1000);
我应该使用其他组件代替JTextArea
吗?
还是应该使用其他容器代替JOptionPane
?
我使用Window.ShowDialog();
在C#中完美地完成了此过程Window.ShowDialog();
我应该使用另一个JFrame
作为另一个弹出窗口吗?
任何建议将被认真考虑。
JOptionPane
使用模式对话框,这意味着它将阻塞代码的执行,直到被解雇为止。
您可以做的三件事...
javax.swing.Timer
而不是java.util.Timer
。 Swing Timer
将在事件调度线程的上下文中执行其滴答通知,从而可以安全地使用它来修改UI。 摆动是单线程的,不是线程安全的。 有关更多详细信息,请参见Swing中的并发和如何使用Swing计时器 。 SwingWorker
而不是java.util.Timer
,这使您可以在EDT外部运行长时间运行的代码或可能阻塞代码,但提供了更简便的将更新同步到EDT的方法。 有关更多详细信息,请参见工作线程和SwingWorker 。 JOptionPane
... 您必须使用SwingUtilities.invokelater
在UI线程上调用此函数ta.append(""+test)
。 在您的情况下,请尝试以下操作:
TimerTask showtime= new TimerTask(){
int test=0;
@Override
public void run() {
// TODO Auto-generated method stub
test++;
SwingUtilities.invokelater(new Runnable(){
public void run(){
ta.append(""+test);
}
})
}};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.