Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: GUI does not stay loaded in applet

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default GUI does not stay loaded in applet

    Hello All,

    I have been working on creating a GUI Java Swing Applet program for my class and I am running into a problem when I run the program. After writing the code, the code successfully compiled, but when it was running and opened the frame that I created, the components in the frame (username, password, login button, cancel button) kept disappearing and appearing. I am not using a browser to open the applet, I am just compiling and running the program on DOS. Any help and suggestions would be greatly appreciated. Thank you!

    import javax.swing.*;
    import javax.swing.JApplet;
     
    public class SwingLoginFrame 
    {
    	public static void main(String args[])
    	{
    		JFrame objJFrame = new JFrame("Login");
    		objJFrame.setSize(300,300);
    		//JFrame.setDefaultLookAndFeelDecorated(true);
    		objJFrame.setVisible(true);
     
    		JButton objJButton1 = new JButton("Login");
    		JButton objJButton2 = new JButton("Cancel");
    		JTextField objJTextField1 = new JTextField(" ",0);
    		JTextField objJTextField2 = new JTextField(" ",0);
    		JLabel objJLabel1 = new JLabel();
    		objJLabel1.setText("Username");
    		JLabel objJLabel2 = new JLabel();
    		objJLabel2.setText("Password");
     
    		objJButton1.setBounds(40,150,80,90);
    		objJButton2.setBounds(130,155,80,83);
    		objJTextField1.setBounds(120,70,150,20);
    		objJTextField2.setBounds(120,110,150,20);
    		objJLabel1.setBounds(40,50,80,50);
    		objJLabel2.setBounds(40,80,80,80);
     
    		objJFrame.add(objJLabel1);
    		objJFrame.add(objJLabel2);
    		objJFrame.add(objJTextField1);
    		objJFrame.add(objJTextField2);
    		objJFrame.add(objJButton1);
    		objJFrame.add(objJButton2);
    	}
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: GUI does not stay loaded in applet

    If you rely on a layout manager to layout the contents, (which appears to be the case here), call pack() on the JFrame.
    Add all components to the JFrame before setting the frame visible. (...and before calling pack()...)

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: GUI does not stay loaded in applet

    There are several improvements that can be made, and I'm not sure any of them will solve your problem, because I can't duplicate it.

    This one certainly won't: Give your variables better names! For example, why name the cancel button "objJButton2"??? Name the dang thing "cancelButton" or something that makes sense. The same with the rest of your variables!

    Run your Swing applications on the EDT. An alarm should go off anytime you build and run a Swing app on the main thread or entirely in the main() method. This may actually be contributing to the error you're seeing, causing the components to blink every time the GUI is redrawn, but I'm not sure since I can't see it.

    Finally, the last component being added to the JFrame is becoming the JFrame's background. I think this is because the default layout manager for the JFrame is BorderLayout, so the last component added is taking the BorderLayout.CENTER position. You can verify this by commenting out the last component being added (currently the cancel button) and observe what happens. Even using a simple layout manager like FlowLayout is preferred to what you've done, but since this is a probably a program to study how things work, playing around to see what happens is fine.

    Here's your program modified to do everything I've described except changing the variable names - that's for you to do. Let me know if you still see the components coming in and out.
    /* File: TechStudentFrame.java
     * GregBrannon, August 2013, modified TechStudent's post:
     * http://www.javaprogrammingforums.com/whats-wrong-my-code/
     * 30834-gui-does-not-stay-loaded-applet.html
     */
     
    import java.awt.FlowLayout;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
     
    public class TechStudentFrame 
    {
    	// the default constructor that builds the frame and sets it visible
    	public TechStudentFrame()
    	{
    		// set the frame's basic characteristics
    		JFrame objJFrame = new JFrame("Login");
    		objJFrame.setSize(250,150);
    		objJFrame.setResizable( false );
    		objJFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    		objJFrame.setLayout( new FlowLayout() );
     
    		// build the components to be added to the frame
    		JButton objJButton1 = new JButton("Login");
    		JButton objJButton2 = new JButton("Cancel");
    		JTextField objJTextField1 = new JTextField(" ",10);
    		JTextField objJTextField2 = new JTextField(" ",10);
    		JLabel objJLabel1 = new JLabel();
    		objJLabel1.setText("Username");
    		JLabel objJLabel2 = new JLabel();
    		objJLabel2.setText("Password");
     
    		// add the components to the frame
    		objJFrame.add(objJLabel1);
    		objJFrame.add(objJTextField1);
    		objJFrame.add(objJLabel2);
    		objJFrame.add(objJTextField2);
    		objJFrame.add(objJButton1);
    		objJFrame.add(objJButton2);
     
    		//JFrame.setDefaultLookAndFeelDecorated(true);
     
    		// set the frame visible
    		objJFrame.setVisible( true );
    	}
     
    	// the main() method creates an instance of the frame on the EDT
    	// see this article for more info:
    	// http://www.javaworld.com/community/node/7717
    	public static void main(String args[])
    	{
    		SwingUtilities.invokeLater( new Runnable()
    		{
    			public void run()
    			{
    				new TechStudentFrame();
    			}
    		} );
     
    	} // end method main()
     
    } // end class TechStudentFrame

Similar Threads

  1. HELP - GUI Java Applet Calculator..
    By AtOmTen in forum AWT / Java Swing
    Replies: 2
    Last Post: March 24th, 2013, 06:13 AM
  2. Tic Tac Toe GUI applet help!
    By bulletsster in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 22nd, 2013, 09:24 PM
  3. Tic Tac Toe GUI applet help!
    By bulletsster in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 21st, 2013, 09:06 PM
  4. Replies: 1
    Last Post: June 25th, 2010, 06:59 AM
  5. [SOLVED] applet vs. gui app
    By rptech in forum AWT / Java Swing
    Replies: 3
    Last Post: August 27th, 2009, 09:13 AM

Tags for this Thread