Solved

Problem getting gridbag to give me symetry

Posted on 2004-08-09
13
217 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now