10 Replies - 1815 Views - Last Post: 19 December 2009 - 11:56 AM Rate Topic: -----

#1 dustinm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 30-November 09

How do I close one window, then open another in a client class?

Posted 18 December 2009 - 09:50 AM

This will probably come out a bit confusing, but I'll try my best. I'm writing a program for an assignment. The program is basically going to "simulate" a train and it's passengers. I have a client, called TrainRidePanel, which will use a GUI and will demonstrate the methods in the class Train, which encapsulates a train full of passengers, which are objects of the class Passenger. Basically the Train constructor will create an ArrayList of Passenger objects, and work off of that for it's methods.

Now, my problem is that for this project, one of the things we were suppsoed to show, is some kind of login. I was going to use a class from the book and modify it to suit what I need, but I'm running into problems and I don't know if I'm going about this in the right way. Here's the Login class after doing the editing that I needed to do to it:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JOptionPane;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Login extends JFrame
{
 private Container contents;
 private JLabel idLabel, passwordLabel, message;
 private JTextField id;
 private JPasswordField password;
 private JTextArea legal;
 private int numLoginAttempts = 0;
 private boolean loginFlag = false;
 private boolean maxAttemptsFlag = false;

 // Constructor
 public Login( )
 {
  super( "Login Screen" );
  login( );
  setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
 }
 
 public void login( )
 {
  contents = getContentPane( );
  contents.setLayout( new FlowLayout( ) );

  idLabel = new JLabel( "Enter id" ); // label for ID
  id = new JTextField( "", 12 );	  // instantiate ID text field

  passwordLabel = new JLabel( "Enter password" ); // password label
  password = new JPasswordField( 8 ); // instantiate password field
  password.setEchoChar( '?' );		// set echo character to '?'

  message = new JLabel( "Log in above. Maximum of 3 login attempts allowed." );  // label to hold messages

  // instantiate JTextArea with legal warning
  legal = new JTextArea( "Warning: Any attempt to illegally\n"
		   + "log in to this server is punishable by law.\n"
		   + "This corporation will not tolerate hacking,\n"
		   + "virus attacks, or other malicious acts." );
  legal.setEditable( false );		// disable typing in this field

  // add all components to the window
  contents.add( idLabel );
  contents.add( id );
  contents.add( passwordLabel );
  contents.add( password );
  contents.add( message );
  contents.add( legal );

  // instantiate event handler for the text fields
  TextFieldHandler tfh = new TextFieldHandler( );

  // add event handler as listener for ID and password fields
  id.addActionListener( tfh );
  password.addActionListener( tfh );

  setSize( 250,200 );
  setVisible( true );  
  
 }
 
 /** getLoginFlag Method
  *	@return boolean value, true if login is good, false if not
  */
  public boolean getLoginFlag( )
  {
	  return loginFlag;
  }
 
 /** getAttemptsFlag Method 
  * @return boolean value, true if max attempts reached, false if not
  */
  public boolean getAttemptsFlag( )
  {
	  return maxAttemptsFlag;
  }

 // private inner class event handler
 private class TextFieldHandler implements ActionListener
 {
  public void actionPerformed( ActionEvent e )
  {
	  if( numLoginAttempts < 3 )
	  {
		if ( id.getText( ).equals( "open" )
		&& ( new String( password.getPassword( ) ) ).equals( "sesame" ) )
		{
		message.setForeground( Color.BLACK );		
		loginFlag = true;
		message.setText( "Welcome!" );
		}
		else
		{
		numLoginAttempts ++;
		message.setForeground( Color.RED );
		message.setText( "Sorry: wrong login" );
		}
	  }			  
  }
 }
 
 
}



What I was wanting to do was have TrainRidePanel create a Login object, then use somehow figure out if the login was successful, close the login window, and open up the main GUI for the program. Right now, I haven't put in the code for the GUI yet, but was trying to test the client to make sure that control would switch back over to the client after the login was either successful or the max amount of attempts was exceeded.
Here's the TrainRidePanel client class:
import javax.swing.JOptionPane;

public class TrainRidePanel
{
		
	// constructor
	public TrainRidePanel() 
	{
			
		Train t1 = new Train( "passengers.txt" );
		
		System.out.println( t1.toString( ) );
	} 
		
	 public static void main( String [] args )
	{
		Login login = new Login( );
					
		//login.login( );
		
		if( ( login.getAttemptsFlag( ) == true ) || ( login.getLoginFlag( ) == true ) )
		{
			if( login.getAttemptsFlag( ) == true )
			{
		
	   		JOptionPane.showMessageDialog( null, 
	   					"Maximum number of login attempts exceeded!");
			JOptionPane.showMessageDialog( null, 
   						"TrainRidePanel now exiting");
   			System.exit(0);
			}
			else
			{
				TrainRidePanel trp = new TrainRidePanel( );
			}	
		}
		
	}
}  



Am I way off here? If it's going to be more efficient, I'll start over and rewrite the Login class, but I'm totally confused as to what direction to go in right now. I've also considered, since I'll be using a combination of BorderLayout and GridLayout in the GUI, just having the login somewhere in there. Then I only deal with one window, and once the login is successful, the rest of the GUI will appear, but until then, just the login part will be visible. Would that be easier?

Also, there's some commented out stuff, just ignore it. I was testing different things out and haven't deleted them.

One more thing, attached is the UML diagram, if it helps at all to see what's going on.

Thanks

Attached image(s)

  • Attached Image

This post has been edited by dustinm: 18 December 2009 - 09:51 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How do I close one window, then open another in a client class?

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: How do I close one window, then open another in a client class?

Posted 18 December 2009 - 09:57 AM

I posted a reply a few topics down the page, where someone wanted a new login screen to appear if the login was successful. Check this out.
Was This Post Helpful? 0
  • +
  • -

#3 dustinm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 30-November 09

Re: How do I close one window, then open another in a client class?

Posted 18 December 2009 - 10:08 AM

View Postnick2price, on 18 Dec, 2009 - 08:57 AM, said:

I posted a reply a few topics down the page, where someone wanted a new login screen to appear if the login was successful. Check this out.

Thanks, I actually read that right before I posted. It was helpful, but I'm a little lost as to how I would implement that into my situation. In your suggestion for that other topic, you were talking about going "back" to a window that had previously been open. In my case, I want to close the first window, and be able to move down in the client class to where I will open a new window.

I just realized something I forgot to mention. When I run my client class, the login window pops up as it should, but after putting in the correct info, it just stays open. I've tried the different default close operations, and that didn't help. If I use "return;" in the login class to get it to close, it closes the whole thing, so does System.exit(). How do I get it so that it will close that window and start executing the next line of code in the client class.
Was This Post Helpful? 0
  • +
  • -

#4 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: How do I close one window, then open another in a client class?

Posted 18 December 2009 - 10:21 AM

That code I posted should work fine. It is to be used in ActionEvents. So picture this, you have a login button, which has an ActionEvent associated with it. In this ActionEvent, check to see if the login was successful, and then use that code which clears the JFrame and adds your new Panel on top of it. Then in your JPanel class, you might have a back button with an ActionEvent. In this ActionEvent, put the second bit of code, which will make sure it goes back to the original Frame if the back button is pushed. Dont do this checking in your main method, keep it in the associated ActionEvents. See, you have done a Text FIeld Listener, I would just have a JButton saying submit ot somthing, and chuck the code in this ActionEvent. If you would like an example, let me know.
Was This Post Helpful? 1
  • +
  • -

#5 dustinm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 30-November 09

Re: How do I close one window, then open another in a client class?

Posted 18 December 2009 - 12:15 PM

OK, it kind of makes sense, but I'm still not getting how the ActionEvent in the Login class is going to open the JPanel from the TrainRidePanel class. I kind of rewrote the Login class to use a JButton, and simplified things a bit. I have the ActionListener in there for the JButton, but I'm still unsure about what to put there.

 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.*;
 
 public class Login extends JFrame
 {
 	private static final int WIDTH = 300;
 	private static final int HEIGHT = 250;
	private Container contents;
	private JLabel idLabel, passwordLabel, message;
	private JTextField id;
	private JPasswordField password;	
	private JButton loginButton;
	//private int numLoginAttempts = 0;
	//private boolean loginFlag = false;
	//private boolean maxAttemptsFlag = false;
	
	//constructor
	public Login( )
	{
		super( "Login Screen" );
		setSize( WIDTH, HEIGHT );
		setVisible( true );
		setDefaultCloseOperation( EXIT_ON_CLOSE );
		setResizable( false );
		
		contents = getContentPane( );
		contents.setLayout( new FlowLayout( ) );
		
		message = new JLabel( "Please log in. Maximum of 3 login attempts allowed." );  // label to hold messages
		
		idLabel = new JLabel( "Enter id" ); // label for ID
		  id = new JTextField( "", 12 );	  // instantiate ID text field
		  
		  passwordLabel = new JLabel( "Enter password" ); // password label
		  password = new JPasswordField( 8 ); // instantiate password field
		  password.setEchoChar( '?' );		// set echo character to '?'
		  
		  // Jbutton
		  loginButton = new JButton( );	
		  jButton1.setBackground(new Color(204, 204, 204));
		jButton1.setForeground(new Color(0, 0, 255));
		jButton1.setText("Login");
		lbHandler = new LoginButtonHandler( );
		jButton1.addActionListener( lbHandler );		  
		  
		  // add all components to the window		  
		  contents.add( message );
		  contents.add( idLabel );
		  contents.add( id );
		  contents.add( passwordLabel );
		  contents.add( password );
		  contents.add( loginButton );
		  
		  public class LoginButtonHandler implements ActionListener
		{
			public void actionPerformed(ActionEvent e)
			{				
				String idText = new String( id.getText( ) );
				String passwordText = new String( password.getText( ) );
				
				if( idText.equals( "open" ) && passwordText.equals( "sesame" ) )
				{
					
				}
			}
		}

		
	}
 	
 }


Was This Post Helpful? 0
  • +
  • -

#6 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: How do I close one window, then open another in a client class?

Posted 18 December 2009 - 12:18 PM

Can i see how some of ur TrainRidePanel class looks
Was This Post Helpful? 0
  • +
  • -

#7 dustinm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 30-November 09

Re: How do I close one window, then open another in a client class?

Posted 18 December 2009 - 12:28 PM

I posted up above, but it's basically not started yet. I wrote the other classes, and when I started on the TrainRideClass, I was just working on it's interaction with the Login class to make sure I could make it work. would it be best to just get it all written, then worry about the login after it's mostly done? Also, would my last idea in my first post be a better option, to basically have the login fields in the panel, then clear the panel and put in the other content if the id/password are correct?
Was This Post Helpful? 0
  • +
  • -

#8 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: How do I close one window, then open another in a client class?

Posted 18 December 2009 - 01:14 PM

I will show you somthing I done a while ago. It doesnt use a login for this one, but can be easily implemented to use a login. My main page is a standard page, which will have buttons to take you to other pages

//GUI where users can choose to update or print events
import java.awt.*; //the importing of required libraries
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.KeyListener;
import java.awt.event.*;

/**Class which displays the users choices*/
public class CreateAtlDatabase extends JFrame { //this class will inherit all the usefulness of JFrame class. eg JFrame.setSize()
		
		private JPanel contentPane;  //declaring variables
		
	 
	 	private JPanel pnlTop; 
		private JPanel pnlCenter;
		private	Icon image = new ImageIcon("olympics.jpg"); //adding an image to Icon data type
		private	ImageIcon image2 = new ImageIcon("PageTitle.jpg");
		private JLabel centerPanel1;
	
		
		private JButton btnUpdate;
		private JButton btnPrint;  
	 	private JButton btnExit; 
	 	private JButton btnSearch;
	 	
	 	private JPanel  pnlBottom;

 /**Class constructor*/
public CreateAtlDatabase() 
	 	{ 
 		super(); //superclassing the JFrame
 		initializeComponent(); //calling up method initializeComponent
	 	} 
 	
 	/**method to place components*/
private void initializeComponent() 
	 	{
		contentPane = (JPanel)this.getContentPane();//contentPane to JFrame is like a canvas to the frame
		
		pnlTop = new JPanel(){ //declering and initialising the top panel
			public void paintComponent(Graphics g)//overiding paint component of the panel in order to have background image
			{
				Dimension d = getSize();//getting size of the panel
				g.drawImage(image2.getImage(), 0, 0, d.width, d.height, null);//drawing background image to the dimension of the panel
				setOpaque(false);									//seting the image opaque so the components could be placed on it
				
				super.paintComponent(g); //applying the image to the panel's paint component
				
			}
			
		};
		
		pnlCenter = new JPanel();
		pnlBottom = new JPanel();

		btnUpdate = new JButton();
		btnPrint = new JButton();
		btnExit = new JButton();
		btnSearch = new JButton();
		
		centerPanel1 = new JLabel(image);//placing the image into JLabel, one way of inserting image onto a JPanel 
		
		contentPane.setLayout(new BorderLayout(3, 3)); //border layout for position of JPanels
		contentPane.add(pnlTop, BorderLayout.NORTH); 
		contentPane.add(pnlCenter, BorderLayout.CENTER); 
		contentPane.add(pnlBottom, BorderLayout.SOUTH); 
		contentPane.setBackground(new Color(0, 0, 0)); 
		
		pnlTop.setLayout(new FlowLayout()); //flow layout for position on JPanels
		pnlTop.setPreferredSize(new Dimension(800, 105)); 
	 
		
		pnlCenter.setLayout(new FlowLayout()); 
		pnlCenter.setBackground(new Color(250, 251, 251));
	//	pnlCenter.setPreferredSize(new Dimension(800, 470));  
		pnlCenter.add(centerPanel1, 0); 
		
		btnUpdate.setText("ENTER DATA"); //Set text to CreateAtlDatabase JButton
 		btnUpdate.addActionListener(new ActionListener() { //Action listener listens for event changes triggered by push of a button
 			public void actionPerformed(ActionEvent e) 
 			{ 
 				UpdateResults_actionPerformed(); //if butto pressed call up this method
 			} 
  
 		}); 
 		
 		btnPrint.setText("PRINT RESULTS"); 
 		btnPrint.addActionListener(new ActionListener() { 
 			public void actionPerformed(ActionEvent e) 
 			{ 
 				btnPrint_actionPerformed(); 
 			} 
  
 		}); 
 		
 		btnSearch.setText("  SEARCH/EDIT  "); 
 		btnSearch.addActionListener(new ActionListener() { 
 			public void actionPerformed(ActionEvent e) 
 			{ 
 				btnSearch_actionPerformed(); 
 			} 
  
 		}); 
 		
 		btnExit.setText("  EXIT  "); 
 		btnExit.addActionListener(new ActionListener() { 
 			public void actionPerformed(ActionEvent e) 
 			{ 
 				btnExit_actionPerformed(); 
 			} 
  
 		}); 
		
		pnlBottom.setLayout(new FlowLayout(FlowLayout.CENTER, 6, 5)); 
		pnlBottom.setBackground(new Color(137, 199, 210)); 
 		pnlBottom.add(btnUpdate, 0); 
 		pnlBottom.add(btnPrint, 1);
 		pnlBottom.add(btnSearch, 2);  
 		pnlBottom.add(btnExit, 3); 

		
		this.setTitle(""); //adding title to a this JFrame
		this.setLocation(new Point(0, 0));   //setting location of the JFrame to x=0 and y=0 (top left of the screen) 
		this.setSize(new Dimension(800, 608)); 
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); //if application is closed it will dispose of it (not hide it)
		this.setVisible(true); 
	 	 }
	
	/**Method for if user chooses to enter data || Data pre entered so has been disabled*/ 	 
	private void UpdateResults_actionPerformed() 
		{ 
		
		setVisible(false);
		
		JDialog dialog = new JDialog(this, "MEET_dataTable", true);
		dialog.add(new Meet());  
		dialog.pack();			
		dialog.setVisible(true);  
		//The reason for this is that our meet JPanel needs a container, and instead of creating a new one, it is more resouceful
		//to clear everything on this JFrame and place the JPanel on here whilst it is needed
		this.dispose();
		
		} 
	
	/**Method for if user chooses to print results*/
	private void btnPrint_actionPerformed() 
		{ 
		setVisible(false);  //setting everithing on the CreateAtlDatabase invisible
		JDialog dialog = new JDialog(this, "Print_Results", true);  //creating new dialog 
		dialog.add(new PrintResults()); //place the PrintResults JPANEL onto the JDialog
		dialog.pack();
		dialog.setVisible(true);  //set the JPanle to visible so that it is displayed in this JFrame
		this.dispose(); //The reason for this is that our meet JPanel needs a container, and instead of creating
						// a new one, it is more resouceful
						//to clear everything on this JFrame and place the JPanel on here whilst it is needed
		} 
	
	/**Method for if user decides to search/edit*/	
	private void btnSearch_actionPerformed() 
		{ 
		setVisible(false);
		JDialog dialog = new JDialog(this, "Update_Results", true);
		dialog.add(new SearchEdit());
		dialog.pack();
		dialog.setVisible(true);
		this.dispose();

		} 
	
	/**Method to exit the system*/
	private void btnExit_actionPerformed() 
		{ 
		this.dispose();
		} 
		
	public static void main(String[] args){  //main method to execute the class
	SwingUtilities.invokeLater(new Runnable(){
		public void run(){
			new CreateAtlDatabase();
		}
	});
	
}
}



Notice what the ActionEvents are doing to bring up other pages. Sorry about the allignment of code, its not pasting correctly. So lets look at btnSearch_actionPerformed, you can see it places the class new SearchEdit() onto this JFrame. This new class extends a JPanel, and looks like

import java.awt.event.KeyEvent;
import javax.swing.*;
import java.awt.*; 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.table.*;
import javax.swing.event.*;
import java.util.List;
import java.util.ArrayList;


/**Class whic displays the search/edit view to the user*/
public class SearchEdit extends JPanel{

	JComboBox eventsCbox, eventsCbox2, roundsCbox, roundsCbox2;
	protected static final String [] columnNames = 
	{
		"<html><b><h2>NATIONALITY</h2></b></html>", 
		"<html><b><h2>FIRST NAME</h2></b></html>",
		"<html><b><h2>LAST NAME</h2></b></html>",
		"<html><b><h2>TIME SET</h2></b></html>",
		
	};
	
	private Object[][] data = {
	{"", "", "", "",}, {"", "", "", "",},
	{"", "", "", "",}, {"", "", "", "",},
	{"", "", "", "",}, {"", "", "", "",},
	{"", "", "", "",}, {"", "", "", "",},
	{"", "", "", "",}
};


	private JTable table;
	private DefaultTableModel model;
	private JScrollPane scroller;
	
	private Icon wembley = new ImageIcon("wemTab.JPG");
	private Icon wimbledon = new ImageIcon("wimblTab.JPG");
	private Icon aquatics = new ImageIcon("aquaTab.JPG");
	private Icon velodrome = new ImageIcon("veloTab.JPG");
	private ImageIcon imageTop, imageTop2; 

	private ImageIcon eventsLeftPanel, wimbledonLeftImage;
	private JButton exit,search, back, update;
	private String sType, sType2;

	DatabaseMethods dMethods = new DatabaseMethods();
	DatabaseSQL sql = new DatabaseSQL();

/**Constructor of class which lays out components*/
public SearchEdit() {
	
		super(new GridLayout(1, 1));

		JTabbedPane tabbedPane = new JTabbedPane();
		
		
		imageTop = new ImageIcon("eventsWembley.JPG");			
		JComponent wemTopPanel = new JPanel(){
			public void paintComponent(Graphics g)
				{
				Dimension d = getSize();
				g.drawImage(imageTop.getImage(), 0, 0, d.width, d.height, null);
				setOpaque( false );
				super.paintComponent(g);
				}
			};

	   	wemTopPanel.setLayout(new FlowLayout());
	   	wemTopPanel.setPreferredSize(new Dimension(800, 120));

	   	
		eventsLeftPanel = new ImageIcon("londonLeftPanel.jpg");
		JComponent wemLeftPanel = new JPanel(){
			public void paintComponent(Graphics g)
				{
				Dimension d = getSize();
				g.drawImage(eventsLeftPanel.getImage(), 0, 0, d.width, d.height, null);
				setOpaque( false );
				super.paintComponent(g);
				}
			};
	   	wemLeftPanel.setLayout(new GridBagLayout());
	   	
	   	
	   	String [] eventsStr = {"Select Event", "100M Run", "200M Run", "400M Run", "800M Run"};
	   	eventsCbox = new JComboBox(eventsStr);
	   	
	   	eventsCbox.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
				{
				JComboBox jboxType = (JComboBox) e.getSource();
				sType = (String)jboxType.getSelectedItem();
				}
			});
	   	
	   	
	   	
	   	GridBagConstraints gb01 = new GridBagConstraints();
	   	gb01.gridx = 0;
	   	gb01.gridy = 0;
	   	gb01.insets = new Insets(5, 30, 5, 30);
	   	wemLeftPanel.add(eventsCbox, gb01);
	   	
	   	String [] roundsStr = {" Select Round", "Round_1", "Round_2", "Round_3"};
	   	roundsCbox= new JComboBox(roundsStr);
	   	
	   	roundsCbox.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
				{
				JComboBox jboxType = (JComboBox) e.getSource();
				sType2 = (String)jboxType.getSelectedItem();
				}
			});
	   	
	   	GridBagConstraints gb02 = new GridBagConstraints();
	   	gb02.gridx = 0;
	   	gb02.gridy = 2;
	   	gb02.insets = new Insets(5, 30, 225, 30);
	   	gb02.anchor = GridBagConstraints.PAGE_START;
	   	wemLeftPanel.add(roundsCbox, gb02);
	   	
	   	JComponent wemRightPanel = new JPanel();
	   	wemRightPanel.setBackground(new Color (0, 0, 0));
		  wemRightPanel.setLayout(new GridBagLayout());
		  

		  model = new DefaultTableModel(data, columnNames);
		table = new JTable(model);
		table.putClientProperty("terminateEditonfocusLost", Boolean.TRUE);
		table.setRowHeight(25);
		table.getInputMap().put(KeyStroke.getKeyStroke("ENTER"),("selectNextColumnCell")); //giving the enter key the same properties as Tab key
		
		  int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
		int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
		  scroller = new javax.swing.JScrollPane(table, v, h);
		  scroller.setPreferredSize(new Dimension(550,380));
		  wemRightPanel.add(scroller, 0);
		  

		GridBagConstraints gb03 = new GridBagConstraints();
		gb03.gridx = 0;
	   	gb03.gridy = 0;
	   	gb03.gridwidth = 3;
	   	gb03.insets = new Insets(25, 5, 5, 5);
	   	gb03.anchor = GridBagConstraints.PAGE_START;
	   	wemRightPanel.add(scroller, gb03);
		  
		  
		  back = new JButton("GO BACK");
		  back.addActionListener(new ActionListener() { 
 			public void actionPerformed(ActionEvent e) 
 			{ 
 				back_actionPerformed(); 
 			} 
  
 		}); 
 		
		GridBagConstraints gb04 = new GridBagConstraints();
		gb04.gridx = 0;
	   	gb04.gridy = 3;
	   	gb04.gridwidth = 1;
	   	gb04.insets = new Insets(15, 135, 5, 5);
	   	gb04.anchor = GridBagConstraints.LAST_LINE_START;
	   	wemRightPanel.add(back, gb04);
	   	
	   	search = new JButton("SEARCH");
	   	search.addActionListener(new ActionListener() { 
 			public void actionPerformed(ActionEvent e) 
 			{ 
 				search_actionPerformed(); 
 			} 
  
 		}); 
 		
		GridBagConstraints gb05 = new GridBagConstraints();
		gb05.gridx = 1;
	   	gb05.gridy = 3;
	   	gb05.gridwidth = 1;
	   	gb05.insets = new Insets(15, 25, 5, 5);
	   	gb05.anchor = GridBagConstraints.LAST_LINE_START;
	   	wemRightPanel.add(search, gb05);
		
		exit = new JButton("EXIT"); 
		exit.addActionListener(new ActionListener() { 
 			public void actionPerformed(ActionEvent e) 
 			{ 
 				exit_actionPerformed(); 
 			} 
  
 		}); 
 		
		GridBagConstraints gb06 = new GridBagConstraints();
		gb06.gridx = 2;
	   	gb06.gridy = 3;
	   	gb06.gridwidth = 1;
	   	gb06.insets = new Insets(15, 25, 5, 5);
	   	gb06.anchor = GridBagConstraints.LAST_LINE_START;
	   	wemRightPanel.add(exit, gb06);
	   	
	   	update = new JButton("UPDATE"); 
		update.addActionListener(new ActionListener() { 
 			public void actionPerformed(ActionEvent e) 
 			{ 
 				update_actionPerformed(); 
 			} 
  
 		}); 
 		
		GridBagConstraints gb07 = new GridBagConstraints();
		gb07.gridx = 3;
	   	gb07.gridy = 3;
	   	gb07.gridwidth = 1;
	   	gb07.insets = new Insets(15, 25, 5, 5);
	   	gb07.anchor = GridBagConstraints.LAST_LINE_START;
	   	wemRightPanel.add(update, gb07);   
   
	   	JComponent panel1 = new JPanel();
	   	panel1.setBackground(new Color (31, 89, 99));
		panel1.setLayout(new BorderLayout(5, 5));
		panel1.add(wemTopPanel, BorderLayout.NORTH);
		panel1.add(wemLeftPanel, BorderLayout.WEST);
		panel1.add(wemRightPanel, BorderLayout.CENTER);
		tabbedPane.addTab("", wembley, panel1);
		tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
		
		
		
		JComponent panel2 = new JPanel();
		tabbedPane.addTab("", wimbledon, panel2);
		tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
		

		JComponent panel3 = new JPanel();
		tabbedPane.addTab("", aquatics, panel3);
		tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);
		
		JComponent panel4 = new JPanel();
		panel4.setPreferredSize(new Dimension(800, 608));
		tabbedPane.addTab("", velodrome,  panel4);
		tabbedPane.setMnemonicAt(3, KeyEvent.VK_4);
		
		add(tabbedPane);
		
		tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
		

 
	}

	/**Method to take the user back to previous screen*/
	private void back_actionPerformed()  
	{
		SwingUtilities.getWindowAncestor(this).dispose();  //get the parent frame
		CreateAtlDatabase data = new CreateAtlDatabase();  //add the components
	} 
	
	/**Method which searches the database*/
	private void search_actionPerformed()  
	{
		if (sType == null || sType2 == null) 
		{
			JOptionPane.showMessageDialog(null, "Please select an Event and Round to search");  
		}
		
		else
		{
			List<UpdateEventSetGet> searchResult = sql.searchRecords(sType, sType2);  //get returned results from searchRecords method
		
				for(int x = 0, y = searchResult.size(); x < y; x++) //loop size of result
				{
					UpdateEventSetGet update = searchResult.get(x); //put results into objects
					
	
					model.setValueAt(update.getNationality(), x, 0); //assign each variable of object to a position in JTable
					model.setValueAt(update.getFirstname(), x, 1);
					model.setValueAt(update.getLastname(), x, 2);
					model.setValueAt(update.getTime(), x, 3);
					
					
				}
		}
		
		UpdateEventSetGet[] update1 = new UpdateEventSetGet[9];
			
			for(int i = 0; i < update1.length; i++)
			{
			update1[i] = new UpdateEventSetGet(table, i);
			}
		dMethods.UpdateTable2(update1); //send original data off to DatabaseMethods.java
			
	}
	
	/**Method which performs an update*/
	private void update_actionPerformed()  
	{
	
			
			UpdateEventSetGet[] update = new UpdateEventSetGet[9];
			
	
		for(int i = 0; i < update.length; i++)
			{
			update[i] = new UpdateEventSetGet(table, i);
			}

		dMethods.UpdateTable(table, update, sType, sType2);

	}
		
	/**Method which exits the system*/
	private void exit_actionPerformed()  
	{
	System.exit(0);	
	}


}
	




You can forget about most of that code, but look what the back button does. Your login will work in the exact same way. You have your main login page which will problably be a JFrame, like my first class. Then you can have another page which is going to be displayed if the login is successful, this class can extend a JPanel. Now, in you login class, when they click submit, in the ActionEvent, firstly make sure login is ok, and if it is, just place the code I use to place the new class in the current login JFrame.

Remember, JFrame is a top level container. Jpanel is a second level container. For a JPanel to displayed, it needs to be placed in a top level container. All you are doing is cleaning the middle of you JFrame (most likely a ContentPane), and placing the new class's JPanel ontop of it. The code I have posted is part of a larger program, so wont work alone. Use it to get ideas of what to do though. I have used this way many times to make login pages.
Was This Post Helpful? 1
  • +
  • -

#9 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: How do I close one window, then open another in a client class?

Posted 18 December 2009 - 09:31 PM

I wrote a Code Snippet on login window followed by the main Frame
Was This Post Helpful? 1
  • +
  • -

#10 dustinm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 30-November 09

Re: How do I close one window, then open another in a client class?

Posted 19 December 2009 - 05:25 AM

Those last two posts helped tremendously. Thanks to both of you. I think I understand it enough to make it work for this project, but i'll probably have to go back and reread on _why_ it works (if that even makes sense). I think my original logic of how I was trying to write the panel class was flawed, and now I get how to structure everything. One more quick question, just for clarification. What's really happening here is that the panel for the program GUI is actually appearing first, but is immediately being replaced by the login, then the submit button goes back to the programs panel if the login is correct, right? I don't know, it just sometimes helps me out to break these things down to a simple explanation of what's going in, since it can sometimes be hard to tell from the actual code (for me atleast). Nevermind, I get it now.

This post has been edited by dustinm: 19 December 2009 - 06:28 AM

Was This Post Helpful? 0
  • +
  • -

#11 dustinm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 30-November 09

Re: How do I close one window, then open another in a client class?

Posted 19 December 2009 - 11:56 AM

Ok, I got it done, for the most part. I have a few problems with getting the layout configured correctly, but the issues aren't related to this, so I'm going to post a new topic.

Thanks again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1