JSpinner with DateModel, setValue not working ?  
Author Message
philippe.barthelemy





PostPosted: 2005-11-17 15:59:00 Top

java-programmer, JSpinner with DateModel, setValue not working ? Hi,

I wrote a JSpinner-based Class that I use as a Time Chooser.
The spinner has a DateModel.

My problem is that the setValue does not work : the content of the
spinner is not updated.

Any clue, any one ?

thanks in advance,
--P

the TimeSpinner Class :


package fr.heartbit.viewer;

import javax.swing.*;
import javax.swing.event.*;
import java.text.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.text.*;

public class TimeSpinner
extends JSpinner {
JFormattedTextField tf;

public TimeSpinner(){
super();
final SpinnerDateModel model = new SpinnerDateModel();
this.setModel(model);

JSpinner.DateEditor editor = new
JSpinner.DateEditor(this,"HH:ss:mm");
this.setEditor(editor);

tf = ((JSpinner.DateEditor)this.getEditor()).getTextField();
tf.setEditable(true);
tf.setBackground(Color.white);
tf.setSelectionColor(Color.blue);
tf.setSelectedTextColor(Color.white);
DefaultFormatterFactory factory =
(DefaultFormatterFactory)tf.getFormatterFactory();
DateFormatter formatter =
(DateFormatter)factory.getDefaultFormatter();
formatter.setAllowsInvalid(false);

}

}

 
hiwa





PostPosted: 2005-11-17 17:22:00 Top

java-programmer >> JSpinner with DateModel, setValue not working ? email***@***.com 銇儭銉冦偦銉笺偢:

> Hi,
>
> I wrote a JSpinner-based Class that I use as a Time Chooser.
> The spinner has a DateModel.
>
> My problem is that the setValue does not work : the content of the
> spinner is not updated.
>
> Any clue, any one ?
>
> thanks in advance,
> --P
>
> the TimeSpinner Class :
>
>
> package fr.heartbit.viewer;
>
> import javax.swing.*;
> import javax.swing.event.*;
> import java.text.*;
> import java.awt.*;
> import java.util.*;
> import java.awt.event.*;
> import javax.swing.text.*;
>
> public class TimeSpinner
> extends JSpinner {
> JFormattedTextField tf;
>
> public TimeSpinner(){
> super();
> final SpinnerDateModel model = new SpinnerDateModel();
> this.setModel(model);
>
> JSpinner.DateEditor editor = new
> JSpinner.DateEditor(this,"HH:ss:mm");
> this.setEditor(editor);
>
> tf = ((JSpinner.DateEditor)this.getEditor()).getTextField();
> tf.setEditable(true);
> tf.setBackground(Color.white);
> tf.setSelectionColor(Color.blue);
> tf.setSelectedTextColor(Color.white);
> DefaultFormatterFactory factory =
> (DefaultFormatterFactory)tf.getFormatterFactory();
> DateFormatter formatter =
> (DateFormatter)factory.getDefaultFormatter();
> formatter.setAllowsInvalid(false);
>
> }
>
> }
JSpinner has a bug for date model:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6321570

 
philippe.barthelemy





PostPosted: 2005-11-17 18:20:00 Top

java-programmer >> JSpinner with DateModel, setValue not working ? Hi,

I do not think it is related to this bug.
this debug is related to the arrow on the spinner control.
my issue is with call to the setValue method : the content of the
spinner's TextFiled is not updated after a call to this method.


thanks,
--p

 
 
Michael Dunn





PostPosted: 2005-11-17 19:41:00 Top

java-programmer >> JSpinner with DateModel, setValue not working ?
<email***@***.com> wrote in message
news:email***@***.com...
> Hi,
>
> I do not think it is related to this bug.
> this debug is related to the arrow on the spinner control.
> my issue is with call to the setValue method : the content of the
> spinner's TextFiled is not updated after a call to this method.

setValue works OK in this (1.5.0_05)
the 'seconds' field (the middle field) changes when the button is clicked
(provided you have 1+ seconds between clicks)


import javax.swing.*;
import javax.swing.event.*;
import java.text.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.text.*;
class Testing extends JFrame
{
public Testing()
{
setLocation(300,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
final TimeSpinner ts = new TimeSpinner();
final JButton btn = new JButton("Change Time");
getContentPane().add(ts,BorderLayout.NORTH);
getContentPane().add(btn,BorderLayout.SOUTH);
pack();
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
ts.setValue(new Date());}});
}
public static void main(String[] args){new Testing().setVisible(true);}
}
class TimeSpinner extends JSpinner
{
JFormattedTextField tf;
public TimeSpinner()
{
super();
final SpinnerDateModel model = new SpinnerDateModel();
this.setModel(model);
JSpinner.DateEditor editor = new JSpinner.DateEditor(this,"HH:ss:mm");
this.setEditor(editor);
tf = ((JSpinner.DateEditor)this.getEditor()).getTextField();
tf.setEditable(true);
tf.setBackground(Color.white);
tf.setSelectionColor(Color.blue);
tf.setSelectedTextColor(Color.white);
DefaultFormatterFactory factory = (DefaultFormatterFactory)tf.getFormatterFactory();
DateFormatter formatter = (DateFormatter)factory.getDefaultFormatter();
formatter.setAllowsInvalid(false);
}
}


 
 
philippe.barthelemy





PostPosted: 2005-11-22 15:45:00 Top

java-programmer >> JSpinner with DateModel, setValue not working ? Yes, thanks...
I got it to work indeed...

-p