如何在修改内容后正确更新JList
?
我正在构建一个简单的Java Messageboard
,如下所示:
JList
JPanels
作为列表条目,JScrollPane
显示在public Jp_Diskussion(Jp_EinProjekt thismyProject) {
private void initiate() {
setLayout(null);
add(getComboBoxMa());
add(getComboBoxTag());
add(getLabelFiltern());
add(getLabelOpt());
add(getBtnBeitragVerfassen());
initializeMessageBoard();
// Add ItemListeners for selection of combobox entries (needed for filtering later)
comboBoxMa.addItemListener(new ItemChangeListener());
comboBoxTag.addItemListener(new ItemChangeListener());
}
public JScrollPane getScrollPane(MessageList discussionBoard) {
messageScroll = new JScrollPane(discussionBoard);
messageScroll.setBounds(20, 20, 954, 461);
messageScroll.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER);
return messageScroll;
}
public void initializeMessageBoard() {
// Create a custom JList (see class MessageList) called discussionBoard
MessageList discussionBoard = new MessageList();
// Get Project ID (atm still static)
int id = 4;
int pid = 4;
// Get all messages and add them to ArrayList
ArrayList <ConBeitrag> allBtr = ctBtr.getBeitraegeByProjektID(id);
// Add all messages to the JList discussionBoard
for (int i = 0; i < allBtr.size(); i++) {
ConBeitrag msg = allBtr.get(i);
discussionBoard.addMessage(msg);
}
// Add the ScrollPane messageScroll (which is created by getMessageScroll) with the JList as parameter to the JPanel
this.add(getScrollPane(discussionBoard));
// Load contents of comboboxes for the filter
// Load all Employees
Object[] mas = ctBtr.getAllMaByProjektId(pid).toArray();
for (Object ma : mas) {
this.setCbMa(ma);
}
// Load all tags
Object[] tags = ctBtr.getAllTagsByProjectId(id).toArray();
for (Object tag : tags) {
this.setCbTag(tag);
}
}
// ItemListener for when a selection in the comboboxes is made
public class ItemChangeListener implements ItemListener {
public void itemStateChanged(ItemEvent event) {
if (event.getStateChange() == ItemEvent.SELECTED) {
filterMessages();
}
}
}
public void filterMessages() {
// Get Project ID (atm still static)
int id = 4;
// Get selections from the comboboxes
String ma = this.getContentCbMa().toString();
String tag = this.getContentCbTag().toString();
// Create new discussionBoard
MessageList discussionBoard = new MessageList();
// Get all messages again and put them in ArrayList btr
ArrayList <ConBeitrag> btr = ctBtr.getBeitraegeByProjektID(id);
// Filtering: kick all messages that don't fit filter parameters out of ArrayList btr
if (ma != "Alle Mitarbeiter") {
for (int i = 0; i < btr.size();) {
ConBeitrag msg = btr.get(i);
String msgMA = msg.getUsername();
if (!msgMA.equals(ma)) {
btr.remove(i);
} else {
i++;
}
}
}
if (tag != "Alle Schlagwörter") {
for (int i = 0; i < btr.size();) {
ConBeitrag msg = btr.get(i);
ArrayList<String> msgTags = msg.getTag();
if (!msgTags.contains(tag)) {
btr.remove(i);
} else {
i++;
}
}
}
// Add remaining messages to discussionBoard
for (int i = 0; i < btr.size(); i++) {
ConBeitrag msg = btr.get(i);
discussionBoard.addMessage(msg);
}
// Add the ScrollPane with the JList again
this.add(getScrollPane(discussionBoard));
revalidate();
}
内。在后台它连接到postgreSQL数据库。还有一个过滤功能,可以过滤作者和标签的消息。
以下是一些代码:
{{1}}
最初加载面板时,它会正确显示消息。但是,如果我过滤(在其中一个组合框中选择一个项目),列表将变为空白。如果我单击空白列表,它会再次显示所有消息(未过滤)。我究竟做错了什么?我在哪里重新绘制/重新验证?
答案 0 :(得分:3)
不要使用null布局和setBounds()。 Swing旨在与布局管理器一起使用。
在您使用的过滤器代码中:
this.add(getScrollPane(discussionBoard));
每次进行更改时都不要创建新组件。您需要做的就是更新ListModel,JList将使用模型自动重绘自己。
所以代码就像:
DefaultListModel model = new DefaultListModel(...);
// add items to the model
discussionBoard.setModel( model );