JButton btnAdd = new JButton("add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Main selectedValue = (Main)courseList.getSelectedValue();
if(selectedValue !=null){
orderList.addElement(chosenList);
}
}
});
i have created a addButton which adds elements from one Jlist
to another Jlist
. However, when i run my applction and click the add buttton it gives me this error in my chosenList Jlist
:
javax.swing.JList[,-2008,0,2255x182,alignmentX=0.0,alignmentY=0.0,border=,flags=50332008,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]
I believe addElement
method should be called on an instance of class DefaultListModel
.
If you previously has added a DefaultListModel
instance as the model for your orderList
, you should use following code to add the element to your orderList
.
Object selectedValue = courseList.getSelectedValue();
DefaultListModle listModel = (DefaultListModle)orderList.getModel();
listModel.addElement(selectedValue);
If you haven't set any instance of class which implements ListModel
, you should initialize your orderList
in this way:
DefaultListModel listModel = new DefaultListModel();
orderList = new JList(listModel);
// or
orderList.setModel(listModel);
Take a look at How to Use Lists from Java Tutorials.