0
我在JOptionPane
中使用JList
來顯示對話框中的線條。 我只是想改變線條的背景顏色和字體(取決於線條的內容)。更改JList中線條的字體和背景顏色
我無法實現,也沒有找到任何有用的文章。 我的實際問題是,我的下面的代碼中的方法getListCellRendererComponent
永遠不會被調用。對話框出現一行「任何一行文字」,但沒有顏色/字體變化。
任何人都可以幫忙嗎?
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);
哦,我的好。我真是太蠢了!你是對的。非常感謝您對此進行了詳細的調查。我現在很開心:-) –
適合每個人。 –