1

How to make size of JToggleButton fixed and equal for both Selected and Not Selected states?

As you see below, I have a variable length button now:

Not Selected size:

enter image description here

And Selected size

enter image description here

I tried setSize() and setPreferedSize() methods, but nothing changed.

Current button method:

private void connectionTglBtnActionPerformed(java.awt.event.ActionEvent evt) {                                                 

        if (connectionTglBtn.isSelected()) {
            connectionTglBtn.setText("S");
        } else {
            connectionTglBtn.setText("SSSS");
        }
}

Update:

Here there is my layout manager initialization method:

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()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(readersComBox, javax.swing.GroupLayout.PREFERRED_SIZE, 338, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(refreshBtn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(connectionTglBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addComponent(jButton1)
                .addGap(0, 0, Short.MAX_VALUE)))
        .addContainerGap())
);
layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
            .addComponent(readersComBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(refreshBtn)
            .addComponent(connectionTglBtn))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jLabel1)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addComponent(jButton1)
        .addContainerGap())
);

In the above snippet, connectionTglBtn is the button that we are talking about.

STaefi
  • 4,297
  • 1
  • 25
  • 43
EbraHim
  • 2,279
  • 2
  • 16
  • 28
  • what is the layout manager you are using? – STaefi May 14 '16 at 06:55
  • @STaefi How can I detect which layout manager I am using? I used Netbeans tools to create my form using Drog&Drop. – EbraHim May 14 '16 at 06:58
  • search for where is `setLayout` method is called in your code. May be your are using `null` layout. – STaefi May 14 '16 at 07:00
  • I found : `javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());` So that means I'm using `GroupLayout`? – EbraHim May 14 '16 at 07:01
  • It's correct you are using `GroupLayout`. Sizing of components is highly depends on which layout manager you are using. Each layout manager is care for a sub-set or all of properties of a component. For example some of them care for `size` while many others care about 'preferredSize'. – STaefi May 14 '16 at 07:06
  • Thank you. So, what shall I do now to have a fixed size for both selected and not selected states? – EbraHim May 14 '16 at 07:07
  • Provide the code where you are adding your components to the contentPane. Without a code nobody can tell where the problem is. For `GroupLayout` the trick is where you are adding your components using the `group.addComponent(component, ...)` method. – STaefi May 14 '16 at 07:14

1 Answers1

3

One way is to set preferredSize of your JToggleButton but it's more important that how your button is added to the underlying container using GroupLayout. GroupLayout may or may not care about the preferredSize property.

Referring to this, you can use the rules of GroupLayout as described:

GroupLayout defines constants that provide precise control over resize behavior. They can be used as parameters in the addComponent(Component comp, int min, int pref, int max) method. Here are two examples:

  1. To force a component to be resizable (allow shrinking and growing):
    group.addComponent(component, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ...

This allows the component to resize between zero size (minimum) to any size (Short.MAX_VALUE as maximum size means "infinite"). If we wanted the component not to shrink below its default minimum size, we would use GroupLayout.DEFAULT_SIZE instead of 0 in the second parameter.

  1. To make a component fixed size (suppress resizing):
    group.addComponent(component, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
      GroupLayout.PREFERRED_SIZE) ...

In these examples the initial size of the component is not altered, its default size is the component's preferred size. If we wanted a specific size for the component, we would specify it in the second parameter instead of using GroupLayout.DEFAULT_SIZE.

So in your code where you have:

.addComponent(connectionTglBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE))

you should change it according to the rule 2 to force your connectionTglBtn to be fixed size.

Hope this would be helpful.

STaefi
  • 4,297
  • 1
  • 25
  • 43