我有抓住一個ListModel的所有元素,當我試圖寫字符串到文件它們輸出類似如下的問題:的JList/ListModel的的getElements不工作
[Ljava.lang.String;@79b43f[Ljava.lang.String;@79b43f
所以我在做什麼錯誤?這是造成悲痛代碼:
for (int i = 0; i < listModel.getSize(); i++)
{
String[] temp = listModel.getElementAt(i).toString().split("-");
bw.write(temp[0]);
bw.write(temp[1]);
System.out.println(temp[0]);
}
體重是一個的BufferedWriter和ListModel的是,我做了一個自定義列表的模型。你可能會注意到我已經分割字符串,,這是因爲我想從每個列表行中提取兩個不同的值,我在我的customCellRenderer實現中簡單地完成了setText(text +「 - 」+ text),以使我能夠使用「 - 」char作爲分隔符來提取這兩個值
public class CustomCellRenderer extends JLabel
implements ListCellRenderer
{
JLabel left, right;
}
public CustomCellRenderer()
{
setOpaque(true);
}
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
String leftData = ((String[])value)[0];
String rightData = ((String[])value)[1];
// simply will not function how i want it to.. GRR
leftData = String.format("%-50s", leftData);
setText(leftData + "- " + rightData);
if (isSelected)
{
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
}
else
{
setBackground(list.getBackground());
setForeground(list.getForeground());
setBackground(list.getBackground());
setForeground(list.getForeground());
}
return this;
}
}
爲了更好地幫助越早,張貼[SSCCE(http://sscce.org/)。 對數據進行硬編碼。 –
看起來你正在編寫幾個'String []''的'toString()' - listModel中的元素是什麼? – amaidment
@amaidment ive添加上面列表模型代碼 –