1

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
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Mac
  • 375
  • 6
  • 10
  • 17
  • 1
    ? `setVisible(false)` ? `dispose()`? You know that have a bunch of JFrames in a program is usually a sign of a newbie program, and often suggests that the design may benefit from change. Instead, why not change the view using a CardLayout. Or if you need a modal window, then use a modal JDialog. – Hovercraft Full Of Eels Nov 22 '14 at 22:01
  • 2
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556). A better design would be to use `CardLayout`, see [How to Use CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) for more details – MadProgrammer Nov 22 '14 at 22:06
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson Nov 23 '14 at 01:05
  • I don't think I fully explained my problem. I don't want multiple jframes. I am trying to get the user input using the gui and then return to the console to display the output in eclipse. – Mac Nov 23 '14 at 17:33

2 Answers2

1

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.

jfdoming
  • 975
  • 10
  • 25
1

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

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366