GridBagLayout

Empty Frame Shown in Output

Page 1 of 1

3 Replies - 1238 Views - Last Post: 27 May 2010 - 04:43 PM Rate Topic: -----

#1 sh1n3   User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 164
  • Joined: 22-April 10

GridBagLayout

Posted 27 May 2010 - 07:48 AM

I recently created a frame with a panel that has a GridBagLayout but when I compile and execute there is only an empty frame being show although I have added 4 labels.
Here comes the code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class gridTest {
        public static void main(String[] args) {
        JFrame frame=new JFrame("Grid Test");
        frame.setSize(500,500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        JPanel panel=new JPanel(new GridBagLayout());
        GridBagConstraints c=new GridBagConstraints();
        JLabel label1=new JLabel("Label 1");
        c.gridx=0;
        c.gridy=0;
        panel.add(label1, c);
        JLabel label2=new JLabel("Label 2");
        c.gridx=0;
        c.gridy=1;
        panel.add(label2, c);
        JLabel label3=new JLabel("Label 3");
        c.gridx=1;
        c.gridy=0;
        panel.add(label3, c);
        JLabel label4=new JLabel("Label 4");
        c.gridx=1;
        c.gridy=1;
        panel.add(label4, c);
        frame.add(panel);
        }
}



The problem is when I compile it there is not a single error shown, I don't know whats wrong.


Is This A Good Question/Topic? 0
  • +

Replies To: GridBagLayout

#2 m-e-g-a-z   User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,457
  • Joined: 19-October 09

Re: GridBagLayout

Posted 27 May 2010 - 08:05 AM

I've compiled your code without changing anything and can see the four JLabels bang in the middle.

You should create a Constructor for the gridTest class like this, it is bad to have all your gui components within your main(). Creating an instance will be enough as you have a constructor.

public gridTest(){
//gui stuff here
}



public static void main (String [] args) {

new gridTest();

}



Edit - Also, you want to make the frame visible right at the end once you have added all the componets etc..

This post has been edited by m-e-g-a-z: 27 May 2010 - 08:07 AM

Was This Post Helpful? 0
  • +
  • -

#3 sh1n3   User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 164
  • Joined: 22-April 10

Re: GridBagLayout

Posted 27 May 2010 - 08:16 AM

I did like you said, looks better now. Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

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

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: GridBagLayout

Posted 27 May 2010 - 04:43 PM

Stay away for GridBagLayout()
leave it to code generators
I you want to use hardcoded value use a null layout
there are always easier to manage work around and here are free ones that I use in 80% of the applications that I delivered for the last 2 years especially the ParagraphLayout

http://jhlabs.com/ja...yout/index.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1