-1

I have a basic GUI class with a frame, a table, and a button. I want to make it launch from the ActionListener of another one of my basic GUI frames located in a different class

this is my main class:

public class IA {    
public static void main(String[] args) { 

    MainFrame m1 = new MainFrame();
   m1.setVisible(true);             

} /*enter code here*/

   public static void vts1 () {   
    ViewTeamStatistics v1 =  new ViewTeamStatistics();       
    v1.setVisible(true); 
    }
}

It initiates my main menu and from the main menu i want to initiate another class named ViewTeamStatistics this is the actionperformed. this is what is supposed to tell the program to open the frame after i press the button

private void vtsActionPerformed(java.awt.event.ActionEvent evt) {                                    
  ViewTeamStatistics v1 =  new ViewTeamStatistics();       
  v1.setVisible(true);         
}        

The compiler comes back with no errors but when I run the program and press the button nothing happens.

John Koerner
  • 37,428
  • 8
  • 84
  • 134
  • 1
    You don't actually have an ActionListener. – SLaks Sep 17 '13 at 16:59
  • 1
    Time to do some debugging to try to isolate the error. Use your IDE's debugger for this. Til you isolate the error, you (and we) can do nothing to solve it. If you're still stuck, then create an [sscce](http://sscce.org) and post it here. – Hovercraft Full Of Eels Sep 17 '13 at 17:00
  • what is vtsActionPerformed? You need to read this: http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html – Cruncher Sep 17 '13 at 17:04
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Sep 17 '13 at 17:52

1 Answers1

0

I don't fully understand your question, do you want to launch a new frame upon pressing a button? If that's it here is a sample code :

public class ExampleWindow implements ActionListener{
    private JFrame mainFrame;
    private JButton button;

    public ExampleWindow(){
        button = new JButton("Press me!");
        button.addActionListener(this);
        mainFrame = new JFrame("Frame name");
        mainFrame.add(button);
        mainFrame.setVisible(true);
        //Remember about this line
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        new SomeWindow();
    }
}


class SomeWindow{
     private JFrame frame;

     public SomeWindow(){
         frame = new JFrame;
         frame.setVisible(true);
     }
}

I didn't try to compile it so there might be some errors.

John Smith
  • 87
  • 10
  • 1
    `//Remember about this line mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);` Indeed. Remember to avoid `EXIT_ON_CLOSE` and use `DISPOSE_ON_CLOSE` (and to shut down any threads explicitly started by the app.). – Andrew Thompson Sep 17 '13 at 17:51
  • I was not aware of that, thanks. However in this simple example EXIT_ON_CLOSE is sufficient or is it not? – John Smith Sep 17 '13 at 21:59
  • No. Try running the code on [this answer](http://stackoverflow.com/a/7143398/418556). Now close one of the frames. The other two remain on-screen. Close both of the remaining ones and the VM will end. Now change the close operation to `EXIT_ON_CLOSE` compile and run it again. Close any one of the frames and the entire app. ends! So, no, `EXIT_ON_CLOSE` is not good in a general situation, and even *worse* in this one with multiple frames. As an aside, I think this answer is of generally poor quality. No use of the EDT, no use of `pack()`, less than optimal close operation.. – Andrew Thompson Sep 18 '13 at 06:04