Click here to Skip to main content
15,437,332 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using eclipse and i am creating a Swing application which have two jframe, one is Login and second is welcome. i want to open the welcome jframe on clicking the submit button of login jframe. both the jframe are within the same package
Posted
Updated 17-Feb-21 19:31pm

Hi Kapilkp,

Same time ago, I did something like that.

In login JFrame you must set the close operation as:

Java
f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);


And when you click on the button, you have dispose this frame:

Java
dispose();



and create a new instance of your class that store the second JFrame

Java
dispose();
JSecondFrame secondFrame = new JSecondFrame();


I hope this helps you

Best regards,
Filipe Marques

--UPDATE--
Java
// MyLogin.java
import javax.swing.*;
import java.awt.event.*;
public class MyLogin {
	private JFrame f = new JFrame("Login");
	private JButton bok = new JButton("OK");
	
	public MyLogin() {
	
		f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		f.getContentPane().add(bok);
		
		bok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				f.dispose();
				new SecondFrame();
			}
		});
		f.setSize(100,100);
		f.setVisible(true);
	}
	
	public static void main(String[] args) {
		new MyLogin();
	}
}

// SecondFrame.java
import javax.swing.*;
public class SecondFrame {
	private JFrame f = new JFrame("Second");
	
	public SecondFrame() {
	
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setSize(300,300);
		f.setVisible(true);
	}
}


The login JFrame is only a button and when you click it, the login window is disposed and the second window appears.
 
Share this answer
 
v2
Comments
Kapilkp 22-Mar-13 7:46am    
hi Filipe Marques, thanks for the help but it will be more helpful if you can tell me where i have to write these code under the ActionPerformed() section or some where else
Filipe Marques 22-Mar-13 8:27am    
Kapilkp, I update my answer with a sample code. It does nothing, it's only to show you. (:
Kapilkp 3-Apr-13 3:22am    
Filipe ,i am using eclipse that is why i am asking because on a notepad it is working but in eclipse it is not working
Filipe Marques 3-Apr-13 17:55pm    
I never used eclipse. I use Netbeans or sometimes I use notepad++ with command line. But this is not relevant. You must have something similar with my code above. The unique difference that I can see is how eclipse create the button, but at the end, the result is the same. Best regards: Filipe
//First JFrame

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

public class Pro implements ActionListener
{
JFrame f1=new JFrame("Log In");
JLabel l1,l2;
JTextField t1,t2;
JButton b1;

Pro()
{
l1=new JLabel("User Name");
l2=new JLabel("Password");
t1=new JTextField(20);
t2=new JTextField(20);
b1=new JButton("OK");

f1.setSize(400,400);
f1.setVisible(true);
f1.setLayout(null);

f1.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
f1.add(l1);
f1.add(t1);
f1.add(l2);
f1.add(t2);
f1.getContentPane().add(b1);

l1.setBounds(0,30,100,30);
t1.setBounds(110,30,100,30);
l2.setBounds(0,70,100,30);
t2.setBounds(110,70,100,30);
b1.setBounds(0,120,100,30);

b1.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{
f1.dispose();
Project1 p2=new Project1();
}

public static void main(String ag[])
{
Pro p1=new Pro();
}
}

//Second JFrame
import javax.swing.*;

public class Project1
{
JFrame f1=new JFrame("Log In1");

Project1()
{
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setVisible(true);
f1.setSize(400,400);
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900