0

I'm trying to create a custom component in NetBeans which contains 2 buttons on a panel.

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JPanel;

public class CustomComponent extends JPanel {

    JButton button1 = new JButton("Button 1");
    JButton button2 = new JButton("Button 2");

    public CustomComponent() {
        
        setLayout(new FlowLayout());
        add(button1);
        add(button2);

        button1.setSize(100, 30);
        button2.setSize(100, 30);
    }

}

When I use this custom component on another project's JFrame (using GUI designer), those two buttons need to have two different ActionPerformed events and those events must be shown in the Netbean's event list. is that possible to do?

(Currently, I only see the events owned by the JPanel.)

enter image description here

Thanks in advance

dins88
  • 165
  • 1
  • 8
  • 1
    You need to select the individual buttons rather than the custom jPanel. Then you will be able to create an event for each one. When working with custom components, or extending/overriding classes/methods you really can't rely on the GUI designer, I suggest adding the button events manually to the `CustomComponent` class. – sorifiend Nov 21 '21 at 23:51
  • there is no way to select an individual button on the designer for the custom component. No matter where we click it only selects the whole panel. if you can show me how to add events manually it will be helpful to me. – dins88 Nov 22 '21 at 00:47
  • 1
    You can add events to your buttons manually as shown here: https://stackoverflow.com/questions/284899/how-do-you-add-an-actionlistener-onto-a-jbutton-in-java and also see the tutorial here on creating an action listener/event: https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html – sorifiend Nov 22 '21 at 01:17

2 Answers2

1

As per the links above, you can add an action listener like below. This example is for a generic ActionEvent, but you can modify the code to other types of events as well:

//The button to add an event to
JButton test = new JButton();

//The first option
test.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        System.out.println("Action performed");
    }
});

//The second option. This only works on Java 8 and newer
test.addActionListener((ActionEvent e) ->
{
    System.out.println("Action performed");
});

//Or a simplified form for a single call
test.addActionListener(e -> System.out.println("Action performed"));

And a working example in your case:

public CustomComponent() {
    
    setLayout(new FlowLayout());
    add(button1);
    add(button2);

    button1.setSize(100, 30);
    button2.setSize(100, 30);
    
    //Add events
    button1.addActionListener(e -> System.out.println("Button 1 action performed"));
    button2.addActionListener(e -> System.out.println("Button 2 action performed"));
}
sorifiend
  • 5,927
  • 1
  • 28
  • 45
0

instead of passing listeners through the constructor, I have created another setter, because the GUI does not support that.

public void setListners(ActionListener btn1ActionListner, ActionListener btn2ActionListner){
        button1.addActionListener(btn1ActionListner);
        button2.addActionListener(btn2ActionListner);
    }

on the other project

  public NewJFrame() {
        initComponents();

        ActionListener al1 = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "Button 1");
            }
        };

        ActionListener al2 = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "Button 2");
            }
        };

        customComponent1.setListners(al1, al2);
    }
dins88
  • 165
  • 1
  • 8