Reputation: 11651
I use a JList
in a JOptionPane
to show lines in a dialog.
I simply want to change the background color and font of the lines (depending on the content of the line).
I can't achieve and did not find any useful article on this.
My actual problem is, that the method getListCellRendererComponent
in my following code is never called. Dialog shows up with one line "any text for one line", but no color/font changes.
can anybody help ?
final DefaultListModel d = new DefaultListModel();
final JList list = new JList(d);
ListCellRenderer renderer = new ListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
JLabel label = new JLabel();
label.setText(value.toString());
label.setFont(new Font("Courier New", Font.ITALIC, 12));
label.setBackground(new Color(12, 12, 12));
int i = 1 / 0; // <<<<<< --- does not throw an error, so it doesn't get into this.
return label;
}
};
list.setCellRenderer(renderer);
for (int iList = 0; iList < alSuggestionsText.size(); iList++) {
// bigList[iList] = alTexte.get(iList);
d.addElement(alSuggestionsText.get(iList));
// jlist.add(bigList);
}
final String sIgronreText = "any text for one line";
d.addElement(sIgronreText);
final JList jlist = new JList(d);
JOptionPane jpane = new JOptionPane();
jpane.showMessageDialog(null, jlist, sWikiidtemp, JOptionPane.PLAIN_MESSAGE);
Upvotes: 0
Views: 1158
Reputation: 1595
You have two different JLists. First one where you set ListCellRenderrer.
list.setCellRenderer(renderer);
and another one you display in dialog:
pane.showMessageDialog(null, jlist, "adsfasdf", JOptionPane.PLAIN_MESSAGE);
Add:
final JList jlist = new JList(d);
jlist.setCellRenderer(renderer);
to get it working.
Upvotes: 3