Home arrow static arrow Java Programming [Archive] - Problem with JSpinner in JTable
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Problem with JSpinner in JTable
This topic has 3 replies on 1 page.

Posts:82
Registered: 3/27/02
Problem with JSpinner in JTable  
Jul 19, 2004 1:39 AM



 
Hi all,

i have a Spinner in a table, my problem is when i change the value of spinner in the table i get this Exception

java.lang.ClassCastException
at SpinnerListener.stateChanged(SpinnerListener.java:8)
i read in the api this is Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.

here are my sources

 /The Spinner class import java.io.Serializable;import javax.swing.*;import javax.swing.event.*;import java.lang.Comparable; public class Spinner extends Object {      	   	protected Integer value, stepSize;	protected Comparable minimum, maximum;    	protected SpinnerNumberModel mModel;           	public Spinner(SpinnerNumberModel mModel){   		   		 //mModel = new SpinnerNumberModel( value, minimum,  maximum, stepSize);   		mModel.addChangeListener(new SpinnerListener());   		value = (Integer)mModel.getValue();   		//value = this.value;   		stepSize = (Integer)mModel.getStepSize();   		//stepSize = this.stepSize;   		minimum = (Integer)mModel.getMinimum();   		//minimum = this.minimum;   		maximum = (Integer)mModel.getMaximum();   		    	}               	public Spinner(Object value ){   		   		setValue(value);   		setStepSize(stepSize);	   	   	}    	public Object getNextValue() {   	   		return getNextValue();         	}		public Object getPreviousValue() {         return getPreviousValue();              }		    public String toString(){		return value.toString();		}    	public Object getValue() {         return value;     }	        public void setValue(Object value) {				//this.value = (Integer)value;  		this.value = (Integer)value;     }         public void setMaximum(Comparable maximum){    	this.maximum = (Integer)maximum;	    	}        public Object getMaximum(){    	    	return mModel.getMaximum();    }    	     public void setStepSize(Object stepSize) {    	    	//this.stepSize = stepSize; 		//this.stepSize = (Integer)mModel.getStepSize();          this.stepSize = (Integer)stepSize;          }          public Object getStepSize(){    	    	return this.stepSize;    }         public class SpinnerListener implements ChangeListener {        public void stateChanged(ChangeEvent evt) {            Spinner spinner = (Spinner)evt.getSource();                //Get the new value            Object value = spinner.getValue();        }    }      } ................................................................................................... //The Editor import javax.swing.table.TableCellRenderer;import javax.swing.table.TableCellEditor;import javax.swing.*;import java.awt.Component;import java.awt.event.*;import java.util.EventObject;  public class SpinnerCellEditor extends AbstractCellEditor implements TableCellEditor {        	protected JSpinner mSpinner ;	// Initializes the spinner.public SpinnerCellEditor() {		int tMin = 0;	int tMax = 255;    int tStep = 1;    int tInitValue = 216;   	SpinnerNumberModel tModel = new SpinnerNumberModel(tInitValue, tMin, tMax, tStep);   	tModel.addChangeListener(new SpinnerListener());    mSpinner = new JSpinner(tModel);       	}    // Prepares the spinner component and returns it.public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int col) {				if (value instanceof Spinner) {			           	Spinner tSp = (Spinner)value;           	mSpinner.setValue(tSp.getValue());        }			cancelCellEditing(); 			return mSpinner;	}		public boolean isCellEditable(EventObject evt) {            if (evt instanceof MouseEvent) {                return ((MouseEvent)evt).getClickCount() >= 1;            }            return true;        } 	public Object getCellEditorValue() {				return new Spinner(mSpinner.getValue());		} 	public boolean stopCellEditing() {			try {			    	mSpinner.commitEdit();	    		} catch (Exception e) { 				 }			//return true;		return super.stopCellEditing();		//return stopCellEditing();}  } ................................................................................................... //The Rendere import javax.swing.table.TableCellRenderer;import javax.swing.table.TableCellEditor;import javax.swing.*;import java.awt.Component;import java.awt.event.*;import java.util.EventObject; public class SpinnerRenderer extends JSpinner implements TableCellRenderer { public SpinnerRenderer() {	      }    public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int column) {        if (value instanceof Spinner) {        	//SpinnerNumberModel tModel = new SpinnerNumberModel();             Spinner tSp = (Spinner)value;          	setValue(tSp.getValue());          	fireStateChanged();         }        if (table != null)            if (table.isCellEditable(row, column))                 setForeground(CellRendererConstants.EDITABLE_COLOR);            else                setForeground(CellRendererConstants.UNEDITABLE_COLOR);        		setBackground(table.getBackground());        		return this;}} ..................................................................................................... //The Listener import javax.swing.*;import javax.swing.event.*; public class SpinnerListener implements ChangeListener {        public void stateChanged(ChangeEvent evt) {            JSpinner spinner = (JSpinner)evt.getSource();                //Get the new value            Object value = spinner.getValue();        }    }   

can anybody help please?

regards
gebi
 

Posts:1,872
Registered: 2/12/03
Re: Problem with JSpinner in JTable  
Jul 19, 2004 3:42 AM (reply 1 of 3)



 
exactly what the execption stated: wrong casting

should be SpinnerModel .. not JSpinner (because you added SpinnerListener to the SpinnerModel)

import javax.swing.*;import javax.swing.event.*; public class SpinnerListener implements ChangeListener {            public void stateChanged(ChangeEvent evt) {                    // JSpinner spinner = (JSpinner)evt.getSource();                        //Get the new value                    //Object value = spinner.getValue();                     SpinnerModel model = (SpinnerModel) evt.getSource();        Object value = model.getValue();    }} 
 

Posts:82
Registered: 3/27/02
Re: Problem with JSpinner in JTable  
Jul 19, 2004 5:00 AM (reply 2 of 3)



 
Thank you for responce...

but value looks now like this Spinner@442c76, do you have an idea what todo to get the of Spinner?

best regards
gebi
 

Posts:4,906
Registered: 23/07/02
Re: Problem with JSpinner in JTable  
Jul 19, 2004 5:57 AM (reply 3 of 3)



 
You need to add a TableCellRenderer which renders components as themselves. You can probably find some example code by searching this forum and the Swing forum.
 
This topic has 3 replies on 1 page.