在迭代或循环内部更改JTextField的文本 [英] Changing JTextField's text while iterating or inside a loop

查看:215
本文介绍了在迭代或循环内部更改JTextField的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个JTextFieldstatus并且我正在运行此代码:

Let's say I have a JTextField "status" and I'm running this code:

status = new JTextField(50);
add(status);

for (int i=0; i<10000; i++) {
     status.setText("bla bla - "+ i);
     System.out.println("bla bla - "+ i);
}

我的问题是现在循环运行时JTextField中没有任何事情发生文本,并且仅当循环结束标签是bla bla - 10000。

My problem is that right now while the loop is running nothing happened in the JTextField's text and only when the loop end the label is "bla bla - 10000".

我想制作类似状态栏但不能更新此状态栏在线 。
我也尝试在一个线程中进行更新,但结果却相同。

I want to make something like a status bar but cant update this status bar "online". I also tried to do the update in a thread but ended with the same result.

有人能告诉我如何在GUI中显示文本迭代还是循环?

Can someone show my how I can present a text in a GUI while iterating or looping?

推荐答案

使用 SwingWorker 分割UI更新和长时间运行的任务。

Use a SwingWorker to split UI-update and long running tasks.

花几分钟时间阅读 Swing标签维基的结尾并按照提供的链接。

Take a few minutes to read the end of the Swing tag wiki and follow the provided links.

以下是此类代码的一个小例子:

Here is a small example of such code:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

public class TestSwingWorker {

    private JTextField progressTextField;

    protected void initUI() {
        final JFrame frame = new JFrame();
        frame.setTitle(TestSwingWorker.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton button = new JButton("Clik me to start work");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                doWork();
            }
        });
        progressTextField = new JTextField(25);
        progressTextField.setEditable(false);
        frame.add(progressTextField, BorderLayout.NORTH);
        frame.add(button, BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
    }

    protected void doWork() {
        SwingWorker<Void, Integer> worker = new SwingWorker<Void, Integer>() {
            @Override
            protected Void doInBackground() throws Exception {
                for (int i = 0; i < 100; i++) {
                    // Simulates work
                    Thread.sleep(10);
                    publish(i);
                }
                return null;
            }

            @Override
            protected void process(List<Integer> chunks) {
                progressTextField.setText(chunks.get(chunks.size() - 1).toString());
            }

            @Override
            protected void done() {
                progressTextField.setText("Done");
            }
        };
        worker.execute();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestSwingWorker().initUI();
            }
        });
    }
}

这篇关于在迭代或循环内部更改JTextField的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆