Custom Classes and JLists

I work in NetBeans in a Java project.

I need to show everyone State(for example, Oklahoma) in ArrayList<State>c JList.

I cannot figure out how to do this ... especially in NetBeans.

I think that this is connected with the creation of DefaultListModelevery Statehow String. I tried this in a million different ways to no avail.

Is it possible to just load ArrayList<State>(maybe if the class Statehas a method toString())? This would be especially useful because I could change directly ArrayList<State>through actionPerformed().

For the question, suppose that Stateis an object with nametype Stringand a populationtype int.

Hope this makes sense.

Thank.

+1
source share
5 answers

All of these answers are great and certainly helpful. I finished it very simply from the controller:

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

The key is to use .setListData()(which may take Array as an argument) instead .setModel().

0
source

Unfortunately, the only "simple" JList initialization with a list is via Vector

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

- DefaultListModel, ArrayList, State . JList. , , ArrayList , , , , AbstractListModel, , , .

+2

(+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);

​​ -, . , . , . .

, .

+2

, GUI Matisse, .

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

}

ListModel initComponents() .

stateListModel = new StateListModel(myListofStates);

In NetBeans GUI Builder, right-click on your JLIst and select "customize code." You will see something in the lines jList1.setModel (...) Change the drop-down list to a custom property and edit the code to read

jList1.setModel(stateListModel);
+2
source

All Articles