[英]Add JCheckBox to JScrollPane
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. 感谢您的帮助。
there is an logical issue to call panel.removeAll();
调用
panel.removeAll();
存在一个逻辑问题panel.removeAll();
instead of scrollPane.removeAll();
而不是
scrollPane.removeAll();
JComponent
are added to JViewport
not to the JScrollPane
JComponent
被添加到JViewport
而不是JScrollPane
then to call (after all Items are added to JPanel
) panel.revalidate()
and panel.repaint()
as last code lines in the void 然后调用(在所有项目添加到
JPanel
) panel.revalidate()
和panel.repaint()
作为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 你在Swing中遇到Concurency问题,所有对已经可见的
Swing GUI
更新都必须在Event Dispatch Thread
上完成,但在其他方面, panel.repaint()
通知Event Dispatch Thread
并且正确
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 view JList
(non_editable)或JTable
(可编辑)一列(可能是删除JTableHeader
)而不是带有JCheckBoxes
的JPanel
和un_natural滚动,请注意模型中存储了Boolean
值,表示视图中的JCheckBox
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.