Frustrations with layouts

S

Sam Takoy

Hi,

In the following code, I'm trying to get the two buttons to be the same
width. They remain different widths, however, the panel adjusts width to
accommodate the preferred size. What gives? I have 1.6.0_18(I have
always found laying out with Swing to be an incredible pita.)


import javax.swing.*;

public class Test {

public static void main(String[] args) {
JButton b1 = new JButton("A");
JButton b2 = new JButton("BB");

b1.setPreferredSize(new java.awt.Dimension(200, 20));
b2.setPreferredSize(new java.awt.Dimension(200, 20));

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(b1);
panel.add(b2);

JFrame frame = new JFrame("Test");
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
 
M

markspace

Sam said:
Hi,

In the following code, I'm trying to get the two buttons to be the same
width. They remain different widths, however, the panel adjusts width to
accommodate the preferred size. What gives? I have 1.6.0_18(I have


Yeah, many layout managers will ignored preferred sizes. Use a layout
that supports what you want to do:

<http://java.sun.com/docs/books/tutorial/uiswing/layout/group.html>

always found laying out with Swing to be an incredible pita.)


Use Matisse:

<http://netbeans.org/kb/docs/java/quickstart-gui.html>


This took me about 1 minute in Matisse:

/*
* WidthTest.java
*
* Created on Apr 3, 2010, 8:31:00 AM
*/

package test;


public class WidthTest extends javax.swing.JFrame {

/** Creates new form WidthTest */
public WidthTest() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();


setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Width Test");


jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Buttons
with same width"));

jButton1.setText("A");

jButton2.setText("BBBBBBBBB");

javax.swing.GroupLayout jPanel1Layout = new
javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()

..addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap(271, Short.MAX_VALUE))
);

jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL,
new java.awt.Component[] {jButton1, jButton2});

jPanel1Layout.setVerticalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jButton2)
.addContainerGap(176, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);

pack();
}// </editor-fold>

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new WidthTest().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JPanel jPanel1;
// End of variables declaration

}
 
L

Lew

markspace said:
This took me about 1 minute in Matisse:

/*
* WidthTest.java
*
* Created on Apr 3, 2010, 8:31:00 AM
*/

package test;


public class WidthTest extends javax.swing.JFrame {

/** Creates new form WidthTest */
public WidthTest() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

I am disturbed by the use of '@SuppressWarnings("unchecked")' at the method
level and with no comments whatsoever about why it's safe, or even necessary,
to do so.
 
M

markspace

Lew said:
I am disturbed by the use of '@SuppressWarnings("unchecked")' at the
method level and with no comments whatsoever about why it's safe, or
even necessary, to do so.


A valid point. I think a partial answer is that it's generated code,
and the code generator is assumed to be correct (i.e., the person
writing the generator knew what they were doing). I don't read
generated code much, it's just thrown away and re-generated if need be.
 
L

Lew

A valid point. I think a partial answer is that it's generated code,
and the code generator is assumed to be correct (i.e., the person
writing the generator knew what they were doing). I don't read
generated code much, it's just thrown away and re-generated if need be.

But of course you'd never do that in a team environment or any context where
others might have to read your code and wonder.

Oops, almost forgot:

:)

for the sake of the namby-pamby poo-poo-heads with no sense of humor.

(Just because I was making a serious point doesn't mean the comment wasn't
intended to be humorous.)

;-)
 
J

John B. Matthews

Sam Takoy said:
In the following code, I'm trying to get the two buttons to be the same
width. They remain different widths, however, the panel adjusts width to
accommodate the preferred size. What gives? I have 1.6.0_18(I have
always found laying out with Swing to be an incredible pita.)


import javax.swing.*;

public class Test {

public static void main(String[] args) {
JButton b1 = new JButton("A");
JButton b2 = new JButton("BB");

b1.setPreferredSize(new java.awt.Dimension(200, 20));
b2.setPreferredSize(new java.awt.Dimension(200, 20));

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(b1);
panel.add(b2);

JFrame frame = new JFrame("Test");
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}

You can also set the minimum, preferred and maximum sizes, as discussed
in "Specifying Component Sizes", but the resulting layout is rather
rigid in the face of changing fonts, locales and platforms.

<http://java.sun.com/docs/books/tutorial/uiswing/layout/box.html>
 
R

Roedy Green

In the following code, I'm trying to get the two buttons to be the same
width. They remain different widths, however, the panel adjusts width to
accommodate the preferred size. What gives? I have 1.6.0_18(I have
always found laying out with Swing to be an incredible pita.)

BoxLayout is not intended for fine posititioning.

Try GridBagLayout or write you own LayoutManager. It is much easier
than you might think.

See http://mindprod.com/jgloss/layout.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

By 2009, computers will disappear. Displays will be written directly onto our retinas by devices in our eyeglasses and contact lenses.
~ Ray Kurzweil (born: 1948-02-12 age: 62)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top