How to make a button close a GUI?

Anyways i have yet to figure this out but i’m trying to make a GUI that’ll allow login and such.

Anyways here is methods

private void initialize() {
		frame = new JFrame();
		container = frame.getContentPane();
		container.setLayout(new FlowLayout());
		idLabel = new JLabel("Id:");
		id = new JTextField("", 12);
		passwordLabel = new JLabel("Password:");
		password = new JPasswordField(8);
		password.setEchoChar('*');
		button2 = new JButton("Log in");
		container.add(idLabel);
		container.add(id);
		container.add(passwordLabel);
		container.add(password);
		container.add(button2);
		//container.add(legal, BorderLayout.CENTER);
		frame.setSize( 250,200 );
		frame.setVisible( true );
		frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		button2.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent ae) {
				if (ae.getSource() == button
						&& id.getText().equals("1")
						&& (new String(password.getPassword()))
						.equals("2"))
				container.remove(idLabel);
				container.remove(id);
				container.remove(passwordLabel);
				container.remove(password);
				container.remove(button2);
				secondScreen();
				frame.setSize(300, 300);
			}
		});
	}

	
	public void secondScreen() {
		//frame = new JFrame();
		//container.add(text);
		//layout = new FlowLayout();
		container.remove(idLabel);
		container.remove(id);
		container.remove(passwordLabel);
		container.remove(password);
		container.remove(button2);
		frame.setSize(300, 300);
		frame.setTitle("second screen");
		container = frame.getContentPane();
		JContentPane = new JPanel();
		JContentPane.setLayout(new BorderLayout());
		frame.setResizable(false);
		frame.pack();
		area = new JTextArea();
		area.setEditable(false);
		text = new JTextField("testing");
		frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		frame.setVisible(true);
		button = new JButton("testing");
		button4 = new JButton("testing");
		button3 = new JButton("testing");
		container.add(button, BorderLayout.SOUTH);
		container.add(button3, BorderLayout.EAST);	
		container.add(button4, BorderLayout.WEST);
		container.add(area, BorderLayout.SOUTH);
		button.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent ae) {
				if ( ae.getSource( ) == button )
					sendMessage("Opening webpage.");
					openWebpage();							
			}
		});
		
	}	

yes, it’s really “nooby” as i’m still learning but what i want to do is once you hit the button “login” it’ll close that JFrame and secondScreen(); will open a new one with a different layout, or can someone tell me how to make it switch from a FlowLayout to a BorderLayout without causing any nullpointerexceptions.

None of my books have anything related to my problem and i don’t know what to do exactly. Help is VERY appreciated.

Have a look at CardLayout. CardLayout is, just like its name suggests, a layout where each component that you add is a card. Only 1 card can be shown at a time and you can easily flip between each. So you can add a JPanel that has all your components, and then when you click on Login, it will “flip” to a new card that has a different JPanel. I suggest reading through the Java tutorials on CardLayout to familiarize yourself with it.

On the other hand, if you want the effect of closing a window and opening another window, then it simply requires you to call setVisible(false) on the first one, then setVisible(true) on the new one.

Wow, thank you very much.

Currently going to use cardLayout as it seems it’ll be better to use then setVisible for what i’m planning on doing.

Glad to help :slight_smile:

Also, it’s never safe to be initializing multiple JFrames within the same program, especially if you want them to be able to “Talk”. For your situation, I would really recommend initializing your JFrame, do setVisible(false);, and then use a JDialog component to get the login information. If it’s correct, poof! the JFrame is visible, if not, well say wrong password or something! :smiley:

I tried making an application using JFrames that when certain buttons were pushed spawned more JFrames, but it’s just a bad idea and without the use of static variables, it’s really hard to get them to communicate.

Here is a really good tut on working with very basic Dialog boxes, just make one to grab the login information.

http://download.oracle.com/javase/tutorial/uiswing/components/dialog.html

There is absolutely no technical issue of using multiple JFrames. It just works. No need for hacks or anything.

How well it integrates with you code, depends on the quality of your code.

I am recommending this because with using multiple JFrames, especially if you are retreiving information, you have to use static variables to give one jframe access to another jframe’s variables. I never said it didn’t work, it’s just not practical. With dialogs, you can instantiate a dialog from a jframe class and easily pull information from it.

ToXSiK

Not true.

As said, it highly depends on the quality of your code. If your code requires you to connect JFrames with static variables, then it shows you have a problem structuring your UI classes in an Object Oriented way. I develop in Swing for intranet applications these days, and I only use static for constants.

not really related to JFrame but I find that often very usefull to use a main controller, exemple to help talk from GUI to DataSection or DataSection to GUI

public class SoftController
{
	MyJFrame frame1;
	MyJFrame frame2;
	Data data;
    SoftController()
	{
	this.frame1=new MyJFrame(this)
	}

	public MyJFrame getAJFrame(.....)
	{
	
	}
	
	public MyJFrame getFrame1(.....)
	{
	
	}
	
	public MyJFrame getFrame2(.....)
	{
	
	}
	
	public Data getData(.....)
	{
	
	}
	
	public OtherClass getOtherClass(.....)
	{
	
	}
	
	//Etc....
}

//Base/super class
public class MyJFrame extends JFrame
{
 private SoftController softController;
 
 public MyJFrame(SoftController softController)
 {
  super();
  this.softController=softController;
 }
 
 public SoftController getSoftController()
 {
  return this.softController;
 }
}

public class OtherClass extends OtherClassParent
{
 private SoftController softController;
 
 public OtherClass(SoftController softController)
 {
  super();
  this.softController=softController;
 }
 
 public SoftController getSoftController()
 {
  return this.softController;
 }
}

public class MyJFrame2 extends MyJFrame
{
 public MyJFrame(Controller controller)
 {
  super(controller);
 }

  //Somewhere requiering other JFrame or any other component
  this.getController().getJFrame(...).doSomething();
  this.getController().getOtherClass(...).doSomething();
}

You’re missing my point… But I’m not the one to argue, so I’ll just let it be.

Perhaps the constructor

[quote]JDialog(Frame owner)
[/quote]
Will explain what I am trying to say?


import javax.swing.JFrame;

public class MyFrame extends JFrame {
	
	
	public void handleMessage(Object message){
		// Message passing without statics
	}

}