0

i'm making a tiny program where you have 3 timers and when you press start it gives a progress bar until time is over, but JFrame decided to do this after I pressed start: enter image description here

Here is my code:

package com.company;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;




public class Main {
    static int getTime(JFrame frame){
        String ts = JOptionPane.showInputDialog(frame, "Set time: ", "time", 1);
        return Integer.parseInt(ts);

    }

    public static class globalVars{
        public static int time1 = 5;
        public static int time2 = 5;
        public static int time3 = 5;
    }
    static void startTimer(JFrame frame) {

        int time = globalVars.time1+globalVars.time2+globalVars.time3;
        int timeF = Math.toIntExact(System.currentTimeMillis() / 1000l);
        JProgressBar bar = new JProgressBar(0, time);
        frame.add(bar);
        frame.pack();

        int i=0;
        while (System.currentTimeMillis()!=timeF+time* 60L){
            bar.setValue((timeF+time*60-Math.toIntExact(System.currentTimeMillis() / 1000l))/60);
        }

    }


    public static void main(String[] args) {
        JFrame frame = new JFrame("Timer");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());


        //-----------------------------------------------------------//
        Icon icon1 = new ImageIcon("placeholder.png");
        String button1Text = "Button1, ";
        JButton button1 = new JButton(String.format("%s%dmin", button1Text, globalVars.time1));
        button1.setIcon(icon1);
        button1.addActionListener(new ActionListener() {
                                      public void actionPerformed(ActionEvent e) {
                                        globalVars.time1=getTime(frame);
                                          button1.setText(String.format("%s%dmin", button1Text, globalVars.time1));
                                      }
                                  });

        frame.add(button1);
        //-----------------------------------------------------------//
        Icon icon2 = new ImageIcon("placeholder.png");
        String button2Text = "Button2, ";
        JButton button2 = new JButton(String.format("%s%dmin", button2Text, globalVars.time2));
        button2.setIcon(icon2);
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                globalVars.time2=getTime(frame);
                button2.setText(String.format("%s%dmin", button2Text, globalVars.time2));
            }
        });

        frame.add(button2);
        //-----------------------------------------------------------//
        Icon icon3 = new ImageIcon("placeholder.png");
        String button3Text = "Button3, ";
        JButton button3 = new JButton(String.format("%s%dmin", button3Text, globalVars.time3));
        button3.setIcon(icon3);
        button3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                globalVars.time3=getTime(frame);
                button3.setText(String.format("%s%dmin", button3Text, globalVars.time3));
            }
        });

        frame.add(button3);
        //-----------------------------------------------------------//
        JButton buttonStart = new JButton("Start");
        buttonStart.addActionListener(new ActionListener() {
                                          public void actionPerformed(ActionEvent e) {
                                              buttonStart.removeActionListener(this);
                                             startTimer(frame);
                                          }
                                      });
        frame.add(buttonStart);
        //-----------------------------------------------------------//

        frame.pack();
        frame.setSize(400, 200);
        frame.setVisible(true);


    }
}

I know it is a very inefficient way of doing things and I will improve it but now I'm more worried about the black screen. Tnx!

camickr
  • 321,443
  • 19
  • 166
  • 288
JasperDG
  • 137
  • 1
  • 3
  • 7
  • 2
    Don't write the entire application before you start testing. 1) Display the frame with just the buttons. 2) Then click on one of the buttons to see what happens. 3) Then when you get the black screen you know what code you just added so you know what to fix. I would guess your problem is the while loop. Read the Swing tutorial on [How to Use Progress Bars](https://docs.oracle.com/javase/tutorial/uiswing/components/progress.html) for working examples and better structured code. You should NOT be using static methods. – camickr May 03 '21 at 16:18
  • You call the `startTimer()` method from an `ActionListener`. This means that `startTimer()` blocks the EDT and prevents Swing from redrawing your UI (because this is also done on the EDT.) For more help look for example at https://stackoverflow.com/questions/9419252/why-does-this-simple-java-swing-program-freeze – Thomas Kläger May 03 '21 at 17:54

0 Answers0