This class shows a GUI with a JLabel, a JButton and a JTextField. When klicking the button, an array of Strings in another class randomizes a String from the array and adds it to the textfield in this class. This works fine.
But I want the button to be repressable. I want the button to get a new randomized String from the array when I press it again, without restarting the program. How do I do?
import com.sun.j3d.utils.behaviors.vp.WandViewBehavior.ResetViewListener; import java.awt.Color; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class p12NameGenGUI extends JFrame implements ActionListener { p12NamnLista randomp12Namn = new p12NamnLista(); JButton knapp; JLabel textTK; JTextField visaNamn; public p12NameGenGUI() { textTK = new JLabel("Tryck på knappen för ett slumpat P12 namn!"); textTK.setForeground(Color.red); knapp = new JButton("TRYCK NU"); visaNamn = new JTextField(); visaNamn.setColumns(10); this.add(textTK); this.add(knapp); knapp.addActionListener(this); this.add(visaNamn); } public static void main(String[] args) { p12NameGenGUI p = new p12NameGenGUI(); p.setSize(300, 300); p.setLocation(200, 300); p.setDefaultCloseOperation(EXIT_ON_CLOSE); p.setLayout(new FlowLayout()); p.setVisible(true); } public void actionPerformed(ActionEvent e) { visaNamn.setText(randomp12Namn.toString()); } }//class end