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 7 of 7

Thread: Problem with swing GUI components in JPanel

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with swing GUI components in JPanel

    Hi everyone,

    I am writing a program with a login screen. That's two labels and one textfield and a passwordField.
    Now, when i want to run the program, i am seeing the JFram, the Panel and one label. The second label and two fields aren't showing up at first. Afther i resize the JFrame with dragging the mouse the other label and fields appear.

    Why it doesn't show when the program is started?

    Kind regards
    Wim


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Problem with swing GUI components in JPanel

    Probably because you did something wrong in your code.
    But we cant really tell because we dont know your code.

    When you post your code make sure to put it inside [code] brackets.

  3. #3
    Junior Member
    Join Date
    Aug 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with swing GUI components in JPanel

    Ok i am sorry that i didn't include the code.

    Here it is:
    package kim.contracts.view;
     
    import java.awt.*;
    import javax.swing.*; 
    import javax.swing.border.Border;
     
    public class LoginView
    {
    	public LoginView()
    	{
    		JFrame loginFrame;
    		JLabel login, username, password;
    		JTextField usernametxt;
    		JPasswordField passwordtxt;
    		JPanel loginPanel;
    		Border border;
    		JButton loginBtn, resetBtn;
     
    		loginFrame = new JFrame("Contracts Login");
    		login = new JLabel("Log Hier In:");
    		loginPanel = new JPanel(new GridBagLayout());
    		border = BorderFactory.createLineBorder(Color.BLUE);
     
     
    		loginFrame.setSize(600,300);
    		loginFrame.setLayout(null);
    		loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		loginFrame.setLocation(600,400);
    		loginFrame.setVisible(true);
     
    		login.setSize(100,20);
    		login.setVisible(true);
    		login.setLocation(50,5);
    		loginFrame.add(login, BorderLayout.NORTH);
     
    		loginPanel.setSize(500,200);
    		loginPanel.setBorder(border);
    		loginPanel.setLocation(40,30);
    		loginPanel.setVisible(true);
    		loginFrame.add(loginPanel, BorderLayout.CENTER);
     
    		GridBagConstraints gbc = new GridBagConstraints();
    		gbc.insets =  new Insets(5,15,0,0);
     
    		username = new JLabel("Gebruikersnaam: ");
    		gbc.gridx = 0;
    		gbc.gridy = 0;
    		loginPanel.add(username, gbc);
    		username.setSize(100,20);
    		username.setVisible(true);
     
    		usernametxt = new JTextField(10);
    		gbc.gridx = 1;
    		gbc.gridy = 0;
    		loginPanel.add(usernametxt, gbc);
    		usernametxt.setVisible(true);
     
    		password = new JLabel("Paswoord: ");
    		gbc.gridx = 0;
    		gbc.gridy = 1;
    		loginPanel.add(password, gbc);
    		username.setSize(100,20);
    		username.setVisible(true);
     
    		passwordtxt = new JPasswordField(10);
    		gbc.gridx = 1;
    		gbc.gridy = 1;
    		loginPanel.add(passwordtxt, gbc);
    		usernametxt.setVisible(true);
     
    		loginBtn = new JButton("Log in");
    		gbc.gridx = 0;
    		gbc.gridy = 2;
    		loginPanel.add(loginBtn, gbc);
    		loginBtn.setVisible(true);
     
    		resetBtn = new JButton("Reset velden");
    		gbc.gridx = 1;
    		gbc.gridy = 2;
    		loginPanel.add(resetBtn, gbc);
    		resetBtn.setVisible(true);
     
    	}
    }
    Last edited by Norm; September 8th, 2014 at 09:45 AM. Reason: Didn't find code tags / Added / to end tag

  4. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Problem with swing GUI components in JPanel

    The problem is you make your frame visible and afterwards you keep adding components to it.
    Once you make a frame visible the frame will then arrange its contents and paint itself. If you change something later you need to call either the pack() method on the frame, or validate().
    Or simply, make sure that loginFrame.setVisible(true) is the last thing you do in your code.

    By the way, some of your lines are very confusing to me and I dont understand why you do it that way. For example, you set "null" as the layout of your loginFrame, but later you try to add components to the frame using constraints from other layouts, namely the BorderLayout. That just cant work and it seems you are making a mistake.

  5. #5
    Junior Member
    Join Date
    Aug 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with swing GUI components in JPanel

    Ok thanks for the answer.

    Yes i tried to do it in one layoutmanager , but then i cant seem to set the panel on the right position. and to use no layout and work with setBounds would be bad practice i think?

  6. #6
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Problem with swing GUI components in JPanel

    A null layout is usually a very bad idea. It doesnt scale well, actually, it doesnt scale at all. And you have so much work to position all elements by hand.
    Just study the different layouts and dont be afraid to use several panels that use yet another layout to achieve what you want.

    Was your original problem solved by now?

  7. #7
    Junior Member
    Join Date
    Aug 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with swing GUI components in JPanel

    yes it is surely solved, thanks to your answer

    Thank you.

    --- Update ---

    PS: thanks for the extra information too.

Similar Threads

  1. Some swing components use wrong look and feel
    By rstrout in forum AWT / Java Swing
    Replies: 11
    Last Post: August 6th, 2013, 02:44 PM
  2. arranging components in a vertical manner in a jpanel?
    By buencamino in forum AWT / Java Swing
    Replies: 2
    Last Post: July 18th, 2012, 09:33 PM
  3. [SOLVED] Problem with image in swing gui
    By Pusillus in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 14th, 2012, 11:13 AM
  4. Swing Components Invisible as Startup
    By SpiceProgrammer in forum AWT / Java Swing
    Replies: 5
    Last Post: January 23rd, 2011, 02:26 PM
  5. count components inside a JPanel
    By dewboy3d in forum AWT / Java Swing
    Replies: 1
    Last Post: August 2nd, 2009, 03:17 PM