Home Showing output on JFrame pop-up while in a loop
Reply: 0

Showing output on JFrame pop-up while in a loop

user4465
1#
user4465 Published in July 22, 2018, 8:31 am

I'm a beginner programmer, and it's my first time trying out JFrames and stuff, so feel free to criticize any violations to good programming practices. I've tried many things but none have really worked so I'm stuck.

I'm trying to make 2 pop-up windows with JFrame, one which ask for button press and then opens another one which "simulates" labels (in this case just a string of text) moving systematically. I've put it in a loop and hoped it would update the text each time, simulating it moving on the window.

But the issue is it's just showing a blank screen for the duration of the loop, and only when it's complete it shows the text on the window. How can I fix it to print the text everytime on the window, not just at the end? I've put a 200 ms delay so I could spot the text moving but it's no use if it isn't even printing the text.

Here is my code:

public class PopUpWindow {

JButton button1, button2;
JLabel text1, text2;
JFrame window1, window2;

public PopUpWindow() {
    this.window1 = new JFrame("Input");
    this.window1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.window1.setLayout(null);
    this.window2 = new JFrame("Simulation");
    this.window2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.window2.setLayout(null);
}

public void createWindow1() {
    this.button1 = new JButton("Start");
    this.button2 = new JButton("Quit");

    this.button1.setBounds(50, 50, 100, 30);
    this.button2.setBounds(180, 50, 100, 30);

    this.window1.add(button1);
    this.window1.add(button2);

    this.window1.setSize(350, 200);
    this.window1.setVisible(true);
    this.window1.setResizable(true);
    this.window1.setLocationRelativeTo(null);

    analyzeInput();
}

public void analyzeInput() {
    this.button1.addActionListener((ActionEvent e) -> {
        createWindow2();
    });

    this.button2.addActionListener((ActionEvent e) -> {
        int question = JOptionPane.showConfirmDialog(this.window1, "Exit program?");
        if (question == JOptionPane.YES_OPTION) {
            this.window1.dispose();
        }
    });
}

public void createWindow2() {
    this.button1 = new JButton("Button 1");
    this.button2 = new JButton("Button 2");
    this.text1 = new JLabel("x");
    this.text2 = new JLabel("x");
    int x = 80;
    int y = 0;

    for (int i = 0; i < 10; i++) {
        this.button1.setBounds(50, 120, 100, 30);
        this.button2.setBounds(180, 120, 100, 30);
        this.text1.setBounds(x, y, 100, 100);
        this.text2.setBounds(x, (y + 30), 100, 100);

        this.window2.add(text1);
        this.window2.add(text2);

        this.window2.setSize(350, 250);
        this.window2.setResizable(true);
        this.window2.setVisible(true);
        this.window2.setLocationRelativeTo(null);

        x += 5;
        y += 10;

        try {
            Thread.sleep(200);
        } catch (InterruptedException ex) {
            Logger.getLogger(PopUpWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

EDIT: well, let's ignore the for-loop for now. What I mainly wanted to know was why the JFrame window will not update or change despite me changing its content. For example, here is an edited version of createWindow2() -method, which does not have a for-loop and instead reacts to another button.

    public void createWindow2() {
        this.window2.setSize(500, 500);
        this.window2.setVisible(true);
        this.window2.setResizable(true);
        this.window2.setLocationRelativeTo(null);

        this.text1 = new JLabel("xfdfgdf");
        this.text2 = new JLabel("xdfgdfgd");
        this.button3 = new JButton("dfsadfas");
        this.button3.setBounds(50, 50, 100, 30);
        this.window2.add(this.button3);

        this.button3.addActionListener((ActionEvent e) -> {
            this.text1.setBounds(200, 200, 100, 100);
            this.text2.setBounds(200, 230, 100, 100);

            this.window2.add(text1);
            this.window2.add(text2);
        });
    }

When I run the program, I only see a blank window, even though I press the button. Only when I rezise the window manually will the text appear in the correct coordinates. Is it supposed to work like that? Is there a way to make it update on every button press on its own? (I haven't done anything with Layouts or stuff so I don't know how that works.)

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.341768 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO