I cannot explain this problem, and in searching all over I've seen a few leads but no help. I do not understand at all why I get this exception. It claims I am trying to cast a JList as a DefaultListModel when I'me trying to cast a ListModel and a DefaultListModel (yes, I instantiated the JList with a DefaultListModel).

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JList$4 cannot be cast to javax.swing.DefaultListModel
	at SchoolClassDisplay.actionPerformed(SchoolClassDisplay.java:171)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JSeparator;
import javax.swing.JPanel;
import javax.swing.JList;
import javax.swing.WindowConstants;
import javax.swing.DefaultListModel;

import java.util.ArrayList;

public class SchoolClassDisplay implements ActionListener {
	
	private SchoolClass schoolClass; //class being edited
	private ActionListener ownerActionListener; //parent frame listener
	private String saveOptionActionCommand; //command for parent frame notification
	private String cancelOptionActionCommand; //command for parent frame notification
	
	public SchoolClassDisplay (ActionListener ownerFrameActionListener, String saveClassCommand, String cancelClassCommand, SchoolClass schClass) throws NullPointerException {
		/*if (ownerFrameActionListener == null)
			throw new NullPointerException ("ownerFrameActionListener cannot be null");*/
		if (saveClassCommand == null)
			throw new NullPointerException ("saveTypeCommand cannot be null");
		if (cancelClassCommand == null)
			throw new NullPointerException ("cancelTypeCommand cannot be null");
		
		schoolClass = schClass;
		ownerActionListener = ownerFrameActionListener;
		saveOptionActionCommand = saveClassCommand;
		cancelOptionActionCommand = cancelClassCommand;
	}
	
	//GUI parts
	private JFrame frame;
	private JTextField name;
	private JTextField location;
	private JList times;
	private JButton addTime; //open add time slot prompt
	private JButton delTime;
	private JButton save;
	private JButton cancel;
	
	private DayTimeDisplay dayTimeEditor; //time slot editor
	private String saveDayTimeActionCommand = "saveDayTime";//command linking above to this
	
	public void show() {
		
		GridBagLayout gridBag = new GridBagLayout();
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.BOTH;
		gbc.anchor = GridBagConstraints.NORTHWEST;
		
		frame = new JFrame ("Class Editor");
		frame.setLayout(gridBag);
		frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		if (schoolClass != null)
			frame.setTitle(schoolClass.getName());
		
		JLabel a = new JLabel("Name: ");
		gbc.gridwidth = 1;
		gridBag.setConstraints(a, gbc);
		frame.add(a);
		name = new JTextField("");
		if (schoolClass != null)
			name.setText(schoolClass.getName());
		gbc.weightx = 1;
		gbc.gridwidth = GridBagConstraints.REMAINDER;
		gridBag.setConstraints(name, gbc);
		frame.add(name);
		
		a = new JLabel("Location: ");
		gbc.weightx = 0;
		gbc.gridwidth = 1;
		gridBag.setConstraints(a, gbc);
		frame.add(a);
		location = new JTextField("");
		if (schoolClass != null)
			location.setText(schoolClass.getLocation());
		gbc.weightx = 1;
		gbc.gridwidth = GridBagConstraints.REMAINDER;
		gridBag.setConstraints(location, gbc);
		frame.add(location);
		
		a = new JLabel("Times: ");
		gbc.weightx = 0;
		gbc.gridwidth = 1;
		gridBag.setConstraints(a, gbc);
		frame.add(a);
		times = new JList(new DefaultListModel());
		if (schoolClass != null)
			times.setListData(schoolClass.getTimeSlots().toArray());
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.gridwidth = GridBagConstraints.REMAINDER;
		gridBag.setConstraints(times, gbc);
		frame.add(times);
		
		a = new JLabel(" ");
		gbc.weightx = 0;
		gbc.weighty = 1;
		gbc.gridwidth = 1;
		gridBag.setConstraints(a, gbc);
		frame.add(a);
		JPanel buttons1 = new JPanel(new GridLayout(1,2));
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.gridwidth = GridBagConstraints.REMAINDER;
		gridBag.setConstraints(buttons1, gbc);
		frame.add(buttons1);
		addTime = new JButton("Add");
		addTime.addActionListener(this);
		addTime.setActionCommand("addTime");
		buttons1.add(addTime);
		delTime = new JButton("Delete");
		delTime.addActionListener(this);
		delTime.setActionCommand("delTime");
		buttons1.add(delTime);
		
		JSeparator s = new JSeparator();
		gbc.weightx = 0;
		gbc.weighty = 0;
		gridBag.setConstraints(s, gbc);
		frame.add(s);
		
		JPanel buttons2 = new JPanel(new GridLayout(1,2));
		gridBag.setConstraints(buttons2, gbc);
		frame.add(buttons2);
		save = new JButton("Save");
		save.addActionListener(this);
		save.addActionListener(ownerActionListener);
		save.setActionCommand(saveOptionActionCommand);
		buttons2.add(save);
		cancel = new JButton("Cancel");
		cancel.addActionListener(this);
		cancel.addActionListener(ownerActionListener);
		cancel.setActionCommand(cancelOptionActionCommand);
		buttons2.add(cancel);
		
		JPanel blank = new JPanel();
		blank.setPreferredSize(new java.awt.Dimension(0,0));
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.gridwidth = GridBagConstraints.REMAINDER;
		gbc.gridheight = GridBagConstraints.REMAINDER;
		gridBag.setConstraints(blank,gbc);
		frame.add(blank);
		
		
		frame.pack();
		frame.setVisible(true);
	}
	
	public void actionPerformed (ActionEvent a) {
		//open add time slot dialogue
		if (a.getActionCommand().equals(addTime.getActionCommand())) {
			frame.setEnabled(false);
			dayTimeEditor = new DayTimeDisplay(this, saveDayTimeActionCommand, null);
			dayTimeEditor.show();
		}
		//remove time slot
		if (a.getActionCommand().equals(delTime.getActionCommand())) {
			if (times.getSelectedIndex() != -1) {
				((DefaultListModel)(times.getModel())).remove(times.getSelectedIndex());
			}
		}
		//command received from time slot editor indicating "Add" pressed finalizing creation
		if (a.getActionCommand().equals(saveDayTimeActionCommand)) {
			frame.setEnabled(true);
			frame.requestFocus();
			DayTime oldDayTime = dayTimeEditor.getUneditedAssignment(); //store old if was editing mode
			DayTime newDayTime = dayTimeEditor.getSavedAssignment();
			
			if(newDayTime == null) //failed creation
				return;
			
			if (oldDayTime == null)
				((DefaultListModel)(times.getModel())).addElement(newDayTime);
			else {
				int index = ((DefaultListModel)(times.getModel())).indexOf(oldDayTime);
				((DefaultListModel)(times.getModel())).set(index, newDayTime);
			}
		}
	}
	
	//test purposes only
	public static void main (String[] args) {
		
		ArrayList<DayTime> daytimes = new ArrayList<DayTime>();
		int[] days1 = {0,1,0,1,0,1,0};
		int[] days2 = {0,0,1,0,1,0,0};
		daytimes.add(new DayTime(days1,10,30));
		daytimes.add(new DayTime(days2,14,30));
		SchoolClass associatedClass1 = new SchoolClass("Math","School",daytimes);
		
		
		SchoolClassDisplay a = new SchoolClassDisplay (null, "saveClass", "cancelClass", associatedClass1);
		a.show();
	}
}

I tried to reproduce this problem in a very simple setting; the same basic thing is happening, lines 11 and 171 are identical and set up the same way. However, this brought NO exception. What is going on?

import javax.swing.*;

public class dfgdrtgrtg {

	private JList times;
	
	public dfgdrtgrtg () {
		
		times = new JList(new DefaultListModel());
		
		DefaultListModel listModel = (DefaultListModel)(times.getModel());
		
	}
	
	public static void main (String[] args) {
		
	dfgdrtgrtg a = new dfgdrtgrtg();
		
		
	}
}

Thanks for the help.

Finally figured out the problem:

When I called times.setListData(Object[] array)
JList constructs a new instance of the ListModel based on its own internal, incompatible with DefaultListModel, ListModel; as if I never used the constructor passing in a DefaultListModel.
Data then must be added one at a time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.