0

I need to reduce the size of a JCheckBox item, which has no text. So far I tried to override the methods getPreferredSize() and getMaximumSize(). Also setting the Font size or the size of the JCheckBox itself doesn't evoke any changes. Is there a way to achieve this?

My-Name-Is
  • 4,814
  • 10
  • 44
  • 84

3 Answers3

2

If you're talking about an Icon that is added to the JCheckBox, then best would be to create a new Icon from a new Image that is a resize of the original image. You can do this with a Image by calling the yourImage.getScaledInstance(...); method. Once you get the new image, create a new ImageIcon(newImage) and use it with your JCheckBox.

e.g.

Image oldImage = oldIcon.getImage();
Image newImage = oldImage.getScaledInstance(newWidth, newHeight, 
      Image.SCALE_DEFAULT);
Icon newIcon = new ImageIcon(newImage);
checkBox.setIcon(newIcon);
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

getPreferredSize and getMaximumSize only return the values assigned to preferredSize and maximumSize, respectively, for the JCheckBox object. If you want to change these values, use setPreferredSize and setMaximumSize instead:

jCheckBox.setPreferredSize(new Dimension(100, 300));

and/or:

jCheckBox.setMaximumSize(new Dimension(200, 600));
Bucket
  • 7,415
  • 9
  • 35
  • 45
0

You can override the paint function like this:

final JCheckBox cb = new JCheckBox("",autoChangeTab) {
    @Override
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.scale(.9, .9);
        super.paint(g);
    }
};
fabdoc
  • 1