Make JCheckbox bigger..?
Asked Answered
P

2

5

i want to make my JCheckboxes in a JTable bigger (for Touchscreen), but it doesn't change the size.

I tried it with

  • setPrefferedSize
  • setSize

What should I do?..

Pygmy answered 22/1, 2011 at 19:23 Comment(0)
L
4

I assume you mean you want a bigger check box. If so then you need to create images to represent the unselected and selected icons of the check box. Then you can create a renderer and editor using these icons. Finally you would need to increase the height of each row in the table. The code might look something like:

Icon normal = new ImageIcon(...);
Icon selected = new ImageIcon(...);
JTable table = new JTable(...);
table.setRowHeight(...);

TableCellRenderer renderer = table.getDefaultRenderer(Boolean.class);
JCheckBox checkBoxRenderer = (JCheckBox)renderer;
checkBoxRenderer.setIcon( normal );
checkBoxRenderer.setSelectedIcon( selected );

DefaultCellEditor editor = (DefaultCellEditor)table.getDefaultEditor(Boolean.class);
JCheckBox checkBoxEditor = (JCheckBox)editor.getComponent();
checkBoxEditor.setIcon( normal );
checkBoxEditor.setSelectedIcon( selected );
Lodgment answered 22/1, 2011 at 20:3 Comment(5)
I have my own Renderer for the Boolean.class column.. but when I set the icons it will not be displayed and when I click on it , it displays for a short second the normal checkbox..Coax
Yes, when you click on it, the "editor" is displayed that is why you need both a custom renderer and editor. If the icons are not displayed then your custom code doesn't work. Try using the default renderer and editor first to understand the concept. Then if you still feel you need a custom renderer and editor you can change both.Lodgment
Why? The code doesn't change the renderer, only the icons used by the renderer.Lodgment
That was a lot of code for something I feel should be possible through "zooming" capabilities in the language or OSJensen
Thank you camickr, the solution that you posted is very cool. I tried a whole bunch of other things and failed, yours was the only solution that worked properly.Prophet
F
4

IMPORTANT NOTE: This was only tested with the default 'Metal' look and feel. I do not guarantee that this will work for any other look and feel. Also I am not entirely sure how it works because it is admittedly a bit of a hack.

I was able to solve this in a slightly different way.

I wanted to use the existing images and just apply a scale to it. I am already scaling the font of my application using the UI defaults and so I have a rather large font. I wondered if I could leverage that and scale the check boxes accordingly.

After scouring the internet and trying a bunch of things I came up with this method:

public static void scaleCheckBoxIcon(JCheckBox checkbox){
    boolean previousState = checkbox.isSelected();
    checkbox.setSelected(false);
    FontMetrics boxFontMetrics =  checkbox.getFontMetrics(checkbox.getFont());
    Icon boxIcon = UIManager.getIcon("CheckBox.icon");
    BufferedImage boxImage = new BufferedImage(
        boxIcon.getIconWidth(), boxIcon.getIconHeight(), BufferedImage.TYPE_INT_ARGB
    );
    Graphics graphics = boxImage.createGraphics();
    try{
        boxIcon.paintIcon(checkbox, graphics, 0, 0);
    }finally{
        graphics.dispose();
    }
    ImageIcon newBoxImage = new ImageIcon(boxImage);
    Image finalBoxImage = newBoxImage.getImage().getScaledInstance(
        boxFontMetrics.getHeight(), boxFontMetrics.getHeight(), Image.SCALE_SMOOTH
    );
    checkbox.setIcon(new ImageIcon(finalBoxImage));

    checkbox.setSelected(true);
    Icon checkedBoxIcon = UIManager.getIcon("CheckBox.icon");
    BufferedImage checkedBoxImage = new BufferedImage(
        checkedBoxIcon.getIconWidth(),  checkedBoxIcon.getIconHeight(), BufferedImage.TYPE_INT_ARGB
    );
    Graphics checkedGraphics = checkedBoxImage.createGraphics();
    try{
        checkedBoxIcon.paintIcon(checkbox, checkedGraphics, 0, 0);
    }finally{
        checkedGraphics.dispose();
    }
    ImageIcon newCheckedBoxImage = new ImageIcon(checkedBoxImage);
    Image finalCheckedBoxImage = newCheckedBoxImage.getImage().getScaledInstance(
        boxFontMetrics.getHeight(), boxFontMetrics.getHeight(), Image.SCALE_SMOOTH
    );
    checkbox.setSelectedIcon(new ImageIcon(finalCheckedBoxImage));
    checkbox.setSelected(false);
    checkbox.setSelected(previousState);
}

What it does is get the size of the font from the checkbox's font metrics. Then using that it derives a new icon based on the icon found in the 'Look and Feel'.

One odd thing that I am not able to explain is how the icon for the checkbox in its 'un-selected' or default state, changes to the 'selected' icon, when I am accessing the same property to get each one.

I start by saving the state of the control so I can restore it at the end. This is done because in order for the icons to be set properly, the state needs to be unchecked when you first request the icon from the UIManager and then it will need to be checked when you request the icon the second time to get the 'selected' icon.

I am not entirely sure how the UIManager works or why the checkbox icon changes when we call the same property just by setting the 'selected' value of a single checkbox, but that is what is required in order to get both the necessary icons.

If you did not want to base the size on the font you could easily just pass in the height and width as parameters and use them instead of the font's height when setting the buffered image size.

I might mention that this same methodology works with radiobuttons

Fortunna answered 18/11, 2014 at 13:3 Comment(1)
Great work, thank you! I'm using the Windows look and feel and it works for me so far. The only downside is that you don't get the "mouseover" effects that you get with normal Windows 7 checkboxes. Normally, when you point to a checkbox, it glows blue. But you lose that effect when this scaling method is used.Kuo

© 2022 - 2024 — McMap. All rights reserved.