1

Update: I've decided the simplest thing to do at the moment would be to use separate JPane's and not JFrame's for the sub-menu's. I'll create them all together and set the others to invisible, and toggle that way. The menus aren't that complex that this would be too much of a problem.

I am creating a GUI that opens another JFrame window from a button click in another. I am just not sure of the right way to approach closing the main window when one of the buttons is clicked, but not closing the whole program. Neither am I sure how to get the second window visible (the line of code I tried from another example isn't working). The second frame that is brought up will give the user options to do things and will actually call another program/class to run on a button clicked within it (the result of one of the options is a long program so I think I need to run it on another thread.). After the program has finished running, the user will have the option to return to the main menu, which would close the second menu (and kill it), or exit the program (and thus kill the main menu and clean everything up). From the main menu, they will also have the option to close the program, where everything will be cleaned up. This is what I have so far:

Main GUI:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


class GUIMain implements ActionListener {

    GUIMain(){
        JFrame jFrm = new JFrame("Data Mining Application");
        jFrm.setSize(800,600);
        jFrm.setLayout(new BorderLayout());
        jFrm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        prepareGUI(jFrm.getContentPane());
        jFrm.pack();
        jFrm.setVisible(true);

    }

    private void prepareGUI(final Container pane){

        JPanel mainPanel = new JPanel(new GridLayout(3,2,50,50));

        JButton b1 = new JButton("Pre-processing");
        b1.addActionListener(this);
        mainPanel.add(b1);


        pane.add(mainPanel,BorderLayout.CENTER);


    }

    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new GUIMain();
            }
        });

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        switch (e.getActionCommand()){
            case "Pre-processing":
                PreProcessingGUI window = new PreProcessingGUI();
                window.getFrame.setVisible(true); //not working
                break;
          // etc
            default:
                break;
        }
    }
}

The class and JFrame that is called:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class PreProcessingGUI implements ActionListener {

    PreProcessingGUI(){
        JFrame jFrm = new JFrame("Pre-processing");
        jFrm.setSize(800,600);
        jFrm.setLayout(new BorderLayout());
        jFrm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        prepareGUI(jFrm.getContentPane());
        jFrm.pack();
    }


    private void prepareGUI(final Container pane) {
    //do stuff
    }


    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    PreProcessingGUI window = new PreProcessingGUI();

                    // Not surewhat to do here either as the program is not recognising the getFrame method
                    //window.getFrame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
    // do stuff
    }
}
marsupials
  • 81
  • 1
  • 8
  • 2
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson May 12 '18 at 10:19
  • Please progress this page to a system-recognized resolution. If the posted answer is satisfactory, please accept it. If you have managed to self-solve, post an answer yourself and accept it. If this question is unresolved, please edit your question to clarify exactly what is broken. – mickmackusa Jan 23 '19 at 00:05

1 Answers1

1

Well I don't work much with Swing but I can help you a bit:

When you try to show the second window in GUIMain.actionPerformed you seem to try to get the frame with a public variable having a method (getFrame).

window.getFrame.setVisible(true);

This variable doesn't exist! It is not defined anywhere. There is no magic here!

You should implement a getFrame() method in PreProcessingGUI and use it in instead of your variable.

In GUIMain.actionPerformed:

window.getFrame().setVisible(true);

In PreProcessingGUI

public class PreProcessingGUI implements ActionListener {
    private JFrame jFrm; //You asssing is as you the constructor
    PreProcessingGUI(){
        jFrm = new JFrame("Pre-processing");
        ...
    }
    public getFrame(){
        return jFrm;
    }
...

In addition to that, I would say you should consider using JDialog (and optionally make it modal) instead of a JFrame.

C.Champagne
  • 5,381
  • 2
  • 23
  • 35
  • Thank you! I got the getFrame method call from some example code on the web and it wasn't clear what it referred to so I assumed it was part of swing. I'll add the method so that will take care of that problem. I don't think I want to get into JDialog at the moment though will consider it for future reference. If anyone else has any idea how I could take care of killing processes at the right time etc, and whether what I've done so far is correct in terms of the setDefaultCloseOperation, that would be fantastic. – marsupials May 12 '18 at 13:59