1,105,242 Community Members

Event Handling with Custom Class of Buttons

Member Avatar
javaprog200
Light Poster
47 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 2 [?]
 
0
 

Hello,

I have a class (Buttons.java) which inherits from JButton and implements the ActionListener interface. The second file (ButtonsTest.java) creates a GridLayout of Buttons and adds them to the applet. The issue I am having is when I click on any of these buttons, the event is not registered. I would be grateful for any help.

         import javax.swing.*;
         import java.awt.event.*;
         import java.awt.*;

            public class ButtonsTest extends JApplet
            {
                private Buttons[] array1 = new Buttons[9];
                private Buttons[] array2 = new Buttons[9];
                private GridLayout  lt;
                private JPanel panelA = new JPanel();
                private JPanel panelB = new JPanel();

                public void init()
                {

                  lt = new GridLayout (3,3,5,5);
                  panelA.setLayout (lt);
                  panelB.setLayout (lt);
                  for (int i = 0; i <9; i++)
                       {
                           array1[i] = new Buttons (String.valueOf(i));
                           array1[i].setPreferredSize(new Dimension (40, 40));
                           array1[i].setFont (new Font ("Calibri", Font.PLAIN, 50));
                           array1[i].setText(String.valueOf(i));
                           panelA.add(array1[i]);
                       }
                  for (int i = 0; i <9; i++)
                       {
                           array2[i] = new Buttons (String.valueOf(i));
                           array2[i].setPreferredSize(new Dimension (40, 40));
                           array2[i].setFont (new Font ("Calibri", Font.PLAIN, 50));
                           array2[i].setText(String.valueOf(i));
                           panelB.add(array2[i]);
                       }


                 add(panelA, BorderLayout.NORTH);
                 add(panelB, BorderLayout.SOUTH);

                }


            }


            import javax.swing.*;
            import java.awt.event.*;
            import java.awt.*;

            public class Buttons extends JButton implements ActionListener
            {
                private String id;

                public Buttons(String id)
                {
                   this.id = id;
                   Graphics g = 
                }

                public void actionPerformed (ActionEvent e)
                {
                    JOptionPane.showMessageDialog (null, "Button#" + id);
                }
            }
Member Avatar
JamesCherrill
... trying to help
10,364 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

I can't see where you have added the action listener to the buttons. It would be easy to do that in the Buttons constructor.
http://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#addActionListener%28java.awt.event.ActionListener%29

Member Avatar
javaprog200
Light Poster
47 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 2 [?]
 
0
 

James,

Thank you! It works now.

Regards

Member Avatar
JamesCherrill
... trying to help
10,364 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

That's great! Please consider marking this thread as "solved" so people will know there is an answer here.

Question Answered as of 1 Year Ago by JamesCherrill
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article