0

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
  }  


}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) The most logical alternative for this case would be to use `JDialog` for the child windows. – Andrew Thompson Jan 11 '18 at 04:06
  • Hmm, I just started using java so I don't understand much, i'm doing a school project which i need to create multiple jframes, thanks for the advice though. – Qamar Irfan Jan 11 '18 at 04:16
  • *"i need to create multiple jframes"* I really doubt that (they explicitly stated `JFrame`). To recommend that to students would be extreme incompetence. – Andrew Thompson Jan 11 '18 at 05:02
  • oh haha, not jframe but more than one window you could say, this is for my grade 12 culminating, teaching my self how to use Jbuttons, jframes, etc. – Qamar Irfan Jan 11 '18 at 05:04
  • *"more than one window"* Well, as implied by my first comment, a `JDialog` **is a window**. It's just much better suited to being a window that has a parent window (such as a `JFrame`) – Andrew Thompson Jan 11 '18 at 05:08
  • ohhh ok, i understand now. Thanks, for the tip. – Qamar Irfan Jan 11 '18 at 05:11

1 Answers1

1

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

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thanks so much this helped me understand so much more, since i am going to be using lots of buttons i want to use the action command, but i don't know how to use it, sorry i am new to this. I added the action command code you gave, and after the if ("open".equals(command)) { I added Start.dispose(); the program runs with no error but nothing happens once i click the button. I know im missing something but i don't know what, i tried searching how to use action command, but i don;t understand how to use it in my code. A little guidance would be really helpful . Thanks – Qamar Irfan Jan 11 '18 at 02:56
  • Add the above code to the same place you create/add your buttons – MadProgrammer Jan 11 '18 at 03:27
  • I have already done that, i am not sure what to type in after the if ("open".equals(command)) { //... – Qamar Irfan Jan 11 '18 at 03:29
  • What ever action you want to take when the button with the `"open"` command is actioned – MadProgrammer Jan 11 '18 at 03:37
  • Yes, i added Start.dispose(); to close the frame nut nothing happens when i click the button. – Qamar Irfan Jan 11 '18 at 03:42
  • I've tested your code and I can verify that the `actionListener` is called and prints out the correct `actionCommand` – MadProgrammer Jan 11 '18 at 03:49
  • OMG I finally figured it out, when you told me to add Open.setActionCommand("open"); I was taking out Open.addActionListener(this); and replacing it with Open.setActionCommand("open"); . WOW. I need both, such a stupid mistake. Thank you for helping me this far, I know some people whould have already stopped helping me at this point, lol sorry. – Qamar Irfan Jan 11 '18 at 03:58