[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Problem getting gridbag to give me symetry

Posted on 2004-08-09
13
Medium Priority
?
257 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 200 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

649 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