We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,682 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Event Handling with Custom Class of Buttons

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);
                }
            }
2
Contributors
3
Replies
12 Hours
Discussion Span
5 Months Ago
Last Updated
5
Views
Question
Answered
javaprog200
Light Poster
46 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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

JamesCherrill
... trying to help
Moderator
8,506 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,454
Skill Endorsements: 29

James,

Thank you! It works now.

Regards

javaprog200
Light Poster
46 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

JamesCherrill
... trying to help
Moderator
8,506 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,454
Skill Endorsements: 29
Question Answered as of 5 Months Ago by JamesCherrill

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0746 seconds using 2.72MB