Search code examples
javajcheckbox

Can't add JCheckBox to Pane in Java


I cannot get the JCheckBoxes to work. I use a normal Pane, no layout. Buttons, Labels, TextFields and comboboxes all seem to be working just fine, except for the JCheckBoxes. My code looks somewhat like this:

Pane root = new Pane();

Button btn = new Button();
JCheckBox voer = new JCheckBox("Voerbakjes");

btn.setLayoutX(50);
btn.setLayoutY(600);
voer.setLayoutX(300);
voer.setLayoutY(300);

root.getChildren().add(voer);
root.getChildren().add(btn);

The button is working, and also it's setLayout functions. These also don't seem to work on the JCheckBox. Is there some way I can fix this?


Solution

  • EDIT: You're creating a JCheckBox. All "J" prefixed components imports the swing components, as your button, label and panel are AWT UI.

    For this to works use all J components.

    JPanel root = new JPanel();
    
    JButton btn = new JButton();
    JCheckBox voer = new JCheckBox("Voerbakjes");
    
    btn.setLayoutX(50);
    btn.setLayoutY(600);
    voer.setLayoutX(300);
    voer.setLayoutY(300);
    
    root.getChildren().add(voer);
    root.getChildren().add(btn);