2

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Akarasu
  • 45
  • 3
  • 7
  • It is pretty much the same as how you showed the window in your main method, essentially just copying and pasting that code (relevant to showing the window) and putting it in your `ActionListener`. – Josh M Aug 20 '13 at 21:47
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Aug 20 '13 at 23:02

4 Answers4

7

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
3

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

Lee Meador
  • 12,829
  • 2
  • 36
  • 42
  • Unless you're writing toy programs, you should not have the GUI class (the view) implement a listener (the control) as this is asking the class to be too many things.\ – Hovercraft Full Of Eels Aug 20 '13 at 21:38
  • @HovercraftFullOfEels True, but ... if you look at the `GardenActivities` class, it is full of display code, setting up a new `JFrame` and doing whatever. If the class was doing something related to the work of the program rather than pure presentation, I would say not to make it an `ActionListener` – Lee Meador Aug 20 '13 at 21:40
  • Lee, he's already using anonymous inner classes for his action listeners which will tailor each action listener for the JButton that uses it. Why instead recommend that he make the GUI implement the listener and thus force him to write a dreaded switchboard actionPerformed method. Sorry, but this is just bad advice. – Hovercraft Full Of Eels Aug 20 '13 at 21:42
  • @HovercraftFullOfEels You have some good points. I'm going to leave the answer but the comments will point out possible problems with this choice. – Lee Meador Aug 21 '13 at 14:27
3

I think that you just need to add:

new GardenActivities();

Into your JButton's actionPerformed() method.

Good Luck!

James Williams
  • 678
  • 4
  • 14
1

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.

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156