Question

I am doing a project for my class and I have the following situation: It's a JDialog that receives an Object on the constructor to work with it. The Object 'Album' has two attribute that can be 0 or 1 according to a MySQL boolean. When the window gets initialized, it checks the status of that two attributes. If one of them is set to 1, the corresponding jCheckBox gets checked. Otherwise (case 0) it remains unchecked, and the same to the second attribute.

What I want to get is to make the two jCheckBoxes uneditable without disabling them, that is, it remains looking as 'enabled' but if you click it won't happen anything, because I don't want them to look grey as disabled.

I tried few things such as creating a Thread that keeps checking the checkboxes state, and if it's not the set on the Album attribute, it changes it to the correct state, but it doesn't work, you can check or uncheck both checkboxes as desired.

What could I do?

Here's what I have until right now.

public class JDVerAlbum extends javax.swing.JDialog {

    /**
     * Creates new form JDVerAlbum
     */
    Album a;
    /**
     * 
     * @deprecated 
     */
    public JDVerAlbum(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public JDVerAlbum(java.awt.Frame parent, boolean modal, Album a) {
        super(parent, modal);
        this.a=a;
        initComponents();
        inicializar();
    }

    private void inicializar(){
        Statement stm = Controlador.connect();
        String s = null;
        try {
            ResultSet rs = stm.executeQuery("SELECT grupo.nombre from album, grupo where album.nombre='"+a.getName()+"' and album.idgrupo=grupo.id;");
            rs.next();
            s = rs.getString(1);
        } catch (SQLException ex) {
            Logger.getLogger(JDVerAlbum.class.getName()).log(Level.SEVERE, null, ex);
        }

        jLabelAlbum.setText(a.getName()+" - "+s);
        this.setTitle("Quaver Records - Ver álbum - "+jLabelAlbum.getText());
        jTextFieldAño.setText(""+a.getReleaseYear());
        jTextFieldTipo.setText(""+a.getType());
        if(a.getPhysicalSales()==1){
            jCheckBoxFisica.setSelected(true);
        }
        if(a.getDigitalSales()==1){
            jCheckBoxDigital.setSelected(true);
        }
    }
Was it helpful?

Solution

You could remove the MouseListener that JCheckBox has that gives it this behavior:

 JCheckBox checkBox = new JCheckBox("Check Box");
 EventListener[] listeners = checkBox.getListeners(MouseListener.class);
 for (EventListener eventListener : listeners) {
     checkBox.removeMouseListener((MouseListener) eventListener);
 }

Also consider setting the JCheckBox's focusable property to false:

checkBox.setFocusable(false);

This prevents the user from tabbing to the check box and then toggling its state with the space bar.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top