简体   繁体   English

JList / ListModel getElements不起作用

[英]JList/ListModel getElements not working

I am having problems with grabbing all the elements of a ListModel, when i try to write the strings to file they output something like the following: 我在抓取ListModel的所有元素时遇到问题,当我尝试将字符串写入文件时,它们输出如下内容:

[Ljava.lang.String;@79b43f[Ljava.lang.String;@79b43f

So what am i doing wrong? 那我做错了什么? Here is the code that is causing the grief: 这是造成悲伤的代码:

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 is a bufferedWriter and listModel is a custom list model that I have made.. You may notice I have split the string,, this is because i want to extract two different values from each list row, I have simply done setText(text + "-" + text) in my customCellRenderer implementation in order to enable me to use the "-" char as a delim to extract the two values bw是一个bufferedWriter,listModel是我自己制作的自定义列表模型..你可能会注意到我已经拆分了字符串,这是因为我想从每个列表行中提取两个不同的值,我只是简单地完成了setText(text +我的customCellRenderer实现中的“ - ”+ text),以便我能够使用“ - ”char作为delim来提取这两个值

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;
    }
}

It seems in your custom list model, listModel.getElementAt(i) returns an array. 在您的自定义列表模型中,listModel.getElementAt(i)返回一个数组。 Please check it and post your custom list model here if you need further assistance. 如果您需要进一步的帮助,请检查并在此处发布自定义列表模型。

What you've posted isn't the ListModel - it's the ListCellRenderer . 你发布的不是ListModel - 它是ListCellRenderer But this does reinforce the idea that each item in your JList is a String[] - eg you are casting each value in the JList to a String[] for rendering. 但这确实强化了JList中的每个项都是String[]的想法 - 例如,您将JList中的每个值转换为String[]进行渲染。

String leftData = ((String[])value)[0];
String rightData = ((String[])value)[1];

It appears that there is some confusion as to where the the ListModel is being populated. 似乎对ListModel的填充位置存在一些疑惑。 Your post/code suggests that you think it is being populated in the ListCellRenderer - this is not the case. 您的帖子/代码表明您认为它正在ListCellRenderer中填充 - 事实并非如此。 It is only working out how to display the values already in the ListModel . 它只是计算如何显示ListModel已有的值。

So, a quick fix could be in the 'code that's giving you grief': 因此,快速解决方案可能出现在“给你悲伤的代码”中:

String[] temp = (String[]) listModel.getElementAt(i);
bw.write(temp[0]);
bw.write(temp[1]);

However, I wouldn't really encourage this, as you'll have unchecked class casts. 但是,我不会真的鼓励这一点,因为你将取消选中的类型。

Instead, I would suggest you consider why you are populating the ListModel with String[] in the first place? 相反,我建议你考虑为什么你首先用String[]填充ListModel eg are you constructing the JList as follows: 例如,您是按如下方式构建JList

JList myList = new JList(new String[]{"A","B"}, new String[]{"C","D"});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM