自定义类和 JList

发布于 2024-11-02 03:43:18 字数 680 浏览 6 评论 0原文

我正在 NetBeans 中从事 Java 项目。

我需要在 JListArrayList 中显示每个 State(例如“Oklahoma”)。

我不知道如何做到这一点......尤其是在 NetBeans 中。

我认为它涉及创建一个 DefaultListModel,其中每个 State 作为 String。我已经尝试过一百万种不同的方法,但都无济于事。

是否可以仅加载 ArrayList (也许如果 State 类具有 toString() 方法)?这将特别有用,因为我可以通过 actionPerformed() 直接修改 ArrayList

为了解决这个问题,我们假设 State 是一个 name 类型为 Stringpopulation 的对象> 类型为int

我希望这是有道理的。

谢谢。

I'm working in NetBeans on a Java Project.

I need to display every State (e.g. "Oklahoma") in an ArrayList<State> in a JList.

I can't figure out how to do this... especially not in NetBeans.

I think it involves creating a DefaultListModel with each State as a String. I've tried this a million different ways to no avail.

Is it possible to just load the ArrayList<State> (perhaps if the State class has a toString() method)? This would be especially helpful, because I could directly modify the ArrayList<State> via actionPerformed().

For the sake of the question, let's assume a State is an object with a name of type String and a population of type int.

I hope this makes sense.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

恍梦境° 2024-11-09 03:43:19

所有这些答案都很棒,而且当然有用。我最终从控制器中非常简单地解决了这个问题:

view.getStatesList().setListData(model.getStates().toArray());

关键是使用 .setListData() (可以将数组作为参数)而不是 .setModel()

All these answers are great, and certainly useful. I ended up solving it very simply from the controller:

view.getStatesList().setListData(model.getStates().toArray());

The key is the use of .setListData() (which can take an Array as an argument) instead of .setModel().

末骤雨初歇 2024-11-09 03:43:18

不幸的是,使用 List 的 JList 唯一“简单”初始化是通过 Vector

Vector<State> myStates = new Vector();
//add states
JList list = new JList(myStates);

Unfortunately, the only 'easy' initialization of JList with a List is via Vector

Vector<State> myStates = new Vector();
//add states
JList list = new JList(myStates);
江城子 2024-11-09 03:43:18

最简单的解决方案是创建一个 DefaultListModel,然后迭代 ArrayList,将 State 对象添加到列表模型中。然后使用这个模型作为您的 JList 的模型。另一方面,如果您想在模型中使用 ArrayList,那么您可能需要创建一个扩展 AbstractListModel 类的类,但要准备好充实该类的更多方法才能使其正常工作。

The easiest solution is to create a DefaultListModel and then iterate through the ArrayList adding your State objects into the list model as you go. Then use this model as the model of your JList. If on the other hand you want to use the ArrayList in the model, then you'll likely need to create a class that extends AbstractListModel class, but be ready to have to flesh out more of the methods of this class for it to work well.

我不会写诗 2024-11-09 03:43:18

这里提供的所有答案都很棒(+1)。在我看来,您最好创建自己的模型,稍后您可以轻松地重用该模型,了解它的工作原理。

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

/**
 * 
 * @author Konrad Borowiecki
 */
public class ObjectDataTableModel extends AbstractTableModel
{
    private static final long serialVersionUID = 1L;
    private List<String> columnNames = new ArrayList<String>();
    private List<List<Object>> data = new ArrayList<List<Object>>();
    public ObjectDataTableModel()
    {
    }

    public void setColumnNames(List<String> cNames)
    {
        this.columnNames = cNames;
    }

    public void setData(List<List<Object>> dbData)
    {
        this.data = dbData;
    }

    public List<List<Object>> getData()
    {
        return data;
    }   

    @Override
    public void setValueAt(Object value, int row, int col)
    {
        List<Object> allRow = this.data.get(row);
        allRow.set(col, value);
    }

    @Override
    public boolean isCellEditable(int row, int col)
    {
        //  if(col == 3){       //set column with id=3 to be not editable
        //      return false;
        //  }
        return false;//true;
    }

    public boolean isDataNull()
    {
        if(this.data == null)
            return true;
        return false;
    }

    @Override
    public int getColumnCount()
    {
        return columnNames.size();
    }

    @Override
    public String getColumnName(int col)
    {
        return columnNames.get(col);
    }

    @Override
    public int getRowCount()
    {
        return data.size();
    }

    @Override
    public Object getValueAt(int row, int col)
    {
        if(data.get(row).isEmpty())
            return null;
        return data.get(row).get(col);
    }
}

这里有一个非常简单的使用示例。

ObjectDataTableModel tm = new ObjectDataTableModel();
        String[] columnNames = new String[]
        {
            "1", "2", "3"
        };//, "4", "5", "6", "7"};
        tm.setColumnNames(Arrays.asList(columnNames));
        int rNo = 30;
        List<List<Object>> data = new ArrayList<List<Object>>(rNo);
        int cNo = columnNames.length;
        for(int i = 0; i < rNo; i++)
        {
            List<Object> r = new ArrayList<Object>(cNo);
            for(int j = 0; j < cNo; j++)
                r.add("i=" + i + ", j=" + j);
            data.add(r);
        }
        tm.setData(data);

我建议您始终在某个地方(例如在实用程序包中)提供这样的模型,可供使用。您会发现这种简单的模型经常有用。它非常适合显示任何类型的数据。

祝一切顺利,博罗。

All answers provided here are great (+1). In my opinion you will be best creating your own model which you can later reuse easily knowing how it works.

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

/**
 * 
 * @author Konrad Borowiecki
 */
public class ObjectDataTableModel extends AbstractTableModel
{
    private static final long serialVersionUID = 1L;
    private List<String> columnNames = new ArrayList<String>();
    private List<List<Object>> data = new ArrayList<List<Object>>();
    public ObjectDataTableModel()
    {
    }

    public void setColumnNames(List<String> cNames)
    {
        this.columnNames = cNames;
    }

    public void setData(List<List<Object>> dbData)
    {
        this.data = dbData;
    }

    public List<List<Object>> getData()
    {
        return data;
    }   

    @Override
    public void setValueAt(Object value, int row, int col)
    {
        List<Object> allRow = this.data.get(row);
        allRow.set(col, value);
    }

    @Override
    public boolean isCellEditable(int row, int col)
    {
        //  if(col == 3){       //set column with id=3 to be not editable
        //      return false;
        //  }
        return false;//true;
    }

    public boolean isDataNull()
    {
        if(this.data == null)
            return true;
        return false;
    }

    @Override
    public int getColumnCount()
    {
        return columnNames.size();
    }

    @Override
    public String getColumnName(int col)
    {
        return columnNames.get(col);
    }

    @Override
    public int getRowCount()
    {
        return data.size();
    }

    @Override
    public Object getValueAt(int row, int col)
    {
        if(data.get(row).isEmpty())
            return null;
        return data.get(row).get(col);
    }
}

And here you have a very simple use example.

ObjectDataTableModel tm = new ObjectDataTableModel();
        String[] columnNames = new String[]
        {
            "1", "2", "3"
        };//, "4", "5", "6", "7"};
        tm.setColumnNames(Arrays.asList(columnNames));
        int rNo = 30;
        List<List<Object>> data = new ArrayList<List<Object>>(rNo);
        int cNo = columnNames.length;
        for(int i = 0; i < rNo; i++)
        {
            List<Object> r = new ArrayList<Object>(cNo);
            for(int j = 0; j < cNo; j++)
                r.add("i=" + i + ", j=" + j);
            data.add(r);
        }
        tm.setData(data);

I recommend it for you to have such a model always somewhere, e.g. in a utility package, available and ready to use. You will see how often such simple model you will find useful. This one is perfect for displaying any type of data.

All the best, Boro.

仄言 2024-11-09 03:43:18

我猜你在这里谈论的是 Matisse GUI 构建器,否则这应该是相当微不足道的。

首先,您需要

public class StateListModel extends AbstractListModel{
    private final List<State> list;

    public StateListModel(List<State> list) {
        this.list = list;
    }

    @Override
    public int getSize() {
        return list.size();
    }

    @Override
    public Object getElementAt(int index) {
        return list.get(index);
    }

}

在调用 initComponents() 之前在类的构造函数中创建自定义 ListModel,初始化 ListModel,并使其成为字段而不是局部变量。

stateListModel = new StateListModel(myListofStates);

在 NetBeans GUI 构建器中,右键单击 JLIst,然后选择“自定义代码”。
您将看到类似于 jList1.setModel(...) 的内容 将下拉列表更改为自定义属性并编辑代码以读取

jList1.setModel(stateListModel);

I presume your talking about the Matisse GUI builder here, otherwise this should be fairly trivial.

First off you need to create the custom ListModel

public class StateListModel extends AbstractListModel{
    private final List<State> list;

    public StateListModel(List<State> list) {
        this.list = list;
    }

    @Override
    public int getSize() {
        return list.size();
    }

    @Override
    public Object getElementAt(int index) {
        return list.get(index);
    }

}

in the constructor of your class initialise the ListModel before initComponents() is called, and make it a field not a local variable.

stateListModel = new StateListModel(myListofStates);

In the NetBeans GUI builder right click on your JLIst, and select 'customize code'.
You will see something along the lines of jList1.setModel(...) Change the dropdown to custom property and edit the code to read

jList1.setModel(stateListModel);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文