Why is JTextField Action Listener not working

289 views Asked by At

Ok, so I am new to Java programming and I wanted to make a simple JAVA GUI app that you first enter an amount of seconds and then you click a button as many times as you can in that number of seconds (Like a Click Speed Test Concept). It's far from finished and now I was implementing so that I can add a placeholder for the text field to explain to the user but the focus action listener is not working.


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.time.LocalTime;
import java.util.*;

public class Main implements ActionListener, FocusListener {

    private JPanel panel;
    private JPanel initPanel;
    private JFrame frame;
    private JButton button;
    private JButton newButton;
    private JLabel label;
    private JLabel newLabel;
    private JTextField textInput;
    private LocalTime time;
    private ImageIcon img;
    private int count = 0;
    private Scanner input = new Scanner(System.in);

    public static void constructor() {

        Main app = new Main();

        app.panel = new JPanel();
        app.frame = new JFrame();
        app.label = new JLabel("Number of clicks: 0");
        app.img = new ImageIcon("E:\\Descarcari Chrome\\mouse.png");
        app.time = LocalTime.now();

        app.panel.setBorder(BorderFactory.createEmptyBorder(20, 40, 10, 40));
        app.panel.setLayout(new GridLayout(0, 1));

        app.button = new JButton("Click me!");
        app.button.setFocusPainted(false);
        app.button.setFont(new Font("Arial", Font.BOLD, 40));
        app.button.addActionListener(app);

        app.panel.add(app.button);
        app.panel.add(app.label);

        app.frame.add(app.panel, BorderLayout.CENTER);
        app.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        app.frame.setTitle("Click Speed Test");
        app.frame.setIconImage(app.img.getImage());
        app.frame.pack();
        app.frame.setVisible(true);
        app.frame.setResizable(false);
    }

    public static void chooseTime() {
        Main app = new Main();

        app.initPanel = new JPanel();
        app.newButton = new JButton("Submit!");
        app.textInput = new JTextField("Number of seconds for the test");
        app.frame = new JFrame();


        app.newButton.setFocusPainted(false);
        app.newButton.setFont(new Font("Arial", Font.BOLD, 40));
        app.newButton.addActionListener(app);

        app.textInput.setForeground(Color.GRAY);

        app.initPanel.setBorder(BorderFactory.createEmptyBorder(20, 40, 10, 40));
        app.initPanel.setLayout(new GridLayout(0, 1));
        app.initPanel.add(app.newButton);
        app.initPanel.add(app.textInput);
        app.textInput.addActionListener(app);

        app.frame.add(app.initPanel, BorderLayout.CENTER);
        app.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        app.frame.setTitle("Click Speed Test");
        app.frame.pack();
        app.frame.setVisible(true);
        app.frame.setResizable(false);

    }

    public static void main(String[] args) {
        chooseTime();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == button) {
            count++;
            label.setText("Number of clicks: " + count);
            button.setText("");
        } else if (textInput.getText().isEmpty() == false){
            frame.getContentPane().removeAll();
            frame.repaint();
            frame.setVisible(false);
            constructor();
        }else {
            JLabel wrong = new JLabel("Input cannot be null");
            initPanel.add(wrong);
            chooseTime();
        }
    }

    @Override
    public void focusGained(FocusEvent e) {
        System.out.println("FOCUS");
        if(textInput.getText().equals("Number of seconds for the test")) {
            textInput.setText("");
            textInput.setForeground(Color.BLACK);
        }
    }

    @Override
    public void focusLost(FocusEvent e) {
        System.out.println("LOST FOCUS");
        if(textInput.getText().isEmpty()) {
            textInput.setForeground(Color.GRAY);
            textInput.setText("Number of seconds for the test");
        }
    }
}

Let me explain what I am doing: chooseTime method is the method that will be ran first and it just creates a button that for now just closes the current frame and goes and runs the constructor method which just increment the click count per button press.

0

There are 0 answers