yemi oladejo
yemi oladejo

Reputation: 85

multiple clicks from the jbutton then populate the table

I'm creating a keypad, so i want to be able to click a button, e.g. 2, so if clicked once "A", twice "B", third time "C", I'm not sure how to get the action listener to be able to do this and then populate the select letter depending on the number of clicks to the text area

class keypad {

    public static void main(String[] args) {
        new myApplication();
    }
}

class myApplication extends JFrame implements ActionListener {

    JButton myButton1;
    JButton myButton2;
    JButton myButton3;
    JButton myButton4;
    JButton myButton5;
    JButton myButton6;
    JButton myButton7;
    JButton myButton8;
    JButton myButton9;
    JButton myButton10;
    JButton myButton11;

    // CLASS INSTANCES AND OBJECTS

    myApplication() {
        setLayout(null);

        JPanel panel1 = new JPanel();
        add(panel1);

        JTextArea text1 = new JTextArea(2, 12);
        panel1.add(text1);
        panel1.setBounds(0, 130, 370, 40);
        Font textFont1 = new Font("Arial Bold", Font.BOLD, 18);

        JButton myButton = new JButton("1");
        myButton.setBounds(20, 190, 60, 60);

        JButton myButton1 = new JButton("<html><center> 2 <br /> ABC </center> </html>");
        myButton1.setBounds(85, 190, 60, 60);
        myButton1.addActionListener(this);

        JButton myButton2 = new JButton("<html><center> 3 <br /> DEF </center> </html>");
        myButton2.setBounds(150, 190, 60, 60);

        JButton myButton3 = new JButton("<html><center> 4 <br /> GHI </center> </html>");
        myButton3.setBounds(20, 260, 60, 60);

        JButton myButton4 = new JButton("<html><center> 5 <br /> JKL </center> </html>");
        myButton4.setBounds(85, 260, 60, 60);

        JButton myButton5 = new JButton("<html><center> 6 <br /> MNO </center> </html>");
        myButton5.setBounds(150, 260, 60, 60);

        JButton myButton6 = new JButton("<html><center> 7 <br /> PQRS </center> </html>");
        myButton6.setBounds(20, 330, 60, 60);

        JButton myButton7 = new JButton("<html><center> 8 <br /> TUV </center> </html>");
        myButton7.setBounds(85, 330, 60, 60);

        JButton myButton8 = new JButton("<html><center> 9 <br /> WXYZ </center> </html>");
        myButton8.setBounds(150, 330, 60, 60);

        JButton myButton9 = new JButton("*");
        myButton9.setBounds(20, 400, 60, 60);

        JButton myButton10 = new JButton("0");
        myButton10.setBounds(85, 400, 60, 60);

        JButton myButton11 = new JButton("#");
        myButton11.setBounds(150, 400, 60, 60);

        add(myButton);
        add(myButton1);
        add(myButton2);
        add(myButton3);
        add(myButton4);
        add(myButton5);
        add(myButton6);
        add(myButton7);
        add(myButton8);
        add(myButton9);
        add(myButton10);
        add(myButton11);

        setTitle("Keypad");
        setSize(300, 500);
        setLocation(250, 250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

    }

    public void actionPerformed(ActionEvent e) {
        Object src = e.getSource();
        if(e.getSource()== myButton2){

        }

        // EVENT HANDLING !!!!!
    }

Any ideas please.

Thanks

Upvotes: 1

Views: 30

Answers (1)

Jaskaranbir Singh
Jaskaranbir Singh

Reputation: 2034

You can create an integer variable to handle number of clicks....

something like:

private int clickCounter = 0;

...

public void actionPerformed(ActionEvent e) {
        Object src = e.getSource();
        if(e.getSource()== myButton2){
            if(clickCounter == 0)
               label.setText("A");
            else if (clickCounter == 1)
               label.setText("B");
            ....
            // Time to increase counter after input has been verified
            clickCounter++;
        }

If some input doesn't pass your validation, you can just set clickCounter = 0 to revert it.

Upvotes: 1

Related Questions