I am working on an application that has a menu window with 2 buttons for menu options. I would like the application to function in the following way. When a menu option button is clicked, the menu window disappears and the window for the menu selection appears. When the close button for the active window is clicked, the menu window re-opens.

At this time, I have my menu window class and one option window ("Add New Record") coded and saved into seperate files. The add new record class is imported into the menu class. I was planning on keeping each window class in a seperate file, thinking that would be a "cleaner" way of orgainizing my application code, but it may be part of my problem.

The menu window opens fine, if the 'add new record' button is clicked the menu window goes away via, .setVisible(false), and the add new record window pops up. But, I can't figure out how to get the menu window to re-open when clicking the 'close' button on the add new record window.

When you press close you have to call the setVisible(true) of the original Menu Window and the setVisible(false) of the add record window. Probably you don't have access to the Menu Window from the add window. Try this
In the Menu Window when you call add-window, have it take as parameter this Menu Window
I don't khow you cod but this is how I do it:

Assume you have the two classes and constructors:

MenuWindow mw = new MenuWindow();
and
AddWindow aw = new AddWindow();
Try implemeting sth like this:

MenuWindow mw = new MenuWindow(AddWindow addW);
and
AddWindow aw = new AddWindow(MenuWindow menuW);

So in Menu when you click add new record:

AddWindow aw = new AddWindow(this);
this.setVisible(false);

And in add window when you close:

menuW.setVisible(true); //whatever variable you are using
this.setVisible(false);

Here is my code for both classes. Keep in mind that the menu window is in one file, and the other window(s) are each in seperate files. I'm wondering if that may be a mistake to do it that way. I tried what you suggested but got errors regarding constructors and missing variables. If it matters, I am using TextPad as the editor.

Here's my "main/menu" class

import windowpackage.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class CompanyTrucker extends JFrame
{//start class CompanyTrucker

               private static final int WIDTH = 475;
               private static final int HEIGHT = 100;

              private JLabel lblMenuSelection, lblBlank1, lblBlank2, lblBlank3,lblBlank4,lblBlank5;
	private JButton btnNewRecord, btnReport, btnExit;
	private btnNewRecordHandler nrbHandler;
	private btnReportHandler rptHandler;
	private btnExitHandler ebHandler;
	private static CompanyTrucker menuWindow;
	private PerMileWindow window;




	public CompanyTrucker()
	{
		//instantiate labels
		lblMenuSelection = new JLabel("What would you like to do?");
		lblBlank1 = new JLabel();
		lblBlank2 = new JLabel();
		lblBlank3 = new JLabel();
		lblBlank4 = new JLabel();
		lblBlank5 = new JLabel();


		//instantiate buttons
		//New Record button
		btnNewRecord = new JButton("New Record");
		nrbHandler = new btnNewRecordHandler();
		btnNewRecord.addActionListener(nrbHandler);

		//Report button
		btnReport = new JButton("Report");
		rptHandler = new btnReportHandler();
		btnReport.addActionListener(rptHandler);

		//Exit button
		btnExit = new JButton("Exit");
		ebHandler = new btnExitHandler();
		btnExit.addActionListener(ebHandler);

		//instantiate container
		Container pane = getContentPane();

		//container size - 3 rows, 3 columns
		pane.setLayout(new GridLayout(3,3));

		//add buttons and labels to window
		pane.add(lblMenuSelection);
		pane.add(lblBlank1);
		pane.add(lblBlank2);
		pane.add(lblBlank3);
		pane.add(lblBlank4);
		pane.add(lblBlank5);
		pane.add(btnNewRecord);
		pane.add(btnReport);
		pane.add(btnExit);

		//title of window
		setTitle("Company Trucker - Main Menu");

		//size the window
		setSize ( WIDTH, HEIGHT);

		//make window visible
		setVisible(true);

		//action when window is closed
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
			
                               //centers window on screen
		setLocationRelativeTo(null);
		}

		//code for New Record button event hanlder
		private class btnNewRecordHandler implements ActionListener
		{
		        public void actionPerformed(ActionEvent e)
			{
			    setVisible(false);
			    PerMileWindow window = new PerMileWindow();
			}
		}

		//code for Report Button
		private class btnReportHandler implements ActionListener
		{
			public void actionPerformed(ActionEvent e)
			{
			}
		}

		//code for exit button even handler
		private class btnExitHandler implements ActionListener
		{
			public void actionPerformed(ActionEvent e)
			{
				System.exit(0);
			}
		}

		public static void main(String args[])
		{
			CompanyTrucker menuWindow = new CompanyTrucker();

		}


}//end class CompanyTrucker

Here's my add record class, aka PerMileWindow

//*************************************************
//02-04-08

package windowpackage;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class PerMileWindow extends JFrame
{
	private static final int WIDTH = 250;
	private static final int HEIGHT = 450;
	private JLabel lblTripNumber, lblDate, lblBeginLocation, lblEndLocation, lblType,
				   lblBeginMileage, lblEndMileage, lblTotalMileage, lblRate, lblAmountDue;

	private JLabel lblBlank1, lblBlank2, lblBlank3, lblBlank4, lblBlank5,
				   lblBlank6, lblBlank7;

	private JTextField txtTripNumber, txtDate, txtBeginLocation, txtEndLocation, txtType,
				       txtBeginMileage, txtEndMileage, txtTotalMileage, txtRate, txtAmountDue;

	private JButton btnSaveRecord, btnExit;


	//private JButton btnCalculate, btnExit;
	//private btnCalculateHandler cbHandler;
	private btnExitHandler ebHandler;


	public PerMileWindow()
	{
		//instantiate labels, right justified
		lblTripNumber = new JLabel("Trip Number",SwingConstants.RIGHT);
		lblDate = new JLabel("Date",SwingConstants.RIGHT);
		lblBeginLocation = new JLabel("Beginning Location",SwingConstants.RIGHT);
		lblEndLocation = new JLabel("Ending Location",SwingConstants.RIGHT);
		lblType = new JLabel("Type",SwingConstants.RIGHT);
		lblBeginMileage = new JLabel("Beginning Mileage",SwingConstants.RIGHT);
		lblEndMileage = new JLabel("Ending Mileage",SwingConstants.RIGHT);
		lblTotalMileage = new JLabel("Total Mileage",SwingConstants.RIGHT);
		lblRate = new JLabel("Rate Per Mile",SwingConstants.RIGHT);
		lblAmountDue = new JLabel("Amount Due",SwingConstants.RIGHT);


		//instantiate blanks
		lblBlank1 = new JLabel("");
		lblBlank2 = new JLabel("");
		lblBlank3 = new JLabel("");
		lblBlank4 = new JLabel("");
		lblBlank5 = new JLabel("");
		lblBlank6 = new JLabel("");
		lblBlank7 = new JLabel("");

		//Calculate button
		btnSaveRecord= new JButton("Save Record");
		//cbHandler = new btnCalculateHandler();
		//btnCalculate.addActionListener(cbHandler);

		//Exit button
		JButton btnExit = new JButton("Exit");
		ebHandler = new btnExitHandler();
		btnExit.addActionListener(ebHandler);

		//instantiate text boxes
		txtTripNumber = new JTextField("");
		txtDate = new JTextField("");
		txtBeginLocation = new JTextField("");
		txtEndLocation = new JTextField("");
		txtType =new JTextField("");
		txtBeginMileage =new JTextField("");
		txtEndMileage = new JTextField("");
		txtTotalMileage = new JTextField("");
		txtRate = new JTextField("");
		txtAmountDue = new JTextField("");

		//buttons

		//instantiate container
		Container pane = getContentPane();

		//container size - 4 rows, 5 columns
		pane.setLayout(new GridLayout(11,2));

		pane.add(lblTripNumber);
		pane.add(txtTripNumber);
		pane.add(lblDate);
		pane.add(txtDate);
		pane.add(lblBlank1);
		pane.add(lblBlank2);

		pane.add(lblBeginMileage);
		pane.add(txtBeginMileage);
		pane.add(lblEndMileage);
		pane.add(txtEndMileage);
		pane.add(lblRate);
		pane.add(txtRate);

		pane.add(lblBlank3);
		pane.add(lblBlank4);
		pane.add(lblTotalMileage);
		pane.add(txtTotalMileage);

		pane.add(lblAmountDue);
		pane.add(txtAmountDue);

		pane.add(lblBlank6);
		pane.add(lblBlank7);

		pane.add(btnSaveRecord);
		pane.add(btnExit);


		//title of window
		setTitle("Trip Type:  Per Mile");

		setSize ( WIDTH, HEIGHT);

		//make window visible
		setVisible(true);

		//action when window is closed
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);

		//centers window on screen
		setLocationRelativeTo(null);

	}

	//code for exit button handler
		public class btnExitHandler implements ActionListener
		{
			public void actionPerformed(ActionEvent e)
			{
				setVisible(false);
			}
	}

}

what were the errors, at which line and where is the code with the so called errors? If you say you have something like missing variable I think it is because I told you to declare you constructors with arguments so one page will know where to go next and you use empty constructors:
I didn't read all of the code but if this is where you are opening the other window then:

public void actionPerformed(ActionEvent e)
{
setVisible(false);
PerMileWindow window = new PerMileWindow(this);
//the constructor of PerMileWindow will have to change of course.
//you should add a CompanyTrucker attribute in your PerMileWindow  class
}
public class PerMileWindow extends JFrame
{
   CompanyTrucker trucker=null;
   //the rest or you declarations   

  public PerMileWindow(CompanyTrucker ct) {
    trucker = ct;
   //the rest of your code
  } 
}

In PerMileWindow class:

//code for exit button handler
public class btnExitHandler implements ActionListener
{
  public void actionPerformed(ActionEvent e)
  {
    this.setVisible(false);
    trucker.setVisible(true);
  }
}

Here are the errors when the above code is added...

C:\Documents and Settings\Allan\My Documents\HomeWork\2007-2008\Winter\Java I\Final Project\windowpackage\PerMileWindow.java:12: cannot find symbol
symbol : class CompanyTrucker
location: class windowpackage.PerMileWindow
CompanyTrucker trucker = null;
^
C:\Documents and Settings\Allan\My Documents\HomeWork\2007-2008\Winter\Java I\Final Project\windowpackage\PerMileWindow.java:32: cannot find symbol
symbol : class CompanyTrucker
location: class windowpackage.PerMileWindow
public PerMileWindow(CompanyTrucker ct)
^
2 errors

The PerMileWindow class is being imported into the CompanyTrucker class. I think that's why this didn't work. Could it be that placing the PerMileWindow code into a different file and importing it is the root of my problem?

If it cannot locate the CompanyTrucker, just imported. And if you cannot figure that out then why are you trying to create a GUI when you don't even know the simple stuff about importing and declaring packages and classes.

Well, so much for friendly help...

You don't need to get snooty because your solutions don't work...

The problem is not importing, I did that. The problem is not being able to refernece the "main" class in the classes that are packaged and being imported into the "main" class. I was taught to place the "main" class in a folder and the package you create to be imported is to be located in a subfolder of the folder that contains the "main" class. In other words the package is one folder down from the "main" class. Maybe that's not correct, I don't know, I was just going by what I was taught, and importing the package into the main class worked fine.

But anyway, I made all the imported classes inner classes of the "main" class, and now the application works. So, thanks for the attempted help. Though maybe next time you can be a little nicer.

Sorry about my reply and sorry about the way I talked. I was not in the right mood when I was answering, I had a problem with something else and I wasn't very calm. Sorry again and I will be nicer next time.

You need to do a couple of things here:

1) change your PerMileWindow.java class as this:

public class PerMileWindow extends JFrame [B]implements ActionListener[/B]{
    [B]private CompanyTrucker ct = new CompanyTrucker();[/B]
    // rest of code...

    [B]btnSaveRecord.addActionListener(this);[/B]
    // rest of code...

    [B]public void actionPerformed (ActionEvent evt) {
            // you can handle all your events from other buttons here too (you will need to add action listener to all your buttons...)
            if (evt.getSource() == btnSaveRecord) {
                this.setVisible(false);
                ct.setVisible(true);
            } else if(evt.getSource() == btnExist) {
                System.out.exit(0);
            } else if(evt.getSource() == btnBlahBlah) {
                // do something else...
            }
      }[/B]
}

2) in your CompanyTrucker.java class make these changes:

// move the "setVisible(true) to main() method, so you main now looks like:
    public static void main(String args[])
    {
         CompanyTrucker menuWindow = new CompanyTrucker();
         menuWindow.setVisible(true);
     }

Try these changes, and it will work.

Obviousely your both classes should be in the same package and you should import your package at the very first line of your "imports"

Didn't he already get his application to work?

But anyway, I made all the imported classes inner classes of the "main" class, and now the application works.

Didn't he already get his application to work?

:) You are right.

I didn't read the entire post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.