Mac
Mac

Reputation: 375

Hiding JFrame when button is clicked without stopping program

I have a jframe as part of a program where the user will populate an arraylist by pressing the add buttons. When they are finished populating the arraylist I want them to click the done button to hide the jframe while the program continues to run.

Is there a way to do this. Initially I was using System.exit(0) but this seems to terminate the program.

public class Street extends JFrame {
    private static final Random randomNumbers = new Random();
    private ArrayList<Vehicle> vehicles = new ArrayList<Vehicle>();

    private JLabel messageJLabel; // displays vehicle that was added
    private JButton addBicycleJButton;
    private JButton addCarJButton;
    private JButton doneJButton;
    private Color background; // background color of application

public Street() {

    super("Street Simulation");

    background = Color.LIGHT_GRAY;

    messageJLabel = new JLabel("No vehicles added.");

    addBicycleJButton = new JButton("Add Bicycle");
    addBicycleJButton.addActionListener(

    new ActionListener() // anonymous inner class
            {
                public void actionPerformed(ActionEvent e) {
                    background = Color.LIGHT_GRAY;
                    Bicycle b = new Bicycle(2, 0);
                    vehicles.add(b);
                    messageJLabel.setText(b.toString()
                            + " added to vehicles");
                    repaint();

                } // end method actionPerformed
            } // end anonymous inner class
            ); // end call to addActionListener
    addCarJButton = new JButton("Add Car");
    addCarJButton.addActionListener(

    new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            background = Color.LIGHT_GRAY;
            Car c = new Car(4, 0);
            vehicles.add(c);
            messageJLabel.setText(c.toString() + " added to vehicles");
            repaint();

        } // end method actionPerformed
    } // end anonymous inner class
            );// end call to addActionListener

    doneJButton = new JButton("Done");
    doneJButton.addActionListener(

    new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // code to exit goes here
            background = Color.LIGHT_GRAY;

            //I would like to have the jframe running in the background after this button is pushed.

            // System.exit(0);
            repaint();

        } // end method actionPerformed
    } // end anonymous inner class
            );// end call to addActionListener

    setLayout(new FlowLayout());
    add(addBicycleJButton);
    add(addCarJButton);
    add(doneJButton);

    add(messageJLabel);

} // end street constructor

Upvotes: 1

Views: 697

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347184

See The Use of Multiple JFrames, Good/Bad Practice? for reasons why you shouldn't avoid you current design/thinking.

A better design would be to use CardLayout, see How to Use CardLayout for more details

Upvotes: 1

jfdoming
jfdoming

Reputation: 975

The JFrame class has the method setVisible(boolean) which can be used for changing the visibility of a JFrame. For example, this.setVisible(false); to hide it.

Upvotes: 1

Related Questions