简体   繁体   English

从 JList 检索自定义对象

[英]Retrieve custom object from JList

I created a list and a model with custom objects.我创建了一个列表和一个带有自定义对象的模型。 I want to retreive the objects that are in the list, however, I get an error message, that String can not be converted to Object.我想检索列表中的对象,但是,我收到一条错误消息,该字符串无法转换为对象。

String cannot be converted to ARMAJTermek

Swing automatically creates the JList, to which then I add a new model. Swing 会自动创建 JList,然后我向其中添加一个新模型。

DefaultListModel model = new DefaultListModel<ARMAJTermek>();
TermekList.setModel(model);

I need this, because I'm using a custom object, that has properties and methods, which I need to invoke later.我需要这个,因为我使用的是一个自定义对象,它具有我稍后需要调用的属性和方法。 For this, I overrode the .toString method, like this:为此,我覆盖了 .toString 方法,如下所示:

@Override
    public String toString () {
        String toString = "";
        toString = this.nev+"\n"+this.cikkszam+"\n"+Integer.toString(this.bruttoKisker)+" Ft";
        return toString;
    }

This way, when I add an object to the JList, it appears as designed by the cell renderer (which is also unique, in a way that it displays JTextArea as a list element...).这样,当我将一个对象添加到 JList 时,它会按照单元格渲染器的设计出现(这也是独一无二的,它将 JTextArea 显示为列表元素......)。

for (int i... and lots of other code) {
    model.addElement(termek[i]);
}
TermekList.setModel(model);
TermekList.revalidate();
TermekList.repaint();

After adding objects to the list, it appears correctly, runs correctly, as when I select an object in the lists, it's methods run correctly.将对象添加到列表后,它显示正确,运行正确,因为当我在列表中选择一个对象时,它的方法运行正确。 However, when I try to make a copy of the selected object for a different JList, I'm unable to retrieve them.但是,当我尝试为不同的 JList 制作所选对象的副本时,我无法检索它们。 I tried getting it from the list itself with我尝试从列表中获取它

ARMAJTermek termek = (ARMAJTermek)TermekList.getSelectedValue();
ARMAJTermek termek = (ARMAJTermek)TermekList.getModel().getElementAt(TermekList.getSelectedIndex());

(Different functions, that's why some variables have the same name even being different types.) (不同的函数,这就是为什么有些变量即使是不同的类型也有相同的名称。)

But I get the message, that these Strings can not be converted to ARMAJTermek.但是我得到消息,这些字符串不能转换为 ARMAJTermek。 But I don't need the String representation of the objects, I need the objects themselves.但是我不需要对象的字符串表示,我需要对象本身。 I mean, if it's methods can be run by clicking on their String representation in the list, I should be able to make a copy of them, right?我的意思是,如果它的方法可以通过单击列表中的字符串表示来运行,我应该能够复制它们,对吗?

public class ARMAJTermek {
    String nev;
    String cikkszam;
    int bruttoKisker;

    public ARMAJTermek(String nev, String cikkszam, int bruttoKisker) {
        this.nev = nev;
        this.cikkszam = cikkszam;
        this.bruttoKisker = bruttoKisker;
    }

    @Override
    public String toString () {
        String toString = "";
        toString = this.nev+"\n"+this.cikkszam+"\n"+Integer.toString(this.bruttoKisker)+" Ft";
        return toString;
    }
}

public ui_main() throws  {
    initComponents();
    DefaultListModel model = new DefaultListModel<ARMAJTermek>();
    TermekList.setModel(model);

    ARMAJTermek termek[2];
    termek[0] = new ARMAJTermek("lorem", "ipsum", 1);
    termek[1] = new ARMAJTermek("lorem2", "ipsum2", 2);
}

public void addTermek(JList TermekList) {
    DefaultListModel model = new DefaultListModel<ARMAJTermek>();
    model.clear();
    if(TermekList.getSelectedIndex() == 0){
            TermekList.setSelectedIndex(1);
        }
    TermekList.removeAll();
    for ( int i = 0; i < termek.length; i++ ) {
        model.addElement(termek[i]);
    }
    TermekList.setModel(model);
        TermekList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        TermekList.revalidate();
        TermekList.repaint();
}

public void getTermek(JList TermekList) {
    ARMAJTermek termek = (ARMAJTermek)TermekList.getSelectedValue();
    ARMAJTermek termek = (ARMAJTermek)TermekList.getModel().getElementAt(TermekList.getSelectedIndex());
}

I made it run by creating a new function inside the function, that can take and store the ARMAJTermek object:我通过在函数内部创建一个新函数来运行它,该函数可以获取和存储 ARMAJTermek 对象:

ARMAJTermek termek = ARMAStaticFunctions.getTermek(TermekList);

public static ARMAJTermek getTermek(JList TermekList) {
        ARMAJTermek termek = (ARMAJTermek)TermekList.getSelectedValue();
        return termek;
}

And now it works as intended, not trying to convert objects to String.现在它按预期工作,而不是尝试将对象转换为字符串。

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

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