[英]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.