Solved

Problem getting gridbag to give me symetry

Posted on 2004-08-09
13
234 Views
Last Modified: 2008-03-10
Hi all,

I can't seem to get the gridbag layout to stop adjusting the size of cells relative to each other.  I have two lists side by side, and I want them to split the display exactly 50/50 no matter what.  

When neither has anything in, they are the same size.  However, when one of them gets some list elements, the amount of x axis room allocated to it increases, making the display lopsided.  Is there a way to stop this happening?  I thought that was the point of the weights?  

Here is the code (note: This isn't the exact code - I've cut several of the UI components out to keep it simple but the principle is the same)

    buildConstraints(constraints, 0, 0, 1, 1, 50, 100);
    constraints.fill = GridBagConstraints.BOTH;
    JList list1 = new JList(nameList);
    list1 .setLayoutOrientation(JList.VERTICAL);
    list1 .setVisibleRowCount( -1);
    JScrollPane pane1= new JScrollPane(
      list1 , JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    gridbag.setConstraints(pane1, constraints);
    mainPane.add(pane1);

    buildConstraints(constraints, 1, 0, 1, 1, 50, 0);
    constraints.fill = GridBagConstraints.BOTH;
    JList list2 = new JList();
    list2 .setLayoutOrientation(JList.VERTICAL);
    list2 .setVisibleRowCount( -1);
    JScrollPane pane2 = new JScrollPane(
      list2 , JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    gridbag.setConstraints(pane2 , constraints);
    mainPane.add(pane2 );
0
Comment
Question by:Dawkins
  • 5
  • 5
  • 3
13 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 11757356
>> I want them to split the display exactly 50/50 no matter what.
Then you should better use the grid layout.
Just a quick demo:

/*
 * BoxDemo.java
 *
 */

public class BoxDemo extends javax.swing.JFrame {
   
    public BoxDemo() {
        initComponents();
    }
   
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jPanel1.setLayout(new java.awt.GridLayout());

        jButton1.setText("jButton1");
        jPanel1.add(jButton1);

        jButton2.setText("jButton2");
        jPanel1.add(jButton2);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }
   
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
   
    public static void main(String args[]) {
        new BoxDemo().show();
    }
   
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jPanel1;
}

As you can see - if you run it - the two buttons will always extend to 50% of the available space.
Well, replace each button by a JPanel that contains your lists and you have what you want
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11757425
>> the two buttons will always extend to 50% of the available space.
You have to resize to see that of course.

Well, here you have the same demo app with the lists you need. Just try it.

/*
 * BoxDemo.java
 *
 */

public class BoxDemo extends javax.swing.JFrame {
   
    public BoxDemo() {
        initComponents();
    }
   
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        panel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        panel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jPanel1.setLayout(new java.awt.GridLayout());

        panel1.setLayout(new java.awt.BorderLayout());

        panel1.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(4, 4, 4, 2)));
        jList1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0)));
        jScrollPane1.setViewportView(jList1);

        panel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel1.add(panel1);

        panel2.setLayout(new java.awt.BorderLayout());

        panel2.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(4, 2, 4, 4)));
        jList2.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0)));
        jScrollPane2.setViewportView(jList2);

        panel2.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jPanel1.add(panel2);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }
   
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
   
    public static void main(String args[]) {
        new BoxDemo().show();
    }
   
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPanel panel1;
    private javax.swing.JPanel panel2;
}
0
 

Author Comment

by:Dawkins
ID: 11757570
Hi zzynx,

the problem is, I actually want to add more components (a narrow column of buttons between the two lists.)  So I need to use gridbag (I think!).  Sorry if I didn't make that clear in the original question.

I considered a border layout but it expands the center to fill in remaining space.  I want the opposite - two lists on either side taking up all available space and a narrow column in the middle just big enough for the buttons.
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 92

Expert Comment

by:objects
ID: 11758368
Use a horizontal BoxLayout, and set the maximum width for your two lists.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11760760
>>I want the opposite - two lists on either side taking up all available space and a narrow column in the middle just big enough for the buttons.

/*
 * BoxDemo.java
 *
 */

public class BoxDemo extends javax.swing.JFrame {
   
    public BoxDemo() {
        initComponents();
    }
   
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        panel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        midPanel = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        panel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.X_AXIS));

        panel1.setLayout(new java.awt.BorderLayout());

        panel1.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(4, 4, 4, 2)));
        jList1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0)));
        jScrollPane1.setViewportView(jList1);

        panel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel1.add(panel1);

        jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.Y_AXIS));

        jButton1.setText("jButton1");
        jPanel2.add(jButton1);

        jButton2.setText("jButton2");
        jPanel2.add(jButton2);

        midPanel.add(jPanel2);

        jPanel1.add(midPanel);

        panel2.setLayout(new java.awt.BorderLayout());

        panel2.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(4, 2, 4, 4)));
        jList2.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0)));
        jScrollPane2.setViewportView(jList2);

        panel2.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jPanel1.add(panel2);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }
   
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
   
    public static void main(String args[]) {
        new BoxDemo().show();
    }
   
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPanel midPanel;
    private javax.swing.JPanel panel1;
    private javax.swing.JPanel panel2;
}

Remark:
And if you want the buttons to be vertically centered then add

        midPanel.setLayout(new javax.swing.BoxLayout(midPanel, javax.swing.BoxLayout.X_AXIS));

after the creation of midPanel [ midPanel = new javax.swing.JPanel(); ]
0
 
LVL 92

Expert Comment

by:objects
ID: 11760785
What would you do without my suggestions :-D
0
 

Author Comment

by:Dawkins
ID: 11760827
> Use a horizontal BoxLayout, and set the maximum width for your two lists

OK I've set it up with a horizontal BoxLayout but I can't see how to set widths.
0
 

Author Comment

by:Dawkins
ID: 11760872
Just seen your most recent suggestion zzynx - taking a look at it now
0
 
LVL 92

Expert Comment

by:objects
ID: 11760878
>  I can't see how to set widths.

use the Componment methods:

setPreferredSize()
setMaximumSize()
setMinimumSize()
0
 

Author Comment

by:Dawkins
ID: 11761030
zzynx:  the boxdemo code you posted gets the same problem - when actual data is added to one of the list components they change size.  What's really annoying is it actually *shrinks* when data is put in it!

Objects:  that sets fixed pixel dimensions but I would like the window to be resizable.

I am amazed it is this hard - it's not as if I want anything amazing - just 3 columns of fixed proportions! :)
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 11761047
> Objects:  that sets fixed pixel dimensions but I would like the window to be resizable.

No it doesn't, it just gives hints to the layout manager which you can set appropriately.

0
 

Author Comment

by:Dawkins
ID: 11761303
Ah yes sorry, setPreferredSize() seems to do exactly what I want, thanks!
0
 
LVL 92

Expert Comment

by:objects
ID: 11768646
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question