Frustrations with layouts

Discussion in 'Java' started by Sam Takoy, Apr 3, 2010.

  1. Sam Takoy

    Sam Takoy Guest

    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);
    }
    }
     
    Sam Takoy, Apr 3, 2010
    #1
    1. Advertising

  2. Sam Takoy

    markspace Guest

    Sam Takoy wrote:
    > 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>

    <http://java.sun.com/javase/6/docs/api/javax/swing/GroupLayout.html#linkSize%28java.awt.Component...%29>


    > 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

    }
     
    markspace, Apr 3, 2010
    #2
    1. Advertising

  3. Sam Takoy

    Lew Guest

    markspace wrote:
    > 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.

    --
    Lew
     
    Lew, Apr 3, 2010
    #3
  4. Sam Takoy

    markspace Guest

    Lew wrote:

    > 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.
     
    markspace, Apr 3, 2010
    #4
  5. Sam Takoy

    Lew Guest

    Lew wrote:
    >> 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.


    markspace wrote:
    > 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.)

    ;-)

    --
    Lew
    >:)
     
    Lew, Apr 3, 2010
    #5
  6. In article <hp7ldg$bqs$-september.org>,
    Sam Takoy <> wrote:

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

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Apr 3, 2010
    #6
  7. Sam Takoy

    Roedy Green Guest

    On Sat, 03 Apr 2010 11:04:15 -0400, Sam Takoy <>
    wrote, quoted or indirectly quoted someone who 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.)


    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)
     
    Roedy Green, Apr 3, 2010
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeffrey Silverman

    Slightly OT: MSIE bugs and frustrations

    Jeffrey Silverman, Nov 9, 2004, in forum: HTML
    Replies:
    4
    Views:
    379
    brucie
    Nov 10, 2004
  2. Ramon F Herrera

    Swing layouts vs. SWT layouts?

    Ramon F Herrera, Nov 10, 2007, in forum: Java
    Replies:
    5
    Views:
    2,657
    Benjamin Toren
    Dec 7, 2009
  3. sean_walsh

    Frustrations with Error Logging

    sean_walsh, Jul 30, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    322
    sloan
    Jul 30, 2008
  4. Marty Ryba
    Replies:
    6
    Views:
    1,210
  5. Sean Armstrong

    More MySQL-Ruby Frustrations!

    Sean Armstrong, Sep 16, 2005, in forum: Ruby
    Replies:
    7
    Views:
    106
    Sean Armstrong
    Sep 17, 2005
Loading...

Share This Page