简体   繁体   English

将JList复制到arrayList时出错

[英]Error copying a JList into an arrayList

I am trying to copy a JList into an array list. 我正在尝试将JList复制到数组列表中。

    ArrayList<String> aList= new ArrayList<String>();
    size= list.getModel().getSize(); //list is a JList defined elsewhere
    for(int i=0;i< size ;i++){
        aList.add(list.toString());
    }

But this does not seem to copy the content, instead it is copying the attributes of the JList. 但这似乎并不复制内容,而是复制JList的属性。 Output :javax.swing.JList[,0,0,414x390,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=50331944,maximumSize=,minimumSize=,preferredSize=,fixedCellHeight=-1,fixedCellWidth=-1,horizontalScrollIncrement=-1,selectionBackground=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],selectionForeground=sun.swing.PrintColorUIResource[r=51,g=51,b=51],visibleRowCount=8,layoutOrientation=0] 输出:javax.swing.JList[,0,0,414x390,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=50331944,maximumSize=,minimumSize=,preferredSize=,fixedCellHeight=-1,fixedCellWidth=-1,horizontalScrollIncrement=-1,selectionBackground=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],selectionForeground=sun.swing.PrintColorUIResource[r=51,g=51,b=51],visibleRowCount=8,layoutOrientation=0]

How to read the content instead ? 如何阅读内容呢? Is there a more simplified way of doing the same (like toArray() ) ? 是否有更简化的方法(例如toArray())?

Thanks 谢谢

You are copying the "toString" of a JList, which is just a "human readable" representation of the JList. 您正在复制JList的“ toString”,它只是JList的“人类可读”表示形式。 You have to copy the content, for example by iterating in the model of the JList : 您必须复制内容,例如,通过在JList模型中进行迭代:

ListModel model : list.getModel();
for (int i=0; i < model.getSize(); i++) {
  aList.add(model.getElementAt(i));
}

Calling JList#toString() cannot in any way work as you seem to think it might work. 似乎您认为调用JList#toString()不能以任何方式起作用。 Please print one call of it to see what it returns. 请打印一次以查看返回的内容。 Instead, you'll want to get the JList's model and then get each element that it holds via the getElement(...) method. 相反,您将需要获取JList的模型,然后通过getElement(...)方法获取其持有的每个元素。

aList.add(list.getModel().getElement(i));

Like mention before for (Hovercraft and Thierry) doesn t work, even if used .toString, or get null output. 就像之前提到的for(气垫船和蒂埃里)一样,即使使用.toString也不起作用,或者得到null输出。 Also do not forget JList come javax. 也不要忘记JList来javax。 However i suggest use this if is not any event: DefaultListModel for JList, i hope help you. 但是,如果没有任何事件,我建议使用此方法:JList的DefaultListModel,希望对您有所帮助。

import java.util.ArrayList;
import javax.swing.DefaultListModel;

public class Jlistinjarry {

    public static void main(String[] args) {
        // TODO Auto-generated method stub


        ArrayList xaList = new ArrayList();
        xaList.add("a");
        xaList.add("b");
        xaList.add("c");
        DefaultListModel model = new DefaultListModel();
        for(String s:xaList){
            model.addElement(s);
        }



         System.out.println( model);


       }
}

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

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