Java Weenis
Java Weenis

Reputation: 83

How to make a JButton repeat its action when clicked again?

I am trying to make the "Generate" JButton place a new word in the text area when pressed each time. I have tried loops and this has caused my GUI to stop responding.

Sample user input: clicks "Generate" button 3 times Sample output in text area: applepotatocucumber

Any suggestions?

Here is the code:

 public Generator(){
    String[] words = {apple, pear, cucumber, lettuce, tomato, potato};
    String random = (words[new Random().nextInt(words.length)]);

            setBackground(Color.CYAN);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 450, 300);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
            SpringLayout sl_contentPane = new SpringLayout();
            contentPane.setLayout(sl_contentPane);

            JLabel lblNewLabel = new JLabel("Random Word Generator");
            sl_contentPane.putConstraint(SpringLayout.NORTH, lblNewLabel, 10,     SpringLayout.NORTH, contentPane);
            sl_contentPane.putConstraint(SpringLayout.WEST, lblNewLabel, 27, SpringLayout.WEST, contentPane);
            lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 21));
            lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
            contentPane.add(lblNewLabel);


            JLabel lblInfo = new JLabel("Result will be printed in the white space.");
            sl_contentPane.putConstraint(SpringLayout.NORTH, lblResultsWillBe, 73, SpringLayout.NORTH, contentPane);
            sl_contentPane.putConstraint(SpringLayout.WEST, lblResultsWillBe, 124, SpringLayout.WEST, contentPane);
            lblResultsWillBe.setHorizontalAlignment(SwingConstants.CENTER);
            contentPane.add(lblInfo);

            JTextArea textArea = new JTextArea();
            textArea.setWrapStyleWord(true);
            sl_contentPane.putConstraint(SpringLayout.NORTH, textArea, 150, SpringLayout.NORTH, contentPane);
            sl_contentPane.putConstraint(SpringLayout.WEST, textArea, 141, SpringLayout.WEST, contentPane);
            sl_contentPane.putConstraint(SpringLayout.SOUTH, textArea, 12, SpringLayout.SOUTH, contentPane);
            sl_contentPane.putConstraint(SpringLayout.EAST, textArea, -124, SpringLayout.EAST, contentPane);
            contentPane.add(textArea);

            JButton btnGenerate = new JButton("Generate");
            sl_contentPane.putConstraint(SpringLayout.NORTH, btnGenerate, 41, SpringLayout.NORTH, contentPane);
            sl_contentPane.putConstraint(SpringLayout.WEST, btnGenerate, 163, SpringLayout.WEST, contentPane);
            contentPane.add(btnGenerate);
            btnGenerate.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                textArea.setText(random);
                Object source = e.getSource();
                if (source == btnGenerate){
                        textArea.setText(random);
                }

            }});
            JLabel txtResults = new JLabel();
            sl_contentPane.putConstraint(SpringLayout.SOUTH, txtResults, -100, 

    SpringLayout.SOUTH, contentPane);
                sl_contentPane.putConstraint(SpringLayout.EAST, txtResults, -143, SpringLayout.EAST, contentPane);
            contentPane.add(txtResults);

        }

Upvotes: 1

Views: 776

Answers (1)

camickr
camickr

Reputation: 324108

Sample output in text area: applepotatocucumber

Don't use the setText() method. That replaces the existing text.

Instead use:

textArea.append(random);

So every time you click the button new text will be appended to the text area.

Upvotes: 1

Related Questions