Im kind of new to programming in java but here im trying to figure out how to add the value of a button clicked in ActionEvent. I even changed the actionlistener to MouseListener etc but still not working. It only displays the current button clicked but i would like to add every button clicked into an arraylist.
Problem: I cant add the value of buttons clicked into an arraylist.
Example: if i click on the button named E then i want E to be added to arraylist, after that if i click on the button S then it should add S o the same arraylist. but its not adding aything to the arraylist. it only displays the current button clicked
public class WordFinder extends JFrame implements ActionListener { private JButton buttons[]; ArrayList<String> LettersClicked = new ArrayList<String>(); private JTextArea jta; private JLabel jLabel; public WordFinder(){ int numberOfLetters = 64; buttons = new JButton[numberOfLetters]; JFrame frame = new JFrame("wordfinder"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setBackground(Color.red); JPanel mainPanel = new JPanel(); mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS)); JPanel topPanel = new JPanel(); topPanel.setBorder(BorderFactory.createLineBorder(Color.GREEN)); topPanel.setPreferredSize(new Dimension(600, 600)); topPanel.setMaximumSize(new Dimension(600, 600)); topPanel.setBackground(Color.red); JPanel bottomPanel = new JPanel(); bottomPanel.setBorder(BorderFactory.createLineBorder(Color.yellow)); bottomPanel.setPreferredSize(new Dimension(600, 300)); bottomPanel.setMaximumSize(new Dimension(600, 300)); bottomPanel.setBackground(Color.green); Font f3 = new Font(Font.DIALOG, Font.BOLD, 35); for (int i = 0;i<numberOfLetters;i++) { char letter = alphabet(); buttons[i] = new JButton(String.valueOf(letter)); buttons[i].setFont(f3); // buttons[i].setMinimumSize(new Dimension(40, 20)); buttons[i].setPreferredSize(new Dimension(65, 65)); buttons[i].setMargin(new Insets(0,0,0,0)); buttons[i].setBackground(Color.white); buttons[i].setFocusPainted(false); buttons[i].addActionListener(this); topPanel.add(buttons[i]); } String buttValue = ""; jLabel = new JLabel(""+buttValue); jLabel.setFont(f3); bottomPanel.add(jLabel); LettersClicked.add(jLabel.toString()); for (int z = 0; z<LettersClicked.size(); z++){ JLabel aa = new JLabel(""+LettersClicked.size()); bottomPanel.add(aa); } mainPanel.add(topPanel); mainPanel.add(bottomPanel); frame.getContentPane().add(mainPanel); frame.setSize(1000,1000); frame.setMinimumSize(new Dimension(1000, 1000)); frame.setVisible(true); } public char alphabet(){ Random rnd = new Random(); String alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char letter = alphabets.charAt(rnd.nextInt(alphabets.length())); return letter; } @Override public void actionPerformed(ActionEvent actionEvent) { JButton button = (JButton) actionEvent.getSource(); jLabel.setText(button.getText()); LettersClicked.add(button.getText()); } }
Advertisement
Answer
I think you are confused about the content of the JLabel
displayed on the GUI and your ArrayList
. The issue is actually in the former.
Your current implementation replaces the text of the JLabel
instead of appending it for each pressed button.
So, in your actionPerformed()
method, change this line
jLabel.setText(button.getText());
to
jLabel.setText(jLabel.getText() + button.getText());