How to change size of JCheckBox Swing? [duplicate] - java

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

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 );

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

Related

Set icon size of JTabbedPane tab icon

Is there a way to easily set the size of an icon for a JTabbedPanetab.
The icon is the same size as the original image and therefore makes for a very ridiculous looking tab with it taking up half the screen.
How would I easily rescale this icon to be a similar size to my text "Settings" and just have it appear normally?
URL cogIconUrl = getClass().getResource("/images/cog.png");
ImageIcon cogImg = new ImageIcon(cogIconUrl);
JTabbedPane jtp = new JTabbedPane();
getContentPane().add(jtp);
JPanel jp1 = new JPanel();
jtp.addTab("Settings", cogImg,jp1);
Furthermore, how would you go about setting the font of the tab text? I assume the two are potentially related.
Resize the Image:
BufferedImage image = ImageIO.read(...);
Image scaled = image.getScaledInstance(...);
Icon icon = new ImageIcon( scaled );
Note: the getScaledInstance(...) method may not be the best approach as it is slow, but for a single image you won't notice any problems.
You can read Perils of Image.getScaledImage for more information.

Scene2d progress bar not resizing

I added a progress bar to my stage, and it gets a default width as expected. However, I do want to change the width a bit, but for some reason LibGDX don't. I've tried setSize, setWidth, setMinWidth at the background of the progress bar and changing the texture. Nothing works. The only thing I can get to change size of, is the knob.
Here's the code:
background = new Texture("progress_bar_texture");
knob = new Texture("knob_texture");
ProgressBar.ProgressBarStyle pbs = new ProgressBar.ProgressBarStyle();
pbs.background = new TextureRegionDrawable(new TextureRegion(background));
pbs.knob = new TextureRegionDrawable(new TextureRegion(knob));
pbs.knobAfter = pbs.knob;
progressBar = new ProgressBar(0f, 80f, 1f, false, pbs);
progressBar.setWidth(400); // This doesn't change anything at all...
table.add(progressBar).right().colspan(1).expandX().padRight(40);
Anton, you can increase the width of the ProgressBar within the table using 1. .colspan(2) or 2.Table.setWidth(). If you want precise (pixel-perfect) control of the position and size of the ProgressBar, add it to a Group instead of Table - then the things you tried ( setSize, setWidth, setMinWidth at the background of the progress bar) will work.

Alpha animation for BitmapFont

I am new to libgdx and want to know if there is an easy way to alpha animate a BitmapFont I am currently using FreeTypeFont to generate my font style and if I call:
bitmapfont.setColor(1,1,1,alphahere)
then it would render my text back to normal removing the style from my FreeTypeFont.
How can I animate the bitmap font with out removing its style from the FreeType?
You can easily change your font color by using a Label();
In your the creation of your class you'll have :
labelStyle = new LabelStyle(yourFreeTypeFont, new Color(1,1,1,1));
label = new Label("Your Text", labelStyle);
And in the render() you'll have :
labelStyle.fontColor = new Color(1,1,1,0);
label.setStyle(labelStyle);
By this method you can off course change any channels, not only the alpha
Edit : Second method
Now that I think of it, as a Label() is an actor, there is an even better way to change the alpha channel.
You can instantly change the alpha value of the label with :
label.addAction(Actions.alpha(floate alphaValue));
Or you can make a progressive change with :
label.addAction(Actions.alpha(float alphaValue, float duration));

LibGDX ImageButton image not to scale?

The red square is the button's boundary, while the image remains centered at 32x32px. I've tried using button.getImage() to set position and size to the button's values, but it doesn't seem to have any effect.
Just use a regular Button. It also contains a Drawable for its background, but that one is always stretched to fill the button. See the JavaDoc for ImageButton:
... If the image is the size of the button, a Button without any children can be used, where the Button.ButtonStyle.up, Button.ButtonStyle.down, and Button.ButtonStyle.checked nine patches define the image.
Note that these don't actually need to be nine patches; any Drawable will do.
Probably super late but have you tried:
yourButton.getImage().setFillParent(true);
Extending on the answer of #xitnesscomplex:
You can use yourButton.getImage().setFillParent(true); to set the size.
To set an offset is somewhat counter-intuitive
ImageButton.ImageButtonStyle style = new ImageButton.ImageButtonStyle();
style.imageUp = new TextureRegionDrawable(new Texture(Gdx.files.internal(btn.png")));
ImageButton yourButton = new ImageButton(style);
style.unpressedOffsetX = -new_brush.getWidth()/4.0f;
style.unpressedOffsetY = -new_brush.getHeight()/4.0f;
style.pressedOffsetX = -new_brush.getWidth()/4.0f;
style.pressedOffsetY = -new_brush.getHeight()/4.0f;
style.checkedOffsetX = -new_brush.getWidth()/4.0f;
style.checkedOffsetY = -new_brush.getHeight()/4.0f;
yourButton.getImage().setFillParent(true);

Set size for checkbox image in libgdx UI

I can't figure out how to manage checkbox images size. Of course, it is possible to create different size of image in my Texture atlas and take appropriate one, but I don't want to do that.
Here is my code:
AtlasRegion checkboxOn = AssetsHelper.textures.findRegion("checked");
AtlasRegion checkboxOff = AssetsHelper.textures.findRegion("unchecked");
CheckBoxStyle checkBoxStyle = new CheckBoxStyle();
checkBoxStyle.font = AssetsHelper.font66yellow;
checkBoxStyle.checkboxOff = checkboxOff;
checkBoxStyle.checkboxOn = checkboxOn;
CheckBox cbSound = new CheckBox(" Sound", checkBoxStyle);
cbSound object doesn't have such methods to rezise image of checkbox, but there is method getImage(), but seems it doesn't work too.
This is not working:
cbSound.getImage().width = 120;
cbSound.getImage().height = 120;
FYI: for example, when I wanted to draw image I did like that:
batch.draw(textureRegion, 0, 0, widthIwant, heightIwant);
But in CheckBox class there is overrided only this (without setting width and height):
public void draw (SpriteBatch batch, float parentAlpha) {
image.setRegion(isChecked ? style.checkboxOn : style.checkboxOff);
super.draw(batch, parentAlpha);
}
Question: how can I change width and height of checkbox image?
Thanks in advance.
The libgdx widgets are using drawables for drawing images. A drawable gets automatically scaled to fit the cell it is in. So in order to change the image size, change the cell size:
cbSound.getCells().get(0).size(widht, height);
For better results, you should use a nine patch for the drawable.
You need to set the image scaling type. Also method getImageCell is more correct than method getCells().get(0). Default is none.
CheckBox soundCB = new CheckBox("Sound", uiskin);
soundCB.getImage().setScaling(Scaling.fill);
soundCB.getImageCell().size(GameConfig.WIDTH/6);
soundCB.left().pad(PAD);
soundCB.getLabelCell().pad(PAD);
//...
content.add(soundCB).width(GameConfig.WIDTH/1.5f).row(); //add to table

Categories