lundi 7 décembre 2020

After inserting values in jtextfields, their random value is not shown. How can I fix it?

I wrote this Java Application which is used to extract a random value from those entered in the JTextFields after starting a counter. Compiles and gives no errors, but the ("" +randomElement) value is not displayed.

I created the randomElement property inside the TimerListener class in the actionPerformed method which starts the counter. At the end of the count the random value taken from the four jtextfields must be shown (as you can see from the code below).

Could you tell me where I'm wrong and how can I fix it?

public class Merge extends JPanel {
public static final int TIMER_DELAY = 1000;
public static final String TEST_TEXT = "123456";
public JTextField textField = new JTextField(16);
public JButton button = new JButton(new ButtonAction());
public Timer timer;
JTextField t1Text = new JTextField();
JTextField t2Text = new JTextField();
JTextField t3Text = new JTextField();
JTextField t4Text = new JTextField();
JLabel f1Label = new JLabel("Filippo film 1");
JLabel f2Label = new JLabel("Filippo film 2");
JLabel l1Label = new JLabel("Laura film 1");
JLabel l2Label = new JLabel("Laura film 2");

public Merge() {
    add(button);
    add(textField);
}

public static void main(String[] args) {
    Merge me = new Merge();
    me.createAndShowGui();
}

public void createAndShowGui() {
    Merge mainPanel = new Merge();
    JFrame frame = new JFrame("Film roulette");
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.getContentPane().add(mainPanel);
    frame.setLayout(new GridLayout(11, 4));
    frame.pack();
    frame.setBounds(100, 10, 400, 400);
    frame.add(f1Label);
    frame.add(t1Text);
    frame.add(f2Label);
    frame.add(t2Text);
    frame.add(l1Label);
    frame.add(t3Text);
    frame.add(l2Label);
    frame.add(t4Text);
    frame.setLocationByPlatform(true);
    frame.setVisible(true);
}

public class ButtonAction extends AbstractAction {

    public ButtonAction() {
        super("The movie you will see...");
        putValue(MNEMONIC_KEY, KeyEvent.VK_P);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (timer != null && timer.isRunning()) {
            return;
        }
        textField.setText("");
        timer = new Timer(TIMER_DELAY, new TimerListener());
        timer.start();
    }
}

private class TimerListener implements ActionListener {
    private String text = "";
    private int counter = 0;

    @Override
    public void actionPerformed(ActionEvent e) {
        
        String filmFilippo1, filmFilippo2, filmLaura1, filmLaura2;

        filmFilippo1 = t1Text.getText();
        filmFilippo2 = t2Text.getText();
        filmLaura1 = t3Text.getText();
        filmLaura2 = t4Text.getText();

        List<String> givenList = Arrays.asList(filmFilippo1, filmFilippo2, filmLaura1, filmLaura2);
        Random rand = new Random();
        String randomElement = givenList.get(rand.nextInt(givenList.size()));
        
        text += TEST_TEXT.charAt(counter);
        textField.setText(text);
        counter++;
        if (counter >= TEST_TEXT.length()) {
            timer.stop();
            textField.setText("" +randomElement);
        }
    }
}

}




Aucun commentaire:

Enregistrer un commentaire