Custom jcheckbox does not appear

Asked

Viewed 140 times

2

I’d like to exhibit a JCheckBox larger than normal by default interface. After some searches I found in the link Customize Jcheckbox icons an example to be used, but it does not change the size of Jcheckbox, but replaces its default image with other images that will represent its possible states (mouse over the object, mouse outside the object, marked, unchecked, enabled, disabled, etc.). All right, because I downloaded images, edited in order to meet every situation.

I then created a class called ACheckBox extending JCheckBox to lock the setting of these images to the JCheckBox, where the class stood as illustrated below:

import javax.swing.ImageIcon;
import javax.swing.JCheckBox;

import br.com.alutal.model.Constantes;

public class ACheckBox extends JCheckBox {

    private static final long serialVersionUID = 1L;

    public ACheckBox() {
        super();
        this.setIcon(new ImageIcon(Constantes.IMG_CAMINHO_CHECKBOX));
        this.setSelectedIcon(new ImageIcon(Constantes.IMG_CAMINHO_CHECKBOX_CHECKED));
        this.setDisabledIcon(new ImageIcon(Constantes.IMG_CAMINHO_CHECKBOX_DISABLED));
        this.setDisabledSelectedIcon(new ImageIcon(Constantes.IMG_CAMINHO_CHECKBOX_DISABLED_CHECKED));
        this.setPressedIcon(new ImageIcon(Constantes.IMG_CAMINHO_CHECKBOX_PRESSED));
        this.setRolloverIcon(new ImageIcon(Constantes.IMG_CAMINHO_CHECKBOX_ROLLOVER));
        this.setRolloverSelectedIcon(new ImageIcon(Constantes.IMG_CAMINHO_CHECKBOX_ROLLOVER_SELECTED));
    }

}

As you can see the path of the images are in the constants, creating objects ImageIcon as demonstrated in the link specified above.

Finally, when I instate an object ACheckBox unfortunately nothing happens. No error, and it does not appear in the panel. I do using directly the JCheckBox there works.

What am I doing wrong? The question of the image path is correct because it is paths used in other functioning objects (such as JButton for example).

I wonder what it might be?

  • Check if the image is loading correctly. If you are using netbeans test like this: create a label, go to properties, icon, then click the button and select custom code, and then enter one of your codes: new ImageIcon(Constantes.IMG_CAMINHO_CHECKBOX) give ok and see if appears.

  • @Dener, I did the test and it is not the image path. I put it in one of the objects JButton from the menu and the image appeared normally.

  • as you add in this panel by dragging and dropping or via code?

  • @Dener, I’m putting via code. I’m developing via Eclipse without using graphic IDE to assemble form.

1 answer

0

Daniel,

I suppose you’re adding your checkbox with a jPanel.add(aCheckBox); and its panel panel features a layout livre.

Thus the layout of your jPainel don’t know where to add your component (right, left, top, bottom, middle, ...).

Note that if you drag and drop the class of your component in the panel (if you are using Netbeans) your checkbox will appear, or if you change the layout of jPanel for other layouts (Flow, Cash, Grid etc) and use the jPanel.add(aCheckBox); will appear normally.

The JPanel with free design uses the GroupLayout, if you don’t want to change the layout from your dashboard to one of the ones I mentioned above, you will have to add with the GroupLayout (Give a look here how to use it!)

  • thanks for the reply and forgiveness for the delay in answering. I am using the MigLayout as layout, using the attribution fill. As it is a form, there is no way I can not use a layout. The objects are all appearing normal, just the custom that does not appear. I did the tests using the same image paths on menu buttons and confirmed that the problem is not the image path...

  • @Danielchiurattoseabra I did a test with your code and there’s nothing wrong with your ACheckBox. You could add to the question the part of the code you are entering and the configuration of your Miglayout?

  • Sorry for the late return. I will finish this topic since there was a change in the layout of the panel, and I will no longer use image to represent the ACheckBox. Most grateful for your attention!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.