ListModelのすべての要素を取得する際に問題が発生しました。文字列をファイルに書き込もうとすると、次のような出力が返されます。
[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]);
}
bwはbufferedWriterであり、listModelは私が作成したカスタムリストモデルです。文字列を分割したことに気付くかもしれません。これは、各リスト行から2つの異なる値を抽出したいため、setText(text + 2つの値を抽出するためのデリムとして「-」文字を使用できるようにするために、customCellRenderer実装で「-」+テキスト)
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;
}
}