Search code examples
javaswingtimermoveprogram-entry-point

Trying to move a code to main - Java


I am trying to put this code in main()

public class TestPane extends JPanel {

    private JTextField field;
    private JButton button;
    private int tick; 
    private Timer timer;

    public TestPane() {

        field = new JTextField(10);
        field.setEditable(false);
        button = new JButton("Start");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                button.setEnabled(false);
                tick = 0;
                timer.start();
            }
        });

        timer = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Success" + tick);
                field.setText(Integer.toString(++tick));
                if (tick > 4) {
                    timer.stop();
                    button.setEnabled(true);
                }
            }
        });
        timer.setInitialDelay(0);


        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        add(field, gbc);
        add(button, gbc);

    }
}

What this does is; it opens a window and then there is a start button if u press that start button, it shows some text after some intervals of time

What i want to do is put the code in main function and what should happen is that there is no start button and when u run the program, it should set text in the area in some interval of times (automatically without pressing the button)

I tried BUT FAILED Here is the code

public static void main(String args[]) {
    //int tick; 
    // Timer timer;
    final Timer timer = new Timer(1000, new ActionListener() {
        int tick=0;

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("Success" + ++tick);
            if (tick > 4) {
                ((Timer)e.getSource()).stop(); 
            }
        }
    });
    timer.setInitialDelay(0);
    System.out.format("About to schedule task.%n");
    new NewJFrame();
    System.out.format("Task scheduled.%n");

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new NewJFrame().setVisible(true);
        }
    });
}

I am not able to figure out what to do ?


Solution

  • As dict19 said, you could do it like this, also added another place you could start it, directly in the main method, really depends on what you want to acheive :

    public static void main(String args[]) {
    
      final Timer timer = new Timer(1000, new ActionListener() {
        int tick = 0;
    
        @Override
        public void actionPerformed(ActionEvent e) {
          System.out.println("Success" + ++tick);
          if (tick > 4) {
            ((Timer) e.getSource()).stop();
          }
        }
      });
    
      timer.setInitialDelay(0);
      System.out.format("About to schedule task.%n");
      // timer.start(); Or here
      System.out.format("Task scheduled.%n");
    
      java.awt.EventQueue.invokeLater(new Runnable() {
    
        public void run() {
          new NewJFrame().setVisible(true); // previously new Test().setVisible(true);
          timer.start();
        }
      });
    }