1

I am trying to make my buttons have the same size in my GUI. However, whenever I setPreferredSize(), Nimbus seems to automatically set the height of some buttons. What is the best way to get Nimbus to make all buttons have the same size (i.e. width and height)? In the sample code below, the height is correct for the buttons in the first row, but the width is not. In the second row, the width is correct but the height is not. How can I get all buttons to appear the same size in Nimbus?

public class SampleNimbusProblem extends javax.swing.JDialog {

    /** Creates new form SampleNimbusProblem */
    public SampleNimbusProblem(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        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() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        okButton1 = new javax.swing.JButton();
        cacelButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jPanel1.setLayout(new java.awt.GridBagLayout());

        okButton.setText("OK");
        okButton.setMaximumSize(new java.awt.Dimension(65, 23));
        okButton.setMinimumSize(new java.awt.Dimension(65, 23));
        okButton.setPreferredSize(new java.awt.Dimension(65, 23));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(okButton, gridBagConstraints);

        cancelButton.setText("Cancel");
        cancelButton.setMaximumSize(new java.awt.Dimension(65, 23));
        cancelButton.setMinimumSize(new java.awt.Dimension(65, 23));
        cancelButton.setPreferredSize(new java.awt.Dimension(65, 23));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(cancelButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jPanel1, gridBagConstraints);

        jPanel2.setLayout(new java.awt.GridBagLayout());

        okButton1.setText("OK");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel2.add(okButton1, gridBagConstraints);

        cacelButton2.setText("Cancel");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel2.add(cacelButton2, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jPanel2, gridBagConstraints);

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

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                SampleNimbusProblem dialog = new SampleNimbusProblem(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton cacelButton2;
    private javax.swing.JButton cancelButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JButton okButton;
    private javax.swing.JButton okButton1;
    // End of variables declaration

}
meyerjp3
  • 197
  • 1
  • 3
  • 16

3 Answers3

2

One way to standardize the size is to put components into a GridLayout. See Laying Out Components Within a Container for more details.


..whenever I setPreferredSize(),

BTW - It is rarely, if ever, necessary to set a preferred size. Let the layout manager (with appropriate padding & borders) do the heavy lifting of calculating the correct size of components across different screen-resolutions, sizes, PLAFs and versions.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thanks. Although GridLayout does not give me much control over the layout, the only solution seems to be the use of a different LayoutManager. GridBagConstraints does not seem to work well with JButtons in Nimbus. I am able to get the right layout using GroupLayout. – meyerjp3 Mar 25 '12 at 16:02
  • The `GridLayout` will make the buttons evenly sized, and is an easy way to do it. If you need different effects in different parts of the GUI (as is common), look to a [nested layout](http://stackoverflow.com/a/5630271/418556). But are you saying you already have a working layout with `GroupLayout`? – Andrew Thompson Mar 25 '12 at 16:45
  • Yes, I am using a nested layout now. There are multiple panels that each have their own layout. I was using GridBagConstraint for each panel, but I could not get the layout to work right in Nimbus. The main layout manager for all the panels in my JDialog is GridbagConstraints. However, my button panel is now using GroupLayout and it seems to be working fine. Thanks for all of the suggestions! – meyerjp3 Mar 25 '12 at 17:25
  • Glad you got it sorted. :) You should an example as the answer and mark it as correct. – Andrew Thompson Mar 25 '12 at 17:44
2

You are right Nimbus pretty buggy (development ended somewhere in first quater) and in some cases ignored set for PreferredSize, this reason why there are Resizing a Component

mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

Here is the way I wanted the example to appear. Using GroupLayout did the trick. It appears correctly in the Nimbus LAF. Thanks again for the help. I guess I need to reconsider my preference for GridBagLayout.

public class SampleNimbusProblem extends javax.swing.JDialog {

/** Creates new form SampleNimbusProblem */
public SampleNimbusProblem(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    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() {
    java.awt.GridBagConstraints gridBagConstraints;

    jPanel1 = new javax.swing.JPanel();
    okButton = new javax.swing.JButton();
    cancelButton = new javax.swing.JButton();
    okButton1 = new javax.swing.JButton();
    cacelButton2 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    getContentPane().setLayout(new java.awt.GridBagLayout());

    okButton.setText("OK");
    okButton.setMaximumSize(new java.awt.Dimension(65, 23));
    okButton.setMinimumSize(new java.awt.Dimension(65, 23));
    okButton.setPreferredSize(new java.awt.Dimension(65, 23));

    cancelButton.setText("Cancel");
    cancelButton.setMaximumSize(new java.awt.Dimension(65, 23));
    cancelButton.setMinimumSize(new java.awt.Dimension(65, 23));
    cancelButton.setPreferredSize(new java.awt.Dimension(65, 23));

    okButton1.setText("OK");

    cacelButton2.setText("Cancel");

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGap(5, 5, 5)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                    .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(10, 10, 10)
                    .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                    .addComponent(okButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(cacelButton2))))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGap(5, 5, 5)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(cacelButton2)
                .addComponent(okButton1))
            .addContainerGap(102, Short.MAX_VALUE))
    );

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.insets = new java.awt.Insets(10, 125, 112, 125);
    getContentPane().add(jPanel1, gridBagConstraints);

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

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            SampleNimbusProblem dialog = new SampleNimbusProblem(new javax.swing.JFrame(), true);
            dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowClosing(java.awt.event.WindowEvent e) {
                    System.exit(0);
                }
            });
            dialog.setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton cacelButton2;
private javax.swing.JButton cancelButton;
private javax.swing.JPanel jPanel1;
private javax.swing.JButton okButton;
private javax.swing.JButton okButton1;
// End of variables declaration                   

}

meyerjp3
  • 197
  • 1
  • 3
  • 16