Link to home
Start Free TrialLog in
Avatar of Zolf
ZolfFlag for United Arab Emirates

asked on

JSpinner not showing changed value

hello there,

i have a jspinner in a jtable.i want to change the value in another row cell when i change the value in the spinner.but it doesnt not do.i did a println to see if the spinner value is being sent.but it does not.

cheers
zolf
Avatar of ksivananth
ksivananth
Flag of United States of America image

can you post the code?
Avatar of CPlusJavaCSharp
CPlusJavaCSharp

getNextValue(), getPreviousValue() and getValue() - these Jspiner methods may help...
Avatar of Zolf

ASKER


SpinnerModel numbers1=new SpinnerNumberModel(0,0,100,1);
		discountSP1=new JSpinner(numbers1);
		
		discountSP1.addChangeListener(new ChangeListener(){
 
			public void stateChanged(ChangeEvent e)
			{
				System.out.println("State changed"+table.getSelectedRow());
				calculateTotal(table.getSelectedRow());
			}
			
		});
		
		ctm.setColumnIdentifiers(new String[]{"Product Code","Product Name","Quantity","Unit","Unit Price","Discount","Total","Tax"});
		table.getColumnModel().getColumn(2).setCellEditor(new SpinnerCellEditor(discountSP1));
 
 
---------------------------------------------------
 
private void calculateTotal(int row)
	{
		int quantity=(Integer)this.table.getValueAt(row,2);
		System.out.println(quantity);
		double price=(Double)table.getValueAt(row,4);
		double discount=(Integer)table.getValueAt(row,5);
		table.setValueAt(price*quantity*(1-discount/100),row,6);
		calculateGrandTotal();
	}

Open in new window

can you show the code of SpinnerCellEditor?
Avatar of Zolf

ASKER


	private JSpinner editor;
	
	public SpinnerCellEditor(int value, int minimum, int maximum, int stepSize) 
	{
		SpinnerModel numbers=new SpinnerNumberModel(value,minimum,maximum,stepSize);
		editor=new JSpinner(numbers);
	}
	
	public SpinnerCellEditor( JSpinner spinner )
    {
        this.editor = spinner;
    }
	
	public Component getTableCellEditorComponent(JTable table, Object value,
			boolean isSelected, int row, int column) 
	{
		editor.setValue(value);
		return editor;
	}
 
	public Object getCellEditorValue()
	{
		return editor.getValue();
	}
 
}

Open in new window

Avatar of CEHJ
Can we assume all your println statements are giving expected results?
Avatar of Zolf

ASKER


CEHJ:

what do you mean??
System.out.println. Are they working?
(Oh - apart from the one you mentioned doesn't work)
Avatar of Zolf

ASKER


yes they are
You missed out part of the SpinnerCellEditor - can you post it again?
Avatar of Zolf

ASKER

public class SpinnerCellEditor extends AbstractCellEditor
      implements TableCellEditor{


      private static final long serialVersionUID = 1L;

      private JSpinner editor;
      
      public SpinnerCellEditor(int value, int minimum, int maximum, int stepSize)
      {
            SpinnerModel numbers=new SpinnerNumberModel(value,minimum,maximum,stepSize);
            editor=new JSpinner(numbers);
      }
      
      public SpinnerCellEditor( JSpinner spinner )
    {
        this.editor = spinner;
    }
      
      public Component getTableCellEditorComponent(JTable table, Object value,
                  boolean isSelected, int row, int column)
      {
            editor.setValue(value);
            return editor;
      }

      public Object getCellEditorValue()
      {
            return editor.getValue();
      }

}
you should be adding your listener to the SpinnerNumberModel that you create inside your SpinnerCellEditor.

eg.
      public SpinnerCellEditor(int value, int minimum, int maximum, int stepSize)
      {
            SpinnerModel numbers=new SpinnerNumberModel(value,minimum,maximum,stepSize);
            editor=new JSpinner(numbers);
                numbers.addChangeListener(new ChangeListener(){
 
                        public void stateChanged(ChangeEvent e)
                        {
                                System.out.println("State changed"+table.getSelectedRow());
                                calculateTotal(table.getSelectedRow());
                        }
                       
                });

Avatar of Zolf

ASKER


objects:

does it make a difference if i add them in the SpinnerCellEditor or in the class where i am using it
SpinnerModel numbers1=new SpinnerNumberModel(0,0,100,1);
		discountSP1=new JSpinner(numbers1);
		
		discountSP1.addChangeListener(new ChangeListener(){
 
			public void stateChanged(ChangeEvent e)
			{
				System.out.println("State changed"+table.getSelectedRow());
				calculateTotal(table.getSelectedRow());
			}
			
		});
 
table.getColumnModel().getColumn(2).setCellEditor(new SpinnerCellEditor(discountSP1));

Open in new window

>>does it make a difference if i add them in the SpinnerCellEditor or in the class where i am using it

nope, you can just add it in the place where you create the model!

SpinnerModel numbers1=new SpinnerNumberModel(0,0,100,1);
            discountSP1=new JSpinner(numbers1);
            
            numbers1.addChangeListener(new ChangeListener(){
 
                  public void stateChanged(ChangeEvent e)
                  {
                        System.out.println("State changed"+table.getSelectedRow());
                        calculateTotal(table.getSelectedRow());
                  }
                  
            });

would do...
Avatar of Zolf

ASKER


i am doing this same thing.i meaning when i change value in the jspinner, value in a textfield is changing.but this is not working in jtable
Avatar of Zolf

ASKER



i am using the jspiiner object with this method
.addChangeListener(new ChangeListener(){

not the SpinnerModel
Avatar of Zolf

ASKER


int quantity=(Integer)table.getValueAt(row,2);

this is not returning the value in the spinner when i increment or decrement the  value
>>int quantity=(Integer)table.getValueAt(row,2);

this would return the old value... and would return the value of the changed updated spinner only if you move the focus out of that sell! instead you may try to get the value from the spinner!
Avatar of Zolf

ASKER


ksivananth:

what do i need to do.is there a way out
ASKER CERTIFIED SOLUTION
Avatar of ksivananth
ksivananth
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zolf

ASKER


how
The value must be obtainable from the TableModel for things to work correctly
>>The value must be obtainable from the TableModel for things to work correctly

in this case, the value is not even updated in the tablemodel as the user didn't complete the cell update...
Zolf, what's the position here - is the problem solved?
Avatar of Zolf

ASKER


yes it got solved
Well i'm confused. You awarded

>>did you try getting the value directly from the spinner?

as the correct answer, although as i mentioned, if things are working correctly, you can get the value from the table model (the only place you *should* get values from)

Then you said

>>How?

which implied you have further questions.

The way it should work is that the calculation is done when you finish moving the spinner, and then the calculated value appears in the table
Avatar of Zolf

ASKER



that "how" was for ksivananth.who suggested

>>did you try getting the value directly from the spinner?
>>as the correct answer, although as i mentioned, if things are working correctly, you can get the value from the table model (the only place you *should* get values from)
>>

pls go through all the comments, you should be getting the real issue which I suspected and given a comment based on that... if not, the auther may give you the explanation!

Below is what I suspected and which has been the real cause,

in this case, the value is not even updated in the tablemodel as the user didn't complete the cell update...
Well i must confess, i was a little confused. Perhaps, then, there was nothing wrong in the first place?