Search code examples
javavariablesjbuttonjlabel

Recalling JButton value


I'm looking to recall a JButton's value when clicked i.e. when the user clicks a JButton, the value of that JButton (which is a single letter) will be written to a JLabel. The user will click multiple buttons and as such, multiple values will need to be stored and printed. Finally the user will click a button and the JLabel holding all recieved JButton values will be stored (Obviously using an array).

Here is what my JButton's look like in code:

theModel.randomLetters();

            for(int i=0;i<16;i++){
                JButton dice = new JButton(theModel.letters.get(i));
                dice.addActionListener(disableButtonListener);
                boggleGrid.add(dice);
            }

theModel.randomLetters(); is a reference to another class with "letters" being an array holding 16 values. Will I need to add each JButton individually to the boggleGrid so their individual names can be recalled to achieve the goal stated above, or do they have individual names and I don't know it? (I've used a for loop someone gave me so I'm not sure if there are individual names for each JButton)

Thanks, and sorry if this is elementary


Solution

  • Read the following code, you'll get some ideas...

    I used a StringBuilder to store the values of the Button's each time they were clicked by using StringBuilder.append(JButton.getText().toString()). That was done in the actionPerformed methods of the JButtons.

    Then finally in the Done buttons actionPerformed method I had stored the text the string builder held in an array.

    P.S. I know you're using 16 JButton's I just used 3 for simplicity...

    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.util.ArrayList;
    
    public class GUI extends JFrame{
    
    
        ArrayList<String> numbers = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        JButton button1;
        JButton button2;
        JButton button3;
        JButton doneButton;
        JPanel row1 = new JPanel();
        JPanel textLabelRow = new JPanel();
        JLabel label = new JLabel();
        JPanel row3 = new JPanel();
    
        public void create(){
            setTitle("S-O");
            setSize(500,200);
    
            setLayout(new GridLayout(3,1,10,10));
    
            button1 = new JButton("1");
            button2 = new JButton("2");
            button3 = new JButton("3");
            doneButton = new JButton("Done");
            row1.add(button1);
            row1.add(button2);
            row1.add(button3);
            button1.addActionListener(new Button1Listener());
            button2.addActionListener(new Button2Listener());
            button3.addActionListener(new Button3Listener());
            doneButton.addActionListener(new doneButtonListener());
    
    
            textLabelRow.add(label);
            row3.add(doneButton);
            add(row1);
            add(textLabelRow);
            add(row3);
    
    
            setVisible(true);
        }
    
        private class Button1Listener implements ActionListener{
            @Override
            public void actionPerformed(ActionEvent e) {
                sb.append(button1.getText().toString());
            }       
        }
    
        private class Button2Listener implements ActionListener{
            @Override
            public void actionPerformed(ActionEvent e) {
                sb.append(button2.getText().toString());
            }       
        }
        private class Button3Listener implements ActionListener{
            @Override
            public void actionPerformed(ActionEvent e) {
                sb.append(button3.getText().toString());
            }       
        }
        private class doneButtonListener implements ActionListener{
    
            @Override
            public void actionPerformed(ActionEvent e) {
                label.setText(sb.toString());
                numbers.add(sb.toString());
                sb = new StringBuilder();
            }
        }
    
        public static void main(String[] args){
            GUI start = new GUI();
            start.create();
        }
    
    }
    

    EDIT:

    You could just use one class which implements ActionListener opposed to having one for each JButton.

    With in the actionPerformed method "public void actionPerformed(ActionEvent ev)"

    You would have to ev.getActionCommand(), that would return the text value of the JButton which caused the event. Then you could append that to the StringBuilder.

    So something along the lines of this: private ArrayList listForEdit = new ArrayList<>(); StringBuilder sbEdit = new StringBuilder();

       class actionHandler implements ActionListener{
             public void actionPerformed(ActionEvent ev){
                   if(ev.getActionCommand.equals("Done"))
                    listForEdit.add(sbEdit);
                    sbEdit = new StringBuilder();
                   }
                   else{
                        sbEdit.append(ev.getActionCommand());
                        }
                  }