0
public class UserInterface {
    OpenFile of = new OpenFile();

    JFrame jf = new JFrame("File Manager");
    JButton jb1 = new JButton("Open File");
    JLabel jl1 = new JLabel("Recommendations appear here");
    JLabel jl2 = new JLabel();
    JList<String> list;

    public void build() {

        DefaultListModel<String> str = new DefaultListModel<String>();

        for (int i = 0; i < of.f.length; i++) {
            str.addElement(of.f[i].getAbsolutePath());
        }

        list = new JList<String>(str);

        Border b = BorderFactory.createLineBorder(Color.black, 2);
        Font f = new Font("Arial", Font.BOLD, 20);

        jb1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                of.OpenFileMethod(list.getSelectedValue());
            }
        });

        jl1.setFont(f);
        jl1.setBorder(b);
        list.setFont(f);
        jf.add(jl1).setBounds(30, 100, 300, 200);
        jf.add(list).setBounds(400, 100, 300, 300);
        jf.add(jb1).setBounds(250, 300, 100, 50);
        jf.setLayout(null);
        jf.setSize(800, 800);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        for (int i = 0; i < 100; i++) {
            jl1.setText("Loading.");
            jl1.setText("Loading...");
        }
    }
}

Problem in the for loop, only setting last "loading..." text to JLabel I want it to get in the loop and print it 100 times. May be the loop gets over before the launch of the swing application. any solution for this?

1 Answer 1

2

any solution for this?

their is nothing wrong here this code work perfect but the problem here is when that loop executes it's done in a blink of an eye!

your best friend for this case is the class javax.swing.Timer

and the example will show you how to use it and hopefully solve your issue , timers have their own shared Thread so you don't have to worry about it it will run without hanging your ui or blocking your code .

//this int determines the time delay for each time it executes it's actions
    private int delay = 20;
    private int times = 0;
    private String text = "Loading ";
    private Timer textTimer;
    private class TimerAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            times++;
            String theSetText = text;
            for (int i = 0; i < times; i++) {
                theSetText += ".";
            }
            if (times == 3) {
                times = 0;
            }
        }

    }

you can always add more action listeners via timer.addActioListener method and it will be looped there too.

for your issue just add the above code to your class and add replace the loop in your code with this

textTimer = new Timer (delay,new TimerAction ());
textTimer.start();

and when the time is right (as you want) when you want this to stop just call

textTimer.stop();

to stop the timer from running . here's a link to get more about the topic How to Use Swing Timers

Sign up to request clarification or add additional context in comments.

1 Comment

(1+) You can also read the section from the Swing tutorial on How to Use Swing Timers for more information. Keep a link to the tutorial handy for all Swing basics.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.