Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gridBagLayout vs absoluteLayout

I'm trying to make a GUI for a Bank Application in JAVA. Using absolute layout for my frame is so easy to create anything if I make use of WindowBuilder in eclipse but the problem with it is when I resize the frame. So that's why I chose to use gridBagLayout where I can use weightx/y to make my job easier. I kind of forgot how to use this layout properly so I got stuck at my first attempt to add a JPanel in my gridBagLayout main panel.

This is what I want to achieve (made in absolute layout):

enter image description here

And this is what I have (made in gridBagLayout):

enter image description here

If someone can point me what I have to change/add when I first add the flowLayout panel I would appreciate. Basically the white space of the first cell - I want to get rid of it and control it!

Here is some code:

    setBackground(new Color(255, 255, 255));
    GridBagLayout gridBagLayout = new GridBagLayout();
    gridBagLayout.columnWidths = new int[]{0, 0};
    gridBagLayout.rowHeights = new int[]{0, 0};
    gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
    gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
    setLayout(gridBagLayout);

    JPanel panel = new JPanel();
    FlowLayout flowLayout = (FlowLayout) panel.getLayout();
    flowLayout.setVgap(15);
    flowLayout.setHgap(15);
    flowLayout.setAlignment(FlowLayout.LEFT);
    panel.setBackground(new Color(51, 102, 204));
    GridBagConstraints gbc_panel = new GridBagConstraints();
    gbc_panel.anchor = GridBagConstraints.NORTH;
    gbc_panel.fill = GridBagConstraints.HORIZONTAL;
    gbc_panel.gridx = 0;
    gbc_panel.gridy = 0;
    add(panel, gbc_panel);

    JLabel label = new JLabel("European Bank");
    label.setForeground(Color.WHITE);
    label.setFont(new Font("Tahoma", Font.PLAIN, 25));
    panel.add(label);

    JLabel lblYourBankAccounts = new JLabel("Your Bank Accounts");
    lblYourBankAccounts.setForeground(new Color(153, 153, 153));
    lblYourBankAccounts.setFont(new Font("Tahoma", Font.PLAIN, 19));
    GridBagConstraints gbc_label = new GridBagConstraints();
    gbc_label.insets = new Insets(0, 60, 0, 0);
    gbc_label.anchor = GridBagConstraints.LINE_START;
    gbc_label.gridx = 0;
    gbc_label.gridy = 1;
    add(lblYourBankAccounts, gbc_label);

    JScrollPane scrollPane = new JScrollPane();
    GridBagConstraints gbc_scrollPane = new GridBagConstraints();
    gbc_scrollPane.insets = new Insets(10, 60, 10, 10);
    gbc_scrollPane.weightx = 1.0;
    gbc_scrollPane.weighty = 1.0;
    gbc_scrollPane.gridx = 0;
    gbc_scrollPane.gridy = 2;
    gbc_scrollPane.fill = GridBagConstraints.BOTH;
    add(scrollPane, gbc_scrollPane);
like image 992
Chris Avatar asked Nov 12 '22 00:11

Chris


1 Answers

I would recommend the built in Layout Customizer in Netbeans. It's easy to learn from it. Hope it helps.

For Your type of problem try to adjust the weights of the component You want to strech.

Here's a screenshot how to do it: http://goo.gl/TBPY9i

like image 110
Thomas Avatar answered Nov 15 '22 13:11

Thomas