Reputation: 244
I'm trying to dynamically add check boxes to a scroll pane depending on what is in my db. Currently i have
ResultSet rs = getAvailableUsers();
try {
while (rs.next()){
User temp = new User();
temp.setUsername(rs.getString("username"));
temp.setUserNo(rs.getInt("userno"));
JCheckBox tempCheckBox = new JCheckBox();
tempCheckBox.setText(temp.getUsername());
tempCheckBox.setVisible(true);
checkBoxes.add(tempCheckBox);
}
} catch (SQLException ex) {
Logger.getLogger(SelectProjectTeamGUI.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(checkBoxes);
panel.setLayout(new GridLayout(0,1));
panel.setVisible(true);
scrollPane.removeAll();
scrollPane = new JScrollPane(panel);
for (int i = 0; i < checkBoxes.size(); i++){
panel.add(checkBoxes.get(i));
//panel.repaint();
System.out.println(i);
}
scrollPane.setVisible(true);
I can see the outline for the scroll pane but none of the checkboxes are there. All the checkboxes are stored in a vector then pulled back out and added to the panel one at a time. Any ideas why it isn't showing anything? I know the checkboxes are in the vector as i've printed it out and seen them all there.
Thanks for your help in advance.
Upvotes: 1
Views: 1280
Reputation: 109815
there is an logical issue to call panel.removeAll();
instead of scrollPane.removeAll();
JComponent
are added to JViewport
not to the JScrollPane
then to call (after all Items are added to JPanel
) panel.revalidate()
and panel.repaint()
as last code lines in the void
you have an issue with Concurency in Swing, all updates to already visible Swing GUI
must be done on Event Dispatch Thread
, but in other hands panel.repaint()
notified Event Dispatch Thread
and correctly
JList
(non_editable) or JTable
(can be editable) with one column (maybe to remove JTableHeader
) instead of JPanel
with bunch of JCheckBoxes
and with un_natural scrolling, note there is stored Boolean
value in the model, that representing JCheckBox
in the viewUpvotes: 4