简体   繁体   中英

Setting text in JLabel in a loop

                start = System.currentTimeMillis();
                FileReader filereader1 = new FileReader("virus_database/point.dat");
                JTextField jtextfield1 = new JTextField();
                jtextfield1.read(filereader1, null);
                point = Integer.parseInt(jtextfield1.getText());
                filereader1.close();
                scanFileName.setText("");
                scanFileName.setText("Please wait. Loading Database...");
                for(int i = 0; i < point && cmd != 1; i++)
                {
                    FileReader filereader4 = new FileReader((new StringBuilder()).append("virus_database/malware").append(i).append(".res").toString());
                    JTextField jtextfield4 = new JTextField();
                    jtextfield4.read(filereader4, null);
                    database[i] = jtextfield4.getText();
                    filereader4.close();
                    int k6 = (i * 100) / point;
                }
                scanFileName.setText(".");

I still don't understand why the output in scanFileName (Jlabel) is directly showing "." instead of "", "Please Wait. Loading Database...", and then ".". Please help. I'd be grateful to you.

Start by having a read through Concurrency in Swing .

Swing is a single threaded framework, this means that anything which blocks this thread (like Thread.sleep ) will prevent the framework from processing new events (including paint requests).

While there are any number of ways you might make this work, probably the easiest is a Swing javax.swing.Timer . Take a look at How to use Swing Timers for more details...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM