Customizing GridBagLayout in PropertyEditor of Container

Swing Designer allows you to quickly create the frames, panels, dialogs, applets and other UI elements that comprise Java Swing applications.

Moderators: Konstantin.Scheglov, gnebling, Alexander.Mitin, jwren, Eric Clayberg

Customizing GridBagLayout in PropertyEditor of Container

Postby chrisR » Wed Aug 05, 2009 8:10 am

Hi,

recently I've learned how to add and remove columns and rows on the design page per mouse.
The Properties of a Container shows the LayoutManager. But i'm not allowed to set or customize the Layoutmanager.
Furthermore SwingDesigner sets a default dummy column and row with weight 1.0E-4 which leads to a gap between the last component
on the right (bottom) and the border.
How do I remove this default dummy column?
The customization of the Layoutmanager is sufficient, however it is nice to have the GridBagLayout editable within the Properties Table.

Kind regards
Chris
chrisR
 
Posts: 19
Joined: Thu Jul 23, 2009 5:07 am

Re: Customizing GridBagLayout in PropertyEditor of Container

Postby Eric Clayberg » Wed Aug 05, 2009 8:37 am

It would be helpful to know what your goal is in removing the "dummy" column. Doing so will just give you a gap before the first column and after the last (e.g., all of the components end up centered rather than left-justified). Any column(s) may be set to grow to fill the available space which eliminates the gap after the last column.

Image Image

You can experiment with it in the source view, if you want to see the effect of removing the dummy column.

Double-clicking on the column or row header will allow you to set the properties for that column or row.

Image

The GridBagConstraints associated with each component may be adjusted using the the layout assistant or property table, if you like.

Image Image
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA

Re: Customizing GridBagLayout in PropertyEditor of Container

Postby chrisR » Thu Aug 06, 2009 1:24 am

You are right.
The dummy column and row fulfil a necessary function.
However, the dummy column and row causes a slight asymmetry.
Here is a brief example of a Dialog with two nested Panels.
The first Panel has an EmptyBorder with Insets (2,2,2,2) the nested Panel an EtchedBorder to show.
Here is the code produced by SwingDesigner with dummy column
Code: Select all
public class TestDialog
   extends JDialog {
   private JPanel pnl;
   private JPanel pnl_1;

   public static void main(String[] args) {
      TestDialog dialog = new TestDialog();
      dialog.setSize(100, 100);
      dialog.setVisible(true);

   }
   public TestDialog() {
      initialize();
   }
   private void initialize() {
      GridBagLayout gridBagLayout = new GridBagLayout();
      gridBagLayout.columnWidths = new int[]{0, 0};
      gridBagLayout.rowHeights = new int[]{0, 0};
      gridBagLayout.columnWeights = new double[]{1.0, 1.0E-4};
      gridBagLayout.rowWeights = new double[]{1.0, 1.0E-4};
      getContentPane().setLayout(gridBagLayout);
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.fill = GridBagConstraints.BOTH;
      gbc.gridx = 0;
      gbc.gridy = 0;
      getContentPane().add(getPnl(), gbc);
      GridBagConstraints gbc_1 = new GridBagConstraints();
      gbc_1.fill = GridBagConstraints.BOTH;
      gbc_1.gridx = 0;
      gbc_1.gridy = 0;
      getPnl().add(getPnl_1(), gbc_1);
   }
   protected JPanel getPnl() {
      if (pnl == null) {
         pnl = new JPanel();
         pnl.setBorder(new EmptyBorder(2, 2, 2, 2));
         GridBagLayout gridBagLayout = new GridBagLayout();
         gridBagLayout.columnWidths = new int[]{0, 0};
         gridBagLayout.rowHeights = new int[]{0, 0};
         gridBagLayout.columnWeights = new double[]{1.0, 1.0E-4};
         gridBagLayout.rowWeights = new double[]{1.0, 1.0E-4};
         pnl.setLayout(gridBagLayout);
      }
      return pnl;
   }
   protected JPanel getPnl_1() {
      if (pnl_1 == null) {
         pnl_1 = new JPanel();
         pnl_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
         GridBagLayout gridBagLayout = new GridBagLayout();
         gridBagLayout.columnWidths = new int[]{0};
         gridBagLayout.rowHeights = new int[]{0};
         gridBagLayout.columnWeights = new double[]{1.0E-4};
         gridBagLayout.rowWeights = new double[]{1.0E-4};
         pnl_1.setLayout(gridBagLayout);
      }
      return pnl_1;
   }
}


and here without dummy column
Code: Select all
public class TestDialog
   extends JDialog {
   private JPanel pnl;
   private JPanel pnl_1;

   public static void main(String[] args) {
      TestDialog dialog = new TestDialog();
      dialog.setSize(100, 100);
      dialog.setVisible(true);

   }
   public TestDialog() {
      initialize();
   }
   private void initialize() {
      GridBagLayout gridBagLayout = new GridBagLayout();
      //      gridBagLayout.columnWidths = new int[]{0, 0};
      //      gridBagLayout.rowHeights = new int[]{0, 0};
      //      gridBagLayout.columnWeights = new double[]{1.0, 1.0E-4};
      //      gridBagLayout.rowWeights = new double[]{1.0, 1.0E-4};
      gridBagLayout.columnWidths = new int[]{0};
      gridBagLayout.rowHeights = new int[]{0};
      gridBagLayout.columnWeights = new double[]{1.0};
      gridBagLayout.rowWeights = new double[]{1.0};
      getContentPane().setLayout(gridBagLayout);
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.fill = GridBagConstraints.BOTH;
      gbc.gridx = 0;
      gbc.gridy = 0;
      getContentPane().add(getPnl(), gbc);
      GridBagConstraints gbc_1 = new GridBagConstraints();
      gbc_1.fill = GridBagConstraints.BOTH;
      gbc_1.gridx = 0;
      gbc_1.gridy = 0;
      getPnl().add(getPnl_1(), gbc_1);
   }
   protected JPanel getPnl() {
      if (pnl == null) {
         pnl = new JPanel();
         pnl.setBorder(new EmptyBorder(2, 2, 2, 2));
         GridBagLayout gridBagLayout = new GridBagLayout();
         //         gridBagLayout.columnWidths = new int[]{0, 0};
         //         gridBagLayout.rowHeights = new int[]{0, 0};
         //         gridBagLayout.columnWeights = new double[]{1.0, 1.0E-4};
         //         gridBagLayout.rowWeights = new double[]{1.0, 1.0E-4};
         gridBagLayout.columnWidths = new int[]{0};
         gridBagLayout.rowHeights = new int[]{0};
         gridBagLayout.columnWeights = new double[]{1.0};
         gridBagLayout.rowWeights = new double[]{1.0};
         pnl.setLayout(gridBagLayout);
      }
      return pnl;
   }
   protected JPanel getPnl_1() {
      if (pnl_1 == null) {
         pnl_1 = new JPanel();
         pnl_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
         GridBagLayout gridBagLayout = new GridBagLayout();
         gridBagLayout.columnWidths = new int[]{0};
         gridBagLayout.rowHeights = new int[]{0};
         gridBagLayout.columnWeights = new double[]{1.0E-4};
         gridBagLayout.rowWeights = new double[]{1.0E-4};
         pnl_1.setLayout(gridBagLayout);
      }
      return pnl_1;
   }
}


You see the difference of the distances of the etched border between top and bottom and left and right.
Do you get an idea?

Kind regards
Chris
chrisR
 
Posts: 19
Joined: Thu Jul 23, 2009 5:07 am

Re: Customizing GridBagLayout in PropertyEditor of Container

Postby Eric Clayberg » Fri Aug 07, 2009 9:26 am

chrisR wrote:You see the difference of the distances of the etched border between top and bottom and left and right.

I think we can solve the problem you describe by using a smaller number like Double.MIN_VALUE. For example,

Code: Select all
         gridBagLayout.columnWeights = new double[]{Double.MIN_VALUE};
         gridBagLayout.rowWeights = new double[]{Double.MIN_VALUE};
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA


Return to Swing Designer

Who is online

Users browsing this forum: No registered users and 1 guest