• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 319
  • Last Modified:

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.
0
spoggles
Asked:
spoggles
  • 2
  • 2
  • 2
1 Solution
 
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
 
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

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.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now