Skip to main content

How to set JButton preferredSize() when using Nimbus LAF?

1 reply [Last post]
meyerjp
Offline
Joined: 2009-07-15
Points: 0

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?

Here is a new example. How can I get the buttons to have the same size?

<code>



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

}

</code>

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
infosun
Offline
Joined: 2006-10-13
Points: 0

Hey,

i do it this way:

private final class YearField extends JButton
{
public YearField()
{
setFocusable(false);
UIDefaults def = new UIDefaults();
def.put("Button.contentMargins", new Insets(6,8,6,8));
putClientProperty("Nimbus.Overrides.InheritDefaults", true);
putClientProperty("Nimbus.Overrides", def);
updateUI();
}
}