Solved

Problem getting gridbag to give me symetry

Posted on 2004-08-09
13
243 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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.

726 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