-1
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class SideNotes {

public static JPanel panel = new JPanel();
private List<String> notes = new ArrayList<String>();
private static JButton add = new JButton("Add note");

public SideNotes() {
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    panel.add(add);
    loadNotes();
    add.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            addNote();
        }
    });
}

public void addNote() {
    String note = JOptionPane.showInputDialog("Enter note: ", null);
    notes.add(note);
    JLabel label = new JLabel(note);
    panel.add(label);
    panel.revalidate();
    panel.repaint();
}

private void loadNotes() {
    for (int i = 0; i < notes.size(); i++) {
        JCheckBox jcb = new JCheckBox(notes.get(i), false);
        panel.add(jcb);
        panel.revalidate();
        panel.repaint();
    }
}

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(200, 400);
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    panel.add(add);
    frame.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    new SideNotes();
}

}

Why isn't my JCheckBox showing up? The text shows up but not the actual box. What's the deal?

I have edited my post to contain all of my code in case that helps solve the issue.

needmoretextneedmoretextneedmoretextneedmoretextneedmoretextneedmoretextneedmoretext

Nicolas
  • 11
  • 2
  • 8

2 Answers2

3

Possible reasons:

  • panel has not been added to GUI
  • panel has been added but for some reason is not visible.
  • panel is too small to show the child component. This can happen for instance if you set a component's size or preferredSize or if you place it in a FlowLayout-using container without thought.
  • panel uses null layout.
  • panel's layout manager is not one that easily accepts a new component -- think GroupLayout for this one.
  • There are other unspecified layout manager problems going on. Do you call pack() on your GUI? Do you use null layout or absolute positioning anywhere? Do you need to put panel in a JScrollPane?

Consider creating and posting an sscce for better help.


Edit

  • Your posted code doesn't ever add any JCheckBoxes to the JPanel, just JLabels. To prove this is so, click on the labels and you'll see that they don't respond to clicks.
  • Your code grossly over-uses static fields. Get rid of all static modifiers on all variables. They should all be instance variables. The only static anything in your code above should be the main method, and that's it. If this causes errors, then fix the errors, but not by making fields static.
  • Give your SideNotes class a method, getPanel() that returns the panel field.
  • Create a SideNotes instance in the beginning of your main method. Then call the above method on the instance to get the JPanel for the JFrame. i.e., frame.add(sideNotes.getPanel());.
  • Don't add JLabels to your GUI (I've no idea why you're doing this). Add JCheckBoxes in the actionPerformed method.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
1

Every time you press the button, a new Note (JLabel) is added to the panel. But you never call loadNotes() after adding a new Note. So the JLabel is added but not its respective JCheckBox as intended.

Besides of this I'd suggest you make this change:

public void addNote() {
    String note = JOptionPane.showInputDialog("Enter note: ", null);
    if(notes != null) {
        notes.add(note);
        JLabel label = new JLabel(note);
        panel.add(label);
        panel.add(new JCheckBox(note, false));
        panel.revalidate();
        panel.repaint();
    }
}

So you don't need to call loadNotes() and update the GUI just once.

dic19
  • 17,821
  • 6
  • 40
  • 69