Reputation: 28907
I have a JList in my Java Swing application and when a user clicks a button, the list clears and the contents reset as follows:
public void reset(ArrayList<String> content) {
listModel.removeAllElements();
System.out.println(content.size());
for(int i = 0; i < content.size(); i++) {
listModel.addElement(content.get(i));
System.out.println("Added element " + content.get(i));
}
}
The list is initialized as follows
listModel = new DefaultListModel();
list = new JList(listModel);
However there is a problem. The list clears (before the reset, there was other content. This content does go away), but the new content does not show up. And, from the output, I can see that 6 elements have been added. But they do not show up in the list. Why is this?
Upvotes: 2
Views: 6441
Reputation: 109813
However there is a problem. The list clears (before the reset, there was other content. This content does go away), but the new content does not show up. And, from the output, I can see that 6 elements have been added. But they do not show up in the list. Why is this?
nobody knows, only Concurency in Swing could be issue, be sure that all events would be dode on EDT, otherwise for better help sooner post an SSCCE, for example
EDIT
added String instances requested by @Robin
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ListString extends JFrame {
private static final long serialVersionUID = 1L;
private DefaultListModel model = new DefaultListModel();
private int i = 01;
public ListString() {
model.addElement(("one" + i++));
model.addElement(("two" + i++));
model.addElement(("three" + i++));
model.addElement(("four" + i++));
JList list = new JList(model);
add(new JScrollPane(list));
JButton btn = new JButton("Remove All Rows :");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
model.removeAllElements();
}
});
add(btn, BorderLayout.SOUTH);
JButton btn1 = new JButton("Add New Rows:");
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
model.addElement(("one" + i++));
model.addElement(("two" + i++));
model.addElement(("three" + i++));
model.addElement(("four" + i++));
}
});
add(btn1, BorderLayout.NORTH);
}
public static void main(String[] args) {
UIManager.getLookAndFeelDefaults().put("List.selectionBackground", Color.red);
ListString frame = new ListString();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
with rendering JPanel inside JScrollPane requested by @trashgod
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ListPanel extends JFrame {
private static final long serialVersionUID = 1L;
private DefaultListModel model = new DefaultListModel();
private int i = 01;
public ListPanel() {
model.addElement(createPanel("one" + i++));
model.addElement(createPanel("two" + i++));
model.addElement(createPanel("three" + i++));
model.addElement(createPanel("four" + i++));
JList list = new JList(model);
list.setCellRenderer(new PanelRenderer());
add(new JScrollPane(list));
JButton btn = new JButton("Remove All Rows :");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
model.removeAllElements();
}
});
add(btn, BorderLayout.SOUTH);
JButton btn1 = new JButton("Add New Rows:");
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
model.addElement(createPanel("one" + i++));
model.addElement(createPanel("two" + i++));
model.addElement(createPanel("three" + i++));
model.addElement(createPanel("four" + i++));
}
});
add(btn1, BorderLayout.NORTH);
}
public static JPanel createPanel(String text) {
JPanel panel = new JPanel();
panel.add(new JLabel("Item: "));
panel.add(new JLabel(text));
return panel;
}
public static void main(String[] args) {
UIManager.getLookAndFeelDefaults().put("List.selectionBackground", Color.red);
ListPanel frame = new ListPanel();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
private class PanelRenderer implements ListCellRenderer {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
JPanel renderer = (JPanel) value;
renderer.setBackground(isSelected ? list.getSelectionBackground() : list.getBackground());
return renderer;
}
}
}
Upvotes: 2