#1
  1. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,534
    Rep Power
    595

    Odd JFrame Behavior


    I've designed a few windows but have encountered something new that I don't understand. I have a JFrame with some JLabels, JTextFields and a JButton. The odd part is that the bottom component is always centered on the window and full size rather than where I place it. If I add another component below the bottom, then the previous bottom one goes exactly where I want and the new one is then centered. It acts like it is ignoring 'setBounds' when it is the bottom component.
    Code:
    public void initialize() {
    		
    	regScreen=new JFrame();
    	regScreen.getContentPane().setBounds(new Rectangle(200, 200, 400, 200));
    	regScreen.getContentPane().setFont(new Font("Times New Roman", Font.PLAIN, 12));
    	regScreen.setTitle("KCBS Events Search Registration");
    	regScreen.setBounds(200, 200, 400, 200);
    		
    	int top=5;
    	int indent=5;
    		
    	JLabel lblName = new JLabel("Name:");
    	lblName.setBounds(indent, top, 50, 20);
    	lblName.setFont(new Font("Times New Roman", Font.PLAIN, 12));
    	lblName.setHorizontalAlignment(SwingConstants.LEFT);
    	regScreen.getContentPane().add(lblName);
    		
    	JTextField name=new JTextField();
    	name.setToolTipText("Enter Name");
    	name.setHorizontalAlignment(SwingConstants.LEFT);
    	name.setFont(new Font("Times New Roman", Font.PLAIN, 12));
    	name.setBounds(indent+130, top, 200, 20);
    	regScreen.getContentPane().add(name);
    		
    	JLabel lblNumber = new JLabel("KCBS Member Number:");
    	lblNumber.setBounds(indent, top+30, 120, 20);
    	lblNumber.setFont(new Font("Times New Roman", Font.PLAIN, 12));
    	lblNumber.setHorizontalAlignment(SwingConstants.LEFT);
    	regScreen.getContentPane().add(lblNumber);
    		
    	JTextField number=new JTextField();
    	number.setToolTipText("Enter Member Number");
    	number.setHorizontalAlignment(SwingConstants.LEFT);
    	number.setFont(new Font("Times New Roman", Font.PLAIN, 12));
    	number.setBounds(indent+130, top+30, 80, 20);
    	regScreen.getContentPane().add(number);
    		
    	JLabel lblKeyCode = new JLabel("Key Code:");
    	lblKeyCode.setBounds(indent, top+60, 80, 20);
    	lblKeyCode.setFont(new Font("Times New Roman", Font.PLAIN, 12));
    	lblKeyCode.setHorizontalAlignment(SwingConstants.LEFT);
    	regScreen.getContentPane().add(lblKeyCode);
    		
    	JTextField keyCode=new JTextField();
    	keyCode.setToolTipText("Enter Provided Key Code");
    	keyCode.setHorizontalAlignment(SwingConstants.LEFT);
    	keyCode.setFont(new Font("Times New Roman", Font.PLAIN, 12));
    	keyCode.setBounds(indent+130, top+60, 200, 20);
    	regScreen.getContentPane().add(keyCode);
    		
    	JButton submit=new JButton("Register");
    	submit.setFont(new Font("Times New Roman", Font.PLAIN, 12));
    	submit.setBounds(indent+130, top+110, 80, 20);
    	submit.setHorizontalAlignment(SwingConstants.NEXT);
    	submit.setVerticalAlignment(SwingConstants.CENTER);
    	regScreen.getContentPane().add(submit);
    		
    	JLabel dummy=new JLabel("Dummy Text");
    	dummy.setFont(new Font("Times New Roman", Font.PLAIN, 12));
    	dummy.setBounds(indent, top+90, 30, 50);
    	regScreen.getContentPane().add(dummy);
    		
    }
    I am complete flummoxed by this behavior. The window is exactly the way I want except for the 'Dummy Text' which is in the middle of the window (it is the entire size of the window and overlays everything else but while it is being rendered I can see everything else is correct). But if I remove it, then the button is in the middle and covers everything else. I compared it with other windows I've created and must be missing something but damfino what.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    How are you using the layout manager of the container you are adding the components to?

    Comments on this post

    • gw1500se agrees : Thanks.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,534
    Rep Power
    595
    Bingo! That's what I was missing and couldn't find, 'setLayout(null)'. Thanks yet again.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.

IMN logo majestic logo threadwatch logo seochat tools logo