javaswinglook-and-feelgrouplayoutjtogglebutton

How to make JToggleButton-size fixed upon selection and deselection?


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.


Solution

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