Mr. Viggo
Mr. Viggo

Reputation: 69

Swing: How do I use multiple buttons?

I created this little test program. It has 2 buttons and 2 labels. I want to know how I can use 2 buttons. So when I press button-1 then I change the text for text-1 and when I press button-2 then I change text for text-2. I just wanna get an idea of how I can use multiple buttons.

My code:

JLabel text1, text2;
JButton button1, button2;

public Game(String title) {
    super(title);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    getContentPane().setLayout(new FlowLayout());

    addComponents();

    setSize(250, 250);
    setResizable(false);

}

public void addComponents() {
    text1 = new JLabel();
    getContentPane().add(text1, text2);

    text2 = new JLabel();
    getContentPane().add(text2);

    button1 = new JButton("Button");
    getContentPane().add(button1);
    button1.addActionListener(this);

    button2 = new JButton("Button 2");
    getContentPane().add(button2);
    button2.addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e) {


}

I'm new to programming, so I would also like if someone could write some comments for the code. Just so I get an idea on how the code for multiple buttons work.

Upvotes: 0

Views: 2445

Answers (4)

Nicolas
Nicolas

Reputation: 121

You should not use setVisible(true) before the components are added.

There are a few ways to deal with more elements in an ActionEvent:

  • e.getSource() returns the object on which the event occurred. So, if button1 was pressed, e.getSource() will be the same as button1 (and e.getSource()==button1 will thus be true)
  • You can use separate classes for each ActionEvent. If you would add the ActionListener "Button1ActionEvent" [button1.addActionListener(new Button1ActionEvent());] you have to create this class, let it implement ActionListener and add the method actionPerformed as you had in your main class. Also, you can create a listener inside of the addActionListener-method [button1.addActionListener(new ActionListener() { // actionPerformed-method here });]

Upvotes: 0

user140547
user140547

Reputation: 8200

Using Java 8, its it much more concise to add ActionListeners:

button.addActionListener(ae -> System.out.println("foo"));

Using multiple statements:

button.addActionListener(ae -> { 
    System.out.println("foo");
    System.out.println("bar");
});

Upvotes: 0

Jordi Castilla
Jordi Castilla

Reputation: 27003

There are various approaches to add listeners to buttons, here just a couple:

Inner

If you don't have to do much actions in each button you can add inner listener in each button

button1.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // DO STUFF

    }
});

Common Listener

If you have more than 2 buttons (i guess your app will be bigger) you can use your actionPerformed(ActionEvent e) and get source of the action

@Override
public void actionPerformed(ActionEvent e) {
    JButton source = (JButton) e.getSource();
    if(source.equals(button1)){
        // DO STUFF    
    }
}

Use actionCommand to clarify

To clarify this approach I would reccommend to use JButton.setActionCommand(stringCommand) so after you can use a switch:

Declaring buttons:

button1.setActionCommand("command1");
button2.setActionCommand("command2");

In ActionListener::actionPerformed()

public void actionPerformed(ActionEvent e) {
    String command = ((JButton) e.getSource()).getActionCommand();

    switch (command) {
    case "command1": 
        // DO STUFF FOR BUTTON 1
    break;
    case "command2": 
        // DO STUFF FOR BUTTON 2
    break;
    }
}

Upvotes: 1

Arda Keskiner
Arda Keskiner

Reputation: 792

In your actionPerformed method you can get the source of the action

@Override
public void actionPerformed(ActionEvent e) {

if(e.getSource() == button1){
 //Do Something
}else if(e.getSource() == button2){
 //Do Something Else
}

Upvotes: 2

Related Questions