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);
                }
            }

Recommended Answers

All 3 Replies

James,

Thank you! It works now.

Regards

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.