GUI - How do i make a new frame?

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 4667 Views - Last Post: 13 January 2010 - 05:39 PM Rate Topic: -----

#1 starwok  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 20-October 09

GUI - How do i make a new frame?

Posted 11 January 2010 - 10:04 PM

hi, I was wondering how can i make a new frame after the password is entered(if it's correct).
Like if the user enters the correct password than another brower/frame will open.


This is the program:
import javax.swing.*; 
import java.awt.event.*; 
import java.awt.*; 

public class password { 
private static String password = "pass";
JFrame frame; 
JPanel contentPane; 
JLabel label; 
JButton button; 
JTextField name; 

 
	public static void main(String[] args) { 
	JFrame frame = new JFrame("Password"); 
	frame.setVisible(true); 
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
	frame.setSize(100, 100); 
	
	JLabel label = new JLabel("Enter password"); 
	JPanel panel = new JPanel(); 
	frame.add(panel); 
	JPasswordField pass = new JPasswordField(10); 
	pass.setEchoChar('*'); 
	pass.addActionListener(new AL()); 
  panel.add(label, BorderLayout.WEST); 
  panel.add(pass, BorderLayout.EAST); 
  } 
  
  static class AL implements ActionListener{ 
	public void actionPerformed(ActionEvent e) { 
	  JPasswordField input = (JPasswordField) e.getSource(); 
	  char[] passy = input.getPassword(); 
	  String p = new String(passy); 
	  
	  if (p.equals(password)) { 
		JOptionPane.showMessageDialog(null, "Correct"); 
		password frame = new password(); 
	  } 
	  else 
		JOptionPane.showMessageDialog(null, "Incorrect"); 
	} 
  } 
} 
	  
	



Is This A Good Question/Topic? 0
  • +

Replies To: GUI - How do i make a new frame?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: GUI - How do i make a new frame?

Posted 11 January 2010 - 10:25 PM

Asked so many times....I wrote a Code Snippet on a login frame replaced by the actual Frame after login has been succesfull
Feel free to cut & paste it
Was This Post Helpful? 0
  • +
  • -

#3 starwok  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 20-October 09

Re: GUI - How do i make a new frame?

Posted 11 January 2010 - 10:56 PM

View Postpbl, on 11 Jan, 2010 - 09:25 PM, said:

Asked so many times....I wrote a Code Snippet on a login frame replaced by the actual Frame after login has been succesfull
Feel free to cut & paste it


thanks. I looked at your code, and the output of it.. it looked nice. but I got confused on the 'dummy/father' part. and I'm looking for the part where when password is entered a new frame pops up. I can't find that..... i just need those/that codes so I can add it to my program.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: GUI - How do i make a new frame?

Posted 12 January 2010 - 12:41 AM

View Poststarwok, on 11 Jan, 2010 - 09:56 PM, said:

View Postpbl, on 11 Jan, 2010 - 09:25 PM, said:

Asked so many times....I wrote a Code Snippet on a login frame replaced by the actual Frame after login has been succesfull
Feel free to cut & paste it


thanks. I looked at your code, and the output of it.. it looked nice. but I got confused on the 'dummy/father' part. and I'm looking for the part where when password is entered a new frame pops up. I can't find that..... i just need those/that codes so I can add it to my program.

It works the other way around
First you built the "MasterFrame" but you do not call setVisible(true) into it as you usually do
Then within the constructor of the MasterFrame you create the LoginFrame which has the GUI to display the Username/Password fields
the constructor of the LoginFrame receives as parameter the instance of the MasterFrame
then the constructor of the MasterFrame set the LoginFrame.setVisible(true);

In the actionPerformed() of the LoginFrame, if the username/password are valid. The LoginFrame hides itself, dispose of the resources and set the MasterFrame (its father) setVisible(true)
Was This Post Helpful? 0
  • +
  • -

#5 0x00hex  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 18
  • Joined: 11-January 10

Re: GUI - How do i make a new frame?

Posted 12 January 2010 - 01:17 AM

Hi starwok =) I re-wrote your code making it pop up a new JFrame if you enter the right password.

I am not sure how efficient the code is, its probably not the best way to do it but I tried to keep the code as similar as possible to your original piece.

There are two new methods I added, popUp() and showAccessFrame()
the popUp() method is used in the Action Listener to do a check to see if access has been granted, if it has been granted (by entering the correct password) then it will call the showAccessFrame() method and despose of the (now unneeded) password frame.

In the showAccessFrame() is where you can put the code for the pop up window, I just made a simple JFrame.

I hope this helps you out a little.

P.S I hope its readable!

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

class NewPassword {
	private String setPass = "pass";
	private JFrame passFrame, accessFrame;  
	private JPanel contentPane;
	private JLabel   label;													 
	private boolean accessGranted;
	protected JPasswordField pass;
	
	public NewPassword(){
			accessGranted = false;
			passFrame = new JFrame("Password");
			label	= new JLabel("Password:");
			pass	= new JPasswordField(10);
			contentPane = new JPanel();
			
			passFrame.setSize(300,200);
			passFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			passFrame.add(contentPane);
			passFrame.setVisible(true);
			pass.addActionListener( new PasswordHandler() );
			pass.setEchoChar('');
			contentPane.add( label,BorderLayout.WEST );
			contentPane.add( pass, BorderLayout.EAST );
		}
		
		private void popUp(){
			// This method is used to display either the Denied message or the popup frame.
			
				if( accessGranted == false ) {
					 JOptionPane.showMessageDialog(passFrame, "Access Denied!"); 
					return;
				  }
				else {
					showAccessFrame();
					passFrame.dispose();
					passFrame = null;
				}
		}
		
	   private void showAccessFrame(){

			accessFrame = new JFrame("This is the Pop-up Page");	
			accessFrame.setSize(400,400);
			accessFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			accessFrame.setVisible(true);
			
	   }
	   
	   public static void main(String[] arg){
		   new NewPassword();
	   }
	   
	 //Using an innerclass for the Password Handling
	class PasswordHandler implements ActionListener{
				
		public void actionPerformed(ActionEvent e) { 
						JPasswordField input = (JPasswordField) ( e.getSource() );
						char[] passwrd = input.getPassword();
						String  tmpPass = new String(passwrd);
						
						if( setPass.equals(tmpPass) ) 
								accessGranted = true;
						else 
								accessGranted = false;
						
						popUp();
						
					}
					
			} 
}


Was This Post Helpful? 1
  • +
  • -

#6 starwok  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 20-October 09

Re: GUI - How do i make a new frame?

Posted 12 January 2010 - 11:03 PM

View Post0x00hex, on 12 Jan, 2010 - 12:17 AM, said:

Hi starwok =) I re-wrote your code making it pop up a new JFrame if you enter the right password.

I am not sure how efficient the code is, its probably not the best way to do it but I tried to keep the code as similar as possible to your original piece.

There are two new methods I added, popUp() and showAccessFrame()
the popUp() method is used in the Action Listener to do a check to see if access has been granted, if it has been granted (by entering the correct password) then it will call the showAccessFrame() method and despose of the (now unneeded) password frame.

In the showAccessFrame() is where you can put the code for the pop up window, I just made a simple JFrame.

I hope this helps you out a little.

P.S I hope its readable!

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

class NewPassword {
	private String setPass = "pass";
	private JFrame passFrame, accessFrame;  
	private JPanel contentPane;
	private JLabel   label;													 
	private boolean accessGranted;
	protected JPasswordField pass;
	
	public NewPassword(){
			accessGranted = false;
			passFrame = new JFrame("Password");
			label	= new JLabel("Password:");
			pass	= new JPasswordField(10);
			contentPane = new JPanel();
			
			passFrame.setSize(300,200);
			passFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			passFrame.add(contentPane);
			passFrame.setVisible(true);
			pass.addActionListener( new PasswordHandler() );
			pass.setEchoChar('');
			contentPane.add( label,BorderLayout.WEST );
			contentPane.add( pass, BorderLayout.EAST );
		}
		
		private void popUp(){
			// This method is used to display either the Denied message or the popup frame.
			
				if( accessGranted == false ) {
					 JOptionPane.showMessageDialog(passFrame, "Access Denied!"); 
					return;
				  }
				else {
					showAccessFrame();
					passFrame.dispose();
					passFrame = null;
				}
		}
		
	   private void showAccessFrame(){

			accessFrame = new JFrame("This is the Pop-up Page");	
			accessFrame.setSize(400,400);
			accessFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			accessFrame.setVisible(true);
			
	   }
	   
	   public static void main(String[] arg){
		   new NewPassword();
	   }
	   
	 //Using an innerclass for the Password Handling
	class PasswordHandler implements ActionListener{
				
		public void actionPerformed(ActionEvent e) { 
						JPasswordField input = (JPasswordField) ( e.getSource() );
						char[] passwrd = input.getPassword();
						String  tmpPass = new String(passwrd);
						
						if( setPass.equals(tmpPass) ) 
								accessGranted = true;
						else 
								accessGranted = false;
						
						popUp();
						
					}
					
			} 
}



thanks!
I'm trying to add buttons the the popup page but it isn't showing >.>"
like I did this:

accessFrame1= new JFrame("Anna");
accessFrame1.setSize(400,400);
accessFrame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
accessFrame1.setVisible(true);

but it's not showing... how do i do it.. sorry D:
Was This Post Helpful? 0
  • +
  • -

#7 starwok  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 20-October 09

Re: GUI - How do i make a new frame?

Posted 12 January 2010 - 11:40 PM

I did something like this:

hello there, sorry if I made to much threads but this is very frustrating! So, In this program.. the first page is to enter a password and the password is "pass" so after the user enters the valid password there will be a popup page(new frame) and the frame should have buttons and stuff.. but the buttons aren't showing and the program isn't working properly.. can someone help me modify this ?? you can change the program slightly if that's what it needs inorder to run properly, all i want is buttons that can go to another frame and than that frame still has more buttons , and so on.. thanks!

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

public class NewPassword {
	private String setPass = "pass";
	JFrame passFrame;
	JFrame accessFrame;
	JPanel contentPane;
	JLabel   label;		
	JButton signIn;
	JButton signUp;
	JButton exit;
	boolean accessGranted;
	JPasswordField pass;
	JTextField name; 
	


	public NewPassword(){
	  
			accessGranted = false;
			passFrame = new JFrame("SignIn Page");
			label = new JLabel("Password:");
			pass = new JPasswordField(10);
			contentPane = new JPanel();
			
			passFrame.setSize(300,200);
			passFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			passFrame.add(contentPane);
			passFrame.setVisible(true);
			pass.addActionListener( new PasswordHandler() );
			pass.setEchoChar('*');
			contentPane.add( label,BorderLayout.SOUTH );
			contentPane.add( pass, BorderLayout.SOUTH);
		}
		
		private void popUp(){
			// This method is used to display either the Denied message or the popup frame.
			
				if( accessGranted == false ) {
					 JOptionPane.showMessageDialog(passFrame, "Incorrect password"); 
					return;
				  }
				else {
					showAccessFrame();
					passFrame.dispose();
					passFrame = null;
				}
		}
		
	   private void showAccessFrame(){

			accessFrame = new JFrame("Sign In Page");	
			accessFrame.setSize(400,400);
			accessFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			accessFrame.setVisible(true);
	   } 
	   
	   private void showButtonFrame() { 
			
			signIn = new JButton("Sign In"); 
			contentPane.add(signIn); 
			signIn.setAlignmentX(JButton.CENTER_ALIGNMENT); 
			contentPane.add(Box.createRigidArea(new Dimension(0, 15))); 
			signIn.setActionCommand("Sign In"); 
			
			signUp = new JButton("Sign Up");
			contentPane.add(signUp);
			signUp.setAlignmentX(JButton.CENTER_ALIGNMENT);
			contentPane.add(Box.createRigidArea(new Dimension(0, 15)));
			signUp.setActionCommand("Sign Up");

	
		   exit = new JButton("Exit");
		   contentPane.add(exit);
		   exit.setAlignmentX(JButton.CENTER_ALIGNMENT);
		   contentPane.add(Box.createRigidArea(new Dimension(0, 15)));
		   exit.addActionListener(new Terminate());
	
	   }
	   
	   public static void main(String[] arg){
		   new NewPassword();
	   }
	   
	   class Terminate implements ActionListener {   
		 public void actionPerformed(ActionEvent event) {
		 System.exit(0);
	   
	   
	}
	   
	 //Using an innerclass for the Password Handling
	class PasswordHandler implements ActionListener{
				
		public void actionPerformed(ActionEvent e) { 
						JPasswordField input = (JPasswordField) ( e.getSource() );
						char[] passwrd = input.getPassword();
						String  tmpPass = new String(passwrd);
						
						if( setPass.equals(tmpPass) ) 
								accessGranted = true;
						else 
								accessGranted = false;
						
						popUp();
						
					}
					
			} 
}



but the buttons are not showing in that new popup page....
Was This Post Helpful? 0
  • +
  • -

#8 0x00hex  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 18
  • Joined: 11-January 10

Re: GUI - How do i make a new frame?

Posted 13 January 2010 - 03:25 AM

Hey man, me again :D

Quote

From my quick glance over your code there are two things I noticed:

1. At no point in the code can I actually see a call to the
showButtonFrame() method. What you need to do there is
change the showAccessFrame() method call in the popUp()
method to showButtonFrame().

2. In the showButtonFrame() method I see you are trying to
add the contentPane JPanel to you new window. I suggest you just
create a new JPanel for this second window just so you dont have
all the stuff from the password frame on your new frame.


Thats what I said in your second post =)
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,986
  • Joined: 20-September 08

Re: GUI - How do i make a new frame?

Posted 13 January 2010 - 04:30 AM

There's no need to use more than one frame - just use a CardLayout and show the 'real' content pane only after a successful login
Was This Post Helpful? 0
  • +
  • -

#10 starwok  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 20-October 09

Re: GUI - How do i make a new frame?

Posted 13 January 2010 - 01:35 PM

View Post0x00hex, on 13 Jan, 2010 - 02:25 AM, said:

Hey man, me again :D

Quote

From my quick glance over your code there are two things I noticed:

1. At no point in the code can I actually see a call to the
showButtonFrame() method. What you need to do there is
change the showAccessFrame() method call in the popUp()
method to showButtonFrame().

2. In the showButtonFrame() method I see you are trying to
add the contentPane JPanel to you new window. I suggest you just
create a new JPanel for this second window just so you dont have
all the stuff from the password frame on your new frame.


Thats what I said in your second post =)



yea sorry. I got it now. but now it doesn't even show up on the second pop up... like on the second pop up i did, for example.. more buttons but it doesn't show up on the frame.. it's just a black frame. hopefully... this will be my last question. you helped alot.
this is my program (I bolded the part where I added the new buttons to the new popup page, but it still shows black on the popup page.. how come? >.>"

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

public class RestaurantFirstPage1 {
  final static String LABEL_TEXT = "Welcome to the restaurant rating website";
  JFrame frame;
  JFrame firstFrame;
  JFrame secondFrame;
  JPanel contentPane;
  JLabel label;
  JButton signIn;
  JButton signUp;
  JButton exit;
  JButton exit2; 
  
  public RestaurantFirstPage1() {
	frame = new JFrame("Restaurant First Page");
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
	contentPane = new JPanel();
	contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
	contentPane.setBorder(BorderFactory.createEmptyBorder(50,20,30,20));
	contentPane.setBackground(Color.white); 
	
	label = new JLabel(LABEL_TEXT);
	label.setForeground(Color.black); 
	label.setAlignmentX(JLabel.CENTER_ALIGNMENT);
	label.setBorder(BorderFactory.createEmptyBorder(20,50,20,50));
	contentPane.add(label);
	
	signIn = new JButton("Sign In");   
	contentPane.add(signIn);
	signIn.setAlignmentX(JButton.CENTER_ALIGNMENT);
	contentPane.add(Box.createRigidArea(new Dimension(0, 15)));
	signIn.setActionCommand("Sign In");
	signIn.addActionListener(new NextPage()); 
						  
	signUp = new JButton("Sign Up");
	contentPane.add(signUp);
	signUp.setAlignmentX(JButton.CENTER_ALIGNMENT);
	contentPane.add(Box.createRigidArea(new Dimension(0, 15)));
	signUp.setActionCommand("Sign Up");
	signUp.addActionListener(new NextPage());
	
	exit = new JButton("Exit");
	contentPane.add(exit);
	exit.setAlignmentX(JButton.CENTER_ALIGNMENT);
	contentPane.add(Box.createRigidArea(new Dimension(0, 15)));
	exit.addActionListener(new Terminate());
  
	frame.setContentPane(contentPane);
	
	frame.pack();
	frame.setVisible(true);
  }

	private static void runGUI() {
	  JFrame.setDefaultLookAndFeelDecorated(true);
	  
	  RestaurantFirstPage1 firstPage = new RestaurantFirstPage1();
	}
	
	class NextPage implements ActionListener { 

	public void actionPerformed(ActionEvent event) {
	  String eventName = event.getActionCommand();
	  
	  if(eventName.equals("Sign In")) {
[b]		firstFrame(); 
		frame.dispose(); 
		frame = null; 
		
	  }
	  else if(eventName.equals("Sign Up")) {
		secondFrame(); 
		frame.dispose(); 
		frame = null; 

	  } 
	} 
	
	private void firstFrame() { 
	  firstFrame = new JFrame("SignIn"); 
	  firstFrame.setSize(350, 320); 
	  firstFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
	  firstFrame.setVisible(true);	 
	  

	} 
	private void secondFrame() { 
	  secondFrame = new JFrame("SignUp"); 
	  secondFrame.setSize(350, 320); 
	  secondFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
	  secondFrame.setVisible(true); [/b] 
	  
	}	
	}
	
	class Terminate implements ActionListener {   
	  public void actionPerformed(ActionEvent event) {
	  System.exit(0);
	}
	}
	

	public static void main(String[] args) {
	  javax.swing.SwingUtilities.invokeLater(new Runnable() {
		public void run() {
		  runGUI();
		}
	  });
	}
  }


Was This Post Helpful? 0
  • +
  • -

#11 0x00hex  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 18
  • Joined: 11-January 10

Re: GUI - How do i make a new frame?

Posted 13 January 2010 - 03:30 PM

Hey, I don't have a lot of time (need to head out). I would suggest the same as what g00se suggested, use the CardLayout (http://java.sun.com/docs/books/tutorial/uiswing/layout/card.html) or if you really want to use seperate JFrames break the program up into Separate classes ie:

Abstract Window (extends JFrame)
- FirstWindow (extends Window)
- SecondWindow (extends Window)

When I get the time later today I will look over you code and help you out =)
Was This Post Helpful? 0
  • +
  • -

#12 starwok  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 20-October 09

Re: GUI - How do i make a new frame?

Posted 13 January 2010 - 03:42 PM

View Post0x00hex, on 13 Jan, 2010 - 02:30 PM, said:

Hey, I don't have a lot of time (need to head out). I would suggest the same as what g00se suggested, use the CardLayout (http://java.sun.com/docs/books/tutorial/uiswing/layout/card.html) or if you really want to use seperate JFrames break the program up into Separate classes ie:

Abstract Window (extends JFrame)
- FirstWindow (extends Window)
- SecondWindow (extends Window)

When I get the time later today I will look over you code and help you out =)


thanks. some ppl recommended the cardlayout to me, but i want the buttons to be like in a column kinda thing... and I'm not good with GUI so i'm just trying to keep everything in one file, so i don't get confused.
Was This Post Helpful? 0
  • +
  • -

#13 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: GUI - How do i make a new frame?

Posted 13 January 2010 - 03:46 PM

I too, along with g00se, suggest using a CardLayout. It's easy and concise.

Here is a tutorial though, so that you can better understand it: http://java.sun.com/...ayout/card.html

I also advise you to look in the way that pbl does with his multiple panels, but yes, they get confusing, so CardLayout might be best for you.
Was This Post Helpful? 0
  • +
  • -

#14 starwok  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 20-October 09

Re: GUI - How do i make a new frame?

Posted 13 January 2010 - 03:54 PM

I can't use the cardlayout because I'm making this kind of "website thing", and later in the program(once i can figure out how too) After the use clicks the "Sign In" button, the second frame will popup(which I already have) and than it will have a label and textfield asking "Enter username", "Enter password" and if it's correct a THIRD frame will popup... and then the rest i know how to do. That's all I/m having trouble with is the pop up frames displaying more buttons(which it's not displaying in my program, dunno why) + the password field. Matybe just after the "User enter password" section, I will do the cardlayout...

[b]So can anyone fix this problem? /b]

P.s. It would be even better if you could help me add a password,username field for me in that secondFrame

This post has been edited by starwok: 13 January 2010 - 03:55 PM

Was This Post Helpful? 0
  • +
  • -

#15 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,986
  • Joined: 20-September 08

Re: GUI - How do i make a new frame?

Posted 13 January 2010 - 04:04 PM

Here's an example using a CardLayout

http://www.technojee...with-cardlayout

This post has been edited by g00se: 13 January 2010 - 04:45 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2