Results 1 to 8 of 8
  1. #1
    nashbender is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default how to open a new JFrame from a JFrame

    Hi, so I want to make this kind of "webpage" using GUI for a standalone program. this program contains a login page, and if login successful, it will go to a panel of buttons. one of the buttons to open my existing program called FileExplorer which displays my comp files and folders. problem is, i don't know how to open up that JFrame from FileExplorer by clicking the button.

    i have tried adding action listener to the button but i encountered an error of :
    "java.lang.IllegalArgumentException: adding a window to a container "

    may i know how to do it correctly?

    My login page:
    Java Code:
    //username : nasya , password : nasya
    
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class Login extends JFrame {
    	private static final String text = "<html> <center><b><font size=+3>Central</font></b> <br><b><font size=+3>Manager</font></b></center> </html>";
    
    	JPanel jPanel1 = new JPanel();
    	BorderLayout borderLayout1 = new BorderLayout();
    	JLabel jLabel1 = new JLabel();
    	JPanel jPanel2 = new JPanel();
    	GridBagLayout gridBagLayout1 = new GridBagLayout();
    	JLabel jLabel2 = new JLabel();
    	JLabel jLabel3 = new JLabel();
    	JTextField loginTextField = new JTextField(20);
    	JPasswordField passwordTextField = new JPasswordField(20);
    	JPanel jPanel3 = new JPanel();
    	JButton exitButton = new JButton();
    	JButton enterButton = new JButton();
    	FlowLayout flowLayout1 = new FlowLayout();
    
    	public Login() {
    		try {
    			jbInit();
    		}
    		catch(Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    	private void login(ActionEvent e) {
    		String login = loginTextField.getText();
    		String password = new String(passwordTextField.getPassword());
    		
    		//validate login and password here. validity will be done by sending login/password to the server
    		if (login.equals("nasya") && password.equals("nasya")) {
    			System.out.println("login successful");
    			showButtonsView();
    		} 	
    		else {
    			JOptionPane.showMessageDialog(this,"Incorrect login or password","Error",JOptionPane.ERROR_MESSAGE);
    			loginTextField.setText("");
    			passwordTextField.setText("");
    			loginTextField.requestFocusInWindow();
    		}
    	}
    
    	private void showButtonsView() {
    		getContentPane().removeAll();
    		ButtonView button = new ButtonView();
    		getContentPane().add(button);
    		getContentPane().validate();
    	}
    	
    	private void exit(ActionEvent e) {
    		setVisible(false);
    	}
    
    	public static void main(String[] args) {
    		Login f = new Login();
    		f.setLocation(200,200);
    		f.pack();
    		f.show();
    	}
    
    	private void jbInit() throws Exception {
    		jPanel1.setLayout(borderLayout1);
    		jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
    		jLabel1.setText(text);
    		jPanel2.setLayout(gridBagLayout1);
    		jLabel2.setText("Password:");
    		jLabel3.setText("Login:");
    		exitButton.setText("Exit");
    		exitButton.addActionListener(new java.awt.event.ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				exit(e);
    			}
    		});
    		
    		enterButton.setText("Enter");
    		enterButton.addActionListener(new java.awt.event.ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				login(e);
    			}
    		});
    		
    		jPanel3.setLayout(flowLayout1);
    		flowLayout1.setAlignment(FlowLayout.RIGHT);
    		this.getContentPane().add(jPanel1, BorderLayout.CENTER);
    		jPanel1.add(jLabel1, BorderLayout.NORTH);
    		jPanel1.add(jPanel2, BorderLayout.CENTER);
    		jPanel2.add(loginTextField, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0
    				,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 11, 0, 0), 0, 0));
    		jPanel2.add(jLabel2, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
    				,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(11, 0, 0, 0), 0, 0));
    		jPanel2.add(passwordTextField, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0
    				,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(11, 11, 0, 0), 0, 0));
    		jPanel2.add(jLabel3, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0
    				,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    		jPanel1.add(jPanel3, BorderLayout.SOUTH);
    		jPanel3.add(enterButton, null);
    		jPanel3.add(exitButton, null);
    	}
    }
    My Buttons Page:
    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    
    
    public class ButtonView extends JFrame
    	{
    	GridLayout gridLayout1 = new GridLayout(2,2);
    	BorderLayout borderLayout1 = new BorderLayout();
    	//JScrollPane jScrollPane1 = new JScrollPane();
    
    	JPanel jPanel1 = new JPanel();
    	JButton jButton1 = new JButton();
    	JButton jButton2 = new JButton();
    	JButton jButton3 = new JButton();
    	JButton jButton4 = new JButton();
    
    	public ButtonView() {
    		try {
    			jbInit();
    		}
    		catch(Exception ex) {
    			ex.printStackTrace();
    		}
    	}
    
    	void jbInit() throws Exception {
    		this.setLayout(borderLayout1);
    		jButton1.setText("View FileSystem");
    		jButton1.addActionListener(new java.awt.event.ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				new FileExplorer();
    				setVisible(true);
    			}
    		});
    		jButton2.setText("Example");
    		jButton3.setText("Example");
    		jButton4.setText("Example");
    		this.add(jPanel1, BorderLayout.CENTER);
    		jPanel1.setLayout(gridLayout1);
    		jPanel1.add(jButton1, null);
    		
    		
    		jPanel1.add(jButton2, null);
    		jPanel1.add(jButton3, null);
    		jPanel1.add(jButton4, null);
    }
    }
    The FileExplorer code spans 7 classes..so i will just attach them here if you wanna look into it...

    Please give me some insights and thanks in advance :)
    Attached Files Attached Files

  2. #2
    nashbender is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    sorry i didn't know there's attachment limit here..hehe anyways these are 2 more files
    Attached Files Attached Files

  3. #3
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    You cannot add JFrame to a JFrame. You can show this by creating new ButtonView.
    Java Code:
        ButtonView aButtonView = new ButtonView();
        aButtonView.setVisible(true);
    Then call dispose() method of Login JFrame.

  4. #4
    nashbender is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by mine0926 View Post
    You cannot add JFrame to a JFrame. You can show this by creating new ButtonView.
    Java Code:
        ButtonView aButtonView = new ButtonView();
        aButtonView.setVisible(true);
    Then call dispose() method of Login JFrame.
    umm...the buttonView is in a panel...therefore i wrote getContentPane.add(button); to include it in login page...the trouble now is from the ButtonView page to the FileExplorer page... :)

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default

    Way too much code to download and debug. Can you make a small simple example that compiles and executes that demonstrates your problem?

  6. #6
    nashbender is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Way too much code to download and debug. Can you make a small simple example that compiles and executes that demonstrates your problem?
    um, i am not sure how to do that, because when i tried a simple program that simply opens up and empty frame after clicking a button,,using initialization then setVisible(true),, it runs well. but i think this has something to do with my current program, that caused it to have errors.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default

    caused it to have errors
    If you get errors, please copy the full text and paste it there.

    Look at the text of the error message, find the line number where the error occurs and copy the code around that statement. Especially in front of it.

  8. #8
    nashbender is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    hi guys..nevermind i solved the problem just now while playing around with it...now im happy :D
    it's something inside file explorer code..
    thanks anyway ...

Similar Threads

  1. How to open new JFrame with clicking on button?
    By phearless in forum AWT / Swing
    Replies: 1
    Last Post: 01-14-2011, 11:36 AM
  2. How do i open JFrame in the same active window
    By Jkiller295 in forum AWT / Swing
    Replies: 35
    Last Post: 06-02-2010, 05:50 PM
  3. How To Open a JFrame Form from a jButton
    By Lyricid in forum AWT / Swing
    Replies: 12
    Last Post: 03-28-2010, 07:53 AM
  4. Open JFrame
    By AndersBjörnör in forum AWT / Swing
    Replies: 2
    Last Post: 12-01-2009, 10:03 PM
  5. How to close an open JFrame window from a jsp page?
    By kasisaiganesh in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 05-27-2008, 07:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •