Akarasu
Akarasu

Reputation: 45

Button running a whole new class

I want my button to run a whole new class that will do different things inside. I don't know if that is even possible cause i'm really bad at java. My code looks like this at the moment:

public class MainMenu {
    private class GardenActivities {
        public GardenActivities() {
            JFrame GardenAct = new JFrame();
            GardenAct.setSize(400, 400);
            GardenAct.setVisible(true);
        }
    }

    public static void main(String[] args) {
        JFrame choice = new JFrame();
        choice.setSize(700, 500);
        choice.setLocationRelativeTo(null);
        choice.setTitle("Seeds");
        choice.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();                                                        
        panel.setLayout(new GridLayout(0, 1));
        JButton Labora = new JButton();
        Labora.setText("Laboratory");
        Labora.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ev) {
                      GardenActivities();
                }
        });

        JButton Garden = new JButton();
        Garden.setText("Garden");
        Garden.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ev) {
            }
        });

        choice.getContentPane().add(panel);
        ButtonGroup group = new ButtonGroup();                                              
        group.add(Garden);
        group.add(Labora);
        panel.add(Garden);
        panel.add(Labora);
        choice.setVisible(true);
    }
}

Just like I said. I need something to run my GardenActivities class just by pressing Garden button.

Upvotes: 2

Views: 6042

Answers (4)

Lee Meador
Lee Meador

Reputation: 12985

One way to do what you want, we make the GardenActivities class implement ActionListener itself.

Then your code would look something like this:

Garden.addActionListener(new GardenActivities());

Otherwise, your plan should work.

NOTE

See comments for opposing opinions about why one would want to leave the ActionListener in the anonymouse inner class and have it call into GardenActivities.

Thank you @HovercraftFullOfEels

Upvotes: 3

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

Your code doesn't compile, does it? When that happens, you need to post compilation errors with your question so that we can help you with them.

You need to add the key word new before the GardenActivities() statement.

@Override
public void actionPerformed(ActionEvent ev) {
  new GardenActivities(); // here
}

Also, put the GardenActivities in its own file. There's no reason for making it a private inner class and many reasons not to do this.

Having said this, I recommend against having one JFrame create and display another JFrame since an application should have usually only one JFrame. Instead consider swapping JPanel "views" using a CardLayout, or if you must show a different window, consider showing the second dependent window as a modal or non-modal dialog.

Also more unsolicited advice: Your main method is doing way too much. Most of the code inside of the static main method should go inside the non-static main gui class, whatever that is, perhaps in its constructor or in an initGui() method that the constructor calls. The main method should just create an instance of the main gui class, make it visible, and that's about it.


And regarding:

I don't know if that is even possible cause i'm really bad at java.

Keep writing lots and lots of code, a ton of code, and keep reviewing tutorials and textbooks, and this will change.

Upvotes: 7

BlackHatSamurai
BlackHatSamurai

Reputation: 23483

As others have pointed out, this:

 @Override
                public void actionPerformed(ActionEvent ev) 
                    {
                      GardenActivities();
                    }

Should look like:

 @Override
                public void actionPerformed(ActionEvent ev) 
                    {
                      new GardenActivities();
                    }

There is no reason to create an inner class, and GardenActivities can be, and should be, its own class.

Upvotes: 1

James Williams
James Williams

Reputation: 678

I think that you just need to add:

new GardenActivities();

Into your JButton's actionPerformed() method.

Good Luck!

Upvotes: 3

Related Questions