Qamar Irfan
Qamar Irfan

Reputation: 40

How do I make JButton open different frames?

I have created 2 JButtons but both open the same file how do i make the second button open another file........................................................................................................................................................

//Starting Page

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


   public class AddressBook implements ActionListener     // Create a new class Address Book
{
    JFrame Start=new JFrame("Address Book");     // Set name of Frame
    JButton Open;              // Set new button
    JButton Second;
 {
      Open=new JButton("OPEN");     // set name of button
      Second=new JButton("Second");
      Start.setSize(500,600);       // set size of frame
      Start.add(new JLabel(new ImageIcon("backgroundforlab.jpg")));      // add background picture
      Start.setVisible(true); 
      Start.setLayout(null);
      Start.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);  
      Start.getContentPane().add(Open);                           //Make button visible
      Start.getContentPane().add(Second); 
      Open.setBounds(100,385,295,88);  
      Second.setBounds(50,160,150,44);                           // set size of button
      Open.addActionListener(this);
      Second.addActionListener(this);

 }

   public void actionPerformed(ActionEvent e)
{
      Start.dispose();              // When button is clicked close frame and open mainMenu 
      mainMenu A=new mainMenu();    
}

   public static void main(String ag[])
  {
      AddressBook A=new AddressBook();          // run class AddressBook
  }  


}

Upvotes: 0

Views: 689

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347194

You could...

Use separate ActionListeners for your buttons

Open.addActionListener(new OpenActionListener());
Second.addActionListener(new SecondActionListener());

You'll need to supply the implementations of the ActionListeners as additional classes

This is probably one of the preferred methods, as it isolates functionality/responsibility for the action to a single class, but it does create a bunch of small class.

You could..

Use anonymous classes instead...

Open.addActionListener(new ActionListener() {
    @Overrride
    public void actionPerformed(ActionEvent e) {
        //...
    }
});

This is basically the same idea as before, but it doesn't require a separate class to manage

You could...

Use the actionCommand property to identify the buttons

Open.setActionCommand("open");
Second.setActionCommand("second");

//...

public void actionPerformed(ActionEvent e) {
  String command = e.getActionCommand();
  if ("open".equals(command)) {
      //...
  } else if ("second".equals(command)) {
      //...
  }
}

This is good if you have a number of buttons which repeat actions (like menus and toolbar buttons)

You could...

Use the source property to identify the buttons

public void actionPerformed(ActionEvent e) {
  Object source = e.getSource();
  if (source == open) {
      //...
  } else if (source == second) {
      //...
  }
}

This will only work if the ActionListener has access to the actually references to the buttons. This makes it limited in it's use, especially since there are better solutions available

You should also have a look at:

for more details and ideas

Upvotes: 1

Related Questions