Java setVisible question

Hello Experts!

Within a JFrame, I would like to create a button that opens up another JFrame. When window two opens, I would like a button to return to the original window. Is there a way to do this in Java? If there is, can you provide some quick sample code? I have always learned best when I can look at sample code.
spogglesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

robthewolfCommented:
try something like this in your JFrame:
 JFrame frame2 = new JFrame();
JButton button = new JButton();
button.setText("MyButton);
button.addActionListener( new ActionListenernew ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
          frame2.setVisible(true);
      });

JButton button2 = new JButton();
button2.addActionListener( new ActionListenernew ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
          frame2.setVisible(false);
      });

frame2.add(button2);


This is the gist of what you need.  Try pluggin it in to your code and see how you get on.  Let me know if you get stuck we can go from there.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
spogglesAuthor Commented:
Here's what I'm trying to do: the callback.java opens up with two buttons. click on either one a window pops up and in the new window you can return to the parent window. What I have "works", but I know it's not the best way to do this. Any ideas?
//Start of callback.java
/*
 * Daniel Williams
 * Chapter 10
 * Assignment 6
 * callback.java
 */

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

public class callback {
	
	static int height = 75;
	static int width = 300;
	
	public static void main(String args[]) {
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (screenSize.width - width)/2; int y = (screenSize.height - height)/2;
		
		callbackFrame f = new callbackFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setSize(width, height);
		f.setLocation(x, y);
		f.setTitle("Parent Window");
		f.setVisible(true);
	}
}

class callbackFrame extends JFrame implements ActionListener {

	static int height = 75;
	static int width = 300;
	
	Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	int x = (screenSize.width - width)/2; int y = (screenSize.height - height)/2;
	
	JButton one = new JButton("Display Child 1");
	JButton two = new JButton("Display Child 2");
	
	public callbackFrame() {
		JPanel contentPanel = new JPanel(new FlowLayout());
		
		contentPanel.add(one);
		contentPanel.add(two);
			one.addActionListener(this);
			two.addActionListener(this);
		
		setContentPane(contentPanel);
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == one) {
			this.setVisible(false);
			
			one childOne = new one();
			childOne.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			childOne.setSize(width, height);
			childOne.setLocation(x, y);
			childOne.setTitle("Child 1");
			childOne.setVisible(true);
			
		}
		if(e.getSource() == two) {
			this.setVisible(false);
			
			two childTwo = new two();
			childTwo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			childTwo.setSize(width, height);
			childTwo.setLocation(x, y);
			childTwo.setTitle("Child 2");
			childTwo.setVisible(true);
		}
	}//end actionPerformed
}//end callbackFrame

//End of callback.java

//Start of one.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class one extends JFrame implements ActionListener {
	JButton one = new JButton("Go Back to Parent");
	
	public one() {
		JPanel contentPanel = new JPanel(new FlowLayout());
		
		contentPanel.add(one);
			one.addActionListener(this);
		
		setContentPane(contentPanel);
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == one) {
			this.setVisible(false);
			
			int height = 75;
			int width = 300;
			
			Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
			int x = (screenSize.width - width)/2; int y = (screenSize.height - height)/2;
			
			callbackFrame f = new callbackFrame();
			f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			f.setSize(width, height);
			f.setLocation(x, y);
			f.setTitle("Parent Window");
			f.setVisible(true);
		}
	}//end actionPerformed
}//end one

//End of one.java

//Start of two.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class two extends JFrame implements ActionListener {
	JButton one = new JButton("Go Back to Parent");
	
	public two() {
		JPanel contentPanel = new JPanel(new FlowLayout());
		
		contentPanel.add(one);
			one.addActionListener(this);
		
		setContentPane(contentPanel);
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == one) {
			this.setVisible(false);
			
			int height = 75;
			int width = 300;
			
			Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
			int x = (screenSize.width - width)/2; int y = (screenSize.height - height)/2;
			
			callbackFrame f = new callbackFrame();
			f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			f.setSize(width, height);
			f.setLocation(x, y);
			f.setTitle("Parent Window");
			f.setVisible(true);
		}
	}//end actionPerformed
}//end two

//End of two.java

Open in new window

0
CEHJCommented:
Child windows are usually better implemented as JDialog
0
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

spogglesAuthor Commented:
I want to build upon the child window. For example, I want to eventually make the first window a logon window and when the user logs on, the log on window will not be visible and the another window becomes visible that has the actual program.
0
robthewolfCommented:
I just looked on google and found this
http://www.daniweb.com/code/snippet216754.html#
It should give you some ideas on how to create a logon window in swing.
0
CEHJCommented:
>>
I want to build upon the child window. For example, I want to eventually make the first window a logon window and when the user logs on, the log on window will not be visible and the another window becomes visible that has the actual program.
>>

You're better off using a CardLayout for that
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.