1

Is there a simple way to align just the checkbox of a JCheckBox to the right without creating a separate label and check box component? I am aware of setHorizontalTextPosition(SwingConstants.LEADING); This only results in the box being on the right side of the text, but not right aligned.

What I want:

| Some sample text                                          ☑ |

instead of

| Some sample text ☑                                          |

Thanks in advance!

ricky3350
  • 1,670
  • 3
  • 19
  • 34
  • Also consider a two column `JTable` having model values of type `Boolean.class` in the last column. – trashgod Apr 02 '16 at 11:29

3 Answers3

3

You can manually manipulate the icon text gap every time the check box size changes by adding a ComponentListener to the check box:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SSCCE extends JPanel
{
    public SSCCE()
    {
        setLayout( new BorderLayout() );

        JCheckBox checkBox = new JCheckBox("Some Sample Text");
        checkBox.setHorizontalTextPosition(JCheckBox.LEADING);
        add(checkBox, BorderLayout.PAGE_START);

        checkBox.addComponentListener( new ComponentAdapter()
        {
            @Override
            public void componentResized(ComponentEvent e)
            {
                JCheckBox checkBox = (JCheckBox)e.getComponent();
                int preferredWidth = getPreferredSize().width;
                int actualWidth = getSize().width;
                int difference = actualWidth - preferredWidth;
                int gap = checkBox.getIconTextGap() + difference;
                gap = Math.max(UIManager.getInt("CheckBox.textIconGap"), gap);
                checkBox.setIconTextGap( gap );
            }
        });

    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new SSCCE());
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater( () -> createAndShowGUI() );
/*
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
*/
    }
}
camickr
  • 316,400
  • 19
  • 155
  • 279
3

You can separate the label and checkbox in a suitable layout. I've made the enclosing frame wider by an arbitrary factor to show the effect.

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/** @see http://stackoverflow.com/a/2933256/230513 */
public class Test {

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setLayout(new GridLayout(0, 1));
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(createPanel("Some label"));
                frame.add(createPanel("Another label"));
                frame.add(createPanel("Yet another label"));
                frame.pack();
                frame.setSize(frame.getWidth() * 2, frame.getHeight());
                frame.setLocationByPlatform(true);
                frame.setVisible(true);
            }

            private JPanel createPanel(String s) {
                JPanel p = new JPanel(new BorderLayout());
                p.add(new JLabel(s, JLabel.LEFT), BorderLayout.WEST);
                p.add(new JCheckBox(), BorderLayout.EAST);
                p.setBorder(BorderFactory.createLineBorder(Color.blue));
                return p;
            }
        });
    }
}
trashgod
  • 200,320
  • 28
  • 229
  • 974
0

you can also use method in AbstractButton.java setIconTextGap(int)

madina
  • 1
  • 1
    Please format your answer with more details, a link to the documentation and maybe an example of how you would perform it for the need of the question – Exomus Oct 15 '21 at 10:58