4 Replies - 2618 Views - Last Post: 03 May 2012 - 06:22 AM

#1 pbl   User is offline

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

Reputation: 8380
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Hidding a frame and showing another

Posted 28 September 2009 - 08:07 PM

Description: Just cut & paste and addapt it to your needs The Login class will be to be adapted to validate the username and the password The idea is to show how to swap from from JFrame to another oneAsked many times in this forum how to make a login form and then show another JFrame when the user input is correct
import java.awt.*;
import javax.swing.*;

public class MasterFrame extends JFrame {

	final static String[] labelStr = {"Just a test", "Dummy label", "to fill", "the form"};
	
	// this the master frame... we just fill it with labels an button to show how it works
	MasterFrame() {
		super("MasterFrame");
		
		// build a dummy panel with dummy labels just to fill the GUI
		JPanel panel = new JPanel(new GridLayout(labelStr.length, 2));
		for(int i = 0; i < labelStr.length; i++) {
			panel.add(new JLabel(labelStr[i]));
			panel.add(new JButton("Button " + i));
		}
		add(panel);
		setSize(400, 200);
		// create and show the Login form
		new Login(this);
	}
	
	// to test the whole thing
	public static void main(String[] args) {
		new MasterFrame();
	}
}


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

class Login extends JFrame implements ActionListener
{
	final JButton submitBtn;
	final JPanel panel;
	final JLabel usernameLabel, passwordLabel;
	final JTextField  usernameField;
	final JPasswordField passwordField;
	
	JFrame father;
	
	Login(JFrame masterForm)
	{
		super("Login form");
		father = masterForm;
		usernameLabel = new JLabel("Username:");
		usernameField = new JTextField(15);

		passwordLabel = new JLabel("Password:");
		passwordField = new JPasswordField(15);

		submitBtn =new JButton("SUBMIT");
		submitBtn.addActionListener(this);
		
		panel = new JPanel(new GridLayout(3,2));
		panel.add(usernameLabel);
		panel.add(usernameField);
		panel.add(passwordLabel);
		panel.add(passwordField);
		panel.add(submitBtn);
		// to fill the GridLayout
		panel.add(new JLabel(""));
		add(panel,BorderLayout.CENTER);

		setTitle("LOGIN FORM");
		setSize(300,100);
		setVisible(true);
	}
	
	public void actionPerformed(ActionEvent ee)
	{
		// validate here the username from usernameField.getText()
		// and the password form passwordField.getPassword();
		// if valid
		this.setVisible(false);				// hide myself
		father.setVisible(true);			// show my father
		this.dispose();						// clean my resource
		
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Hidding a frame and showing another

#2 pbl   User is offline

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

Reputation: 8380
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Hidding a frame and showing another

Posted 28 September 2009 - 08:07 PM

Description: Just cut & paste and addapt it to your needs The Login class will be to be adapted to validate the username and the password The idea is to show how to swap from from JFrame to another oneAsked many times in this forum how to make a login form and then show another JFrame when the user input is correct
import java.awt.*;
import javax.swing.*;

public class MasterFrame extends JFrame {

	final static String[] labelStr = {"Just a test", "Dummy label", "to fill", "the form"};
	
	// this the master frame... we just fill it with labels an button to show how it works
	MasterFrame() {
		super("MasterFrame");
		
		// build a dummy panel with dummy labels just to fill the GUI
		JPanel panel = new JPanel(new GridLayout(labelStr.length, 2));
		for(int i = 0; i < labelStr.length; i++) {
			panel.add(new JLabel(labelStr[i]));
			panel.add(new JButton("Button " + i));
		}
		add(panel);
		setSize(400, 200);
		// create and show the Login form
		new Login(this);
	}
	
	// to test the whole thing
	public static void main(String[] args) {
		new MasterFrame();
	}
}


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

class Login extends JFrame implements ActionListener
{
	final JButton submitBtn;
	final JPanel panel;
	final JLabel usernameLabel, passwordLabel;
	final JTextField  usernameField;
	final JPasswordField passwordField;
	
	JFrame father;
	
	Login(JFrame masterForm)
	{
		super("Login form");
		father = masterForm;
		usernameLabel = new JLabel("Username:");
		usernameField = new JTextField(15);

		passwordLabel = new JLabel("Password:");
		passwordField = new JPasswordField(15);

		submitBtn =new JButton("SUBMIT");
		submitBtn.addActionListener(this);
		
		panel = new JPanel(new GridLayout(3,2));
		panel.add(usernameLabel);
		panel.add(usernameField);
		panel.add(passwordLabel);
		panel.add(passwordField);
		panel.add(submitBtn);
		// to fill the GridLayout
		panel.add(new JLabel(""));
		add(panel,BorderLayout.CENTER);

		setTitle("LOGIN FORM");
		setSize(300,100);
		setVisible(true);
	}
        public void actionPerformed(ActionEvent ee)
	{
		// validate here the username from usernameField.getText()
		// and the password form passwordField.getPassword();
		// ususally that would be done from a database but here 
		// (for testing purpose) we will hardcode the value
		// username must be "abc" password "xyz"
		
		if(usernameField.getText().equals("abc")) {
            // getPassword returns an array of char[] we have to make it a String
			if(new String(passwordField.getPassword()).equals("xyz")) {
				this.setVisible(false);				// hide myself
				father.setVisible(true);			// show my father
				this.dispose();						// clean my resource
				return;
			}
		}
		// send an option pane that does not match
		JOptionPane.showMessageDialog(this, "Sorry wrong Username Passwordn Try again");
		
	}	
}


Was This Post Helpful? 0
  • +
  • -

#3 circuspeanuts   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 224
  • Joined: 11-April 08

Re: Hidding a frame and showing another

Posted 18 November 2009 - 01:24 PM

Thank you very much! This has helped me progress greatly!
Was This Post Helpful? 0
  • +
  • -

#4 kharenverona   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 01-October 10

Re: Hidding a frame and showing another

Posted 21 February 2011 - 07:10 PM

thank you!this was of great help to me..but are there any way on how i can combine the jrame and action listener so that i won't make another file?
Was This Post Helpful? 0
  • +
  • -

#5 oparahenryy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 27-April 12

Re: Hidding a frame and showing another

Posted 03 May 2012 - 06:06 AM

wat if i want to make the buttons to work or open another frame.thanks.
Was This Post Helpful? 0
  • +
  • -

#6 oparahenryy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 27-April 12

Re: Hidding a frame and showing another

Posted 03 May 2012 - 06:22 AM

i want to one of the buttons to work.how can i do dat?please help me
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1