murillosmaia 25 de nov. de 2010
Bom…
Vamos partir do principio…
Se eu entendi bem, você está querendo utilizar um jtextfield para inserir Hora e Minutos…
poder ser utilizado uma classe jformattedtextfield para fazer uma validação…
http://download.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html
particularmente eu nao utilizaria um textfield para fazer uma validação de horas, você pode utilizar uma biblioteca
http://software.filestube.com/j/java+time+picker
andrestrindade 25 de nov. de 2010
murillosmaia:
Bom…
Vamos partir do principio…
Se eu entendi bem, você está querendo utilizar um jtextfield para inserir Hora e Minutos…
poder ser utilizado uma classe jformattedtextfield para fazer uma validação…
http://download.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html
particularmente eu nao utilizaria um textfield para fazer uma validação de horas, você pode utilizar uma biblioteca
http://software.filestube.com/j/java+time+picker
É isso mesmo, eu quero um jtextfield.
A intenção não é fazer a validação apenas no lado do usuário, mas sim não induzir o usuário ao erro.
Obrigado.
murillosmaia 25 de nov. de 2010
andrestrindade:
murillosmaia:
Bom…
Vamos partir do principio…
Se eu entendi bem, você está querendo utilizar um jtextfield para inserir Hora e Minutos…
poder ser utilizado uma classe jformattedtextfield para fazer uma validação…
http://download.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html
particularmente eu nao utilizaria um textfield para fazer uma validação de horas, você pode utilizar uma biblioteca
http://software.filestube.com/j/java+time+picker
É isso mesmo, eu quero um jtextfield.
A intenção não é fazer a validação apenas no lado do usuário, mas sim não induzir o usuário ao erro.
Obrigado.
Cara você precisa aprender a ordenar suas frases, tenta traduzir essa frase para mim.
Enfim… Você deseja limitar a entrada de dados entre 00:00 e 24:59 correto…
da uma olhada no formattedtextfield… ele irá resolver o seu problema…
tente entender a API… qualquer coisa eu faço um exemplo…
entanglement 25 de nov. de 2010
Eu não usaria um JFormattedField e sim um JSpinner. Um exemplo de uso de JSpinner.
package guj ;
import java.awt.Dimension ;
import java.awt.FlowLayout ;
import java.util.Calendar ;
import java.util.Date ;
import javax.swing.BorderFactory ;
import javax.swing.JLabel ;
import javax.swing.JPanel ;
import javax.swing.JSpinner ;
import javax.swing.SpinnerDateModel ;
import javax.swing.SpinnerNumberModel ;
import javax.swing.border.TitledBorder ;
public class DateTimePanel extends JPanel {
private static final long serialVersionUID = 1L ;
private JSpinner spnDate = null ;
private JLabel jLabel = null ;
private JSpinner spnHour = null ;
private JLabel jLabel1 = null ;
private JSpinner spnMinute = null ;
public DateTimePanel () {
super ();
initialize ();
}
private void initialize () {
jLabel1 = new JLabel ();
jLabel1 . setText ( ":" );
jLabel = new JLabel ();
jLabel . setText ( "-" );
this . setSize ( 372 , 87 );
this . setLayout ( new FlowLayout ());
this . setBorder ( BorderFactory . createTitledBorder ( "-" ));
this . setSize ( new Dimension ( 334 , 87 ));
this . add ( getSpnDate (), null );
this . add ( jLabel , null );
this . add ( getSpnHour (), null );
this . add ( jLabel1 , null );
this . add ( getSpnMinute (), null );
}
private JSpinner getSpnDate () {
if ( spnDate == null ) {
Calendar cal = Calendar . getInstance ();
cal . set ( Calendar . HOUR_OF_DAY , 0 );
cal . set ( Calendar . MINUTE , 0 );
cal . set ( Calendar . SECOND , 0 );
cal . set ( Calendar . MILLISECOND , 0 );
Calendar calLast = ( Calendar ) cal . clone ();
calLast . add ( Calendar . YEAR , + 100 );
spnDate = new JSpinner ( new SpinnerDateModel ( cal . getTime (),
cal . getTime (), calLast . getTime (), Calendar . DAY_OF_MONTH ));
spnDate . setEditor ( new JSpinner . DateEditor ( spnDate , "dd/MM/yyyy" ));
}
return spnDate ;
}
private JSpinner getSpnHour () {
if ( spnHour == null ) {
Calendar cal = Calendar . getInstance ();
SpinnerNumberModel snm = new SpinnerNumberModel ( cal . get ( Calendar . HOUR_OF_DAY ), 0 , 23 , 1 );
spnHour = new JSpinner ( snm );
(( JSpinner . NumberEditor ) spnHour . getEditor ()). getFormat (). applyPattern ( "00" );
}
return spnHour ;
}
private JSpinner getSpnMinute () {
if ( spnMinute == null ) {
Calendar cal = Calendar . getInstance ();
SpinnerNumberModel snm = new SpinnerNumberModel ( cal . get ( Calendar . MINUTE ), 0 , 59 , 1 );
spnMinute = new JSpinner ( snm );
(( JSpinner . NumberEditor ) spnMinute . getEditor ()). getFormat (). applyPattern ( "00" );
}
return spnMinute ;
}
public Date getTime () {
Date dt = ( Date ) spnDate . getValue ();
Calendar cal = Calendar . getInstance ();
cal . setTime ( dt );
cal . set ( Calendar . HOUR_OF_DAY , (( Number ) spnHour . getValue ()). intValue ());
cal . set ( Calendar . MINUTE , (( Number ) spnMinute . getValue ()). intValue ());
cal . set ( Calendar . SECOND , 0 );
cal . set ( Calendar . MILLISECOND , 0 );
return cal . getTime ();
}
public void setTitle ( String title ) {
(( TitledBorder ) getBorder ()). setTitle ( title );
this . repaint ();
}
public String getTitle () {
return (( TitledBorder ) getBorder ()). getTitle ();
}
}
Aqui usei 3 JSpinners.
andrestrindade 25 de nov. de 2010
entanglement:
Eu não usaria um JFormattedField e sim um JSpinner. Um exemplo de uso de JSpinner.
package guj ;
import java.awt.Dimension ;
import java.awt.FlowLayout ;
import java.util.Calendar ;
import java.util.Date ;
import javax.swing.BorderFactory ;
import javax.swing.JLabel ;
import javax.swing.JPanel ;
import javax.swing.JSpinner ;
import javax.swing.SpinnerDateModel ;
import javax.swing.SpinnerNumberModel ;
import javax.swing.border.TitledBorder ;
public class DateTimePanel extends JPanel {
private static final long serialVersionUID = 1L ;
private JSpinner spnDate = null ;
private JLabel jLabel = null ;
private JSpinner spnHour = null ;
private JLabel jLabel1 = null ;
private JSpinner spnMinute = null ;
public DateTimePanel () {
super ();
initialize ();
}
private void initialize () {
jLabel1 = new JLabel ();
jLabel1 . setText ( ":" );
jLabel = new JLabel ();
jLabel . setText ( "-" );
this . setSize ( 372 , 87 );
this . setLayout ( new FlowLayout ());
this . setBorder ( BorderFactory . createTitledBorder ( "-" ));
this . setSize ( new Dimension ( 334 , 87 ));
this . add ( getSpnDate (), null );
this . add ( jLabel , null );
this . add ( getSpnHour (), null );
this . add ( jLabel1 , null );
this . add ( getSpnMinute (), null );
}
private JSpinner getSpnDate () {
if ( spnDate == null ) {
Calendar cal = Calendar . getInstance ();
cal . set ( Calendar . HOUR_OF_DAY , 0 );
cal . set ( Calendar . MINUTE , 0 );
cal . set ( Calendar . SECOND , 0 );
cal . set ( Calendar . MILLISECOND , 0 );
Calendar calLast = ( Calendar ) cal . clone ();
calLast . add ( Calendar . YEAR , + 100 );
spnDate = new JSpinner ( new SpinnerDateModel ( cal . getTime (),
cal . getTime (), calLast . getTime (), Calendar . DAY_OF_MONTH ));
spnDate . setEditor ( new JSpinner . DateEditor ( spnDate , "dd/MM/yyyy" ));
}
return spnDate ;
}
private JSpinner getSpnHour () {
if ( spnHour == null ) {
Calendar cal = Calendar . getInstance ();
SpinnerNumberModel snm = new SpinnerNumberModel ( cal . get ( Calendar . HOUR_OF_DAY ), 0 , 23 , 1 );
spnHour = new JSpinner ( snm );
(( JSpinner . NumberEditor ) spnHour . getEditor ()). getFormat (). applyPattern ( "00" );
}
return spnHour ;
}
private JSpinner getSpnMinute () {
if ( spnMinute == null ) {
Calendar cal = Calendar . getInstance ();
SpinnerNumberModel snm = new SpinnerNumberModel ( cal . get ( Calendar . MINUTE ), 0 , 59 , 1 );
spnMinute = new JSpinner ( snm );
(( JSpinner . NumberEditor ) spnMinute . getEditor ()). getFormat (). applyPattern ( "00" );
}
return spnMinute ;
}
public Date getTime () {
Date dt = ( Date ) spnDate . getValue ();
Calendar cal = Calendar . getInstance ();
cal . setTime ( dt );
cal . set ( Calendar . HOUR_OF_DAY , (( Number ) spnHour . getValue ()). intValue ());
cal . set ( Calendar . MINUTE , (( Number ) spnMinute . getValue ()). intValue ());
cal . set ( Calendar . SECOND , 0 );
cal . set ( Calendar . MILLISECOND , 0 );
return cal . getTime ();
}
public void setTitle ( String title ) {
(( TitledBorder ) getBorder ()). setTitle ( title );
this . repaint ();
}
public String getTitle () {
return (( TitledBorder ) getBorder ()). getTitle ();
}
}
Aqui usei 3 JSpinners.
Sua ideia é perfeita. Vou usar isso mesmo! MUITO OBRIGADO!
murillosmaia 25 de nov. de 2010
new JFormattedTextField(new SimpleDateFormat(“HH:mm”));
http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
Letter Date or Time Component Presentation Examples
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
murillosmaia 25 de nov. de 2010
andrestrindade:
entanglement:
Eu não usaria um JFormattedField e sim um JSpinner. Um exemplo de uso de JSpinner.
package guj ;
import java.awt.Dimension ;
import java.awt.FlowLayout ;
import java.util.Calendar ;
import java.util.Date ;
import javax.swing.BorderFactory ;
import javax.swing.JLabel ;
import javax.swing.JPanel ;
import javax.swing.JSpinner ;
import javax.swing.SpinnerDateModel ;
import javax.swing.SpinnerNumberModel ;
import javax.swing.border.TitledBorder ;
public class DateTimePanel extends JPanel {
private static final long serialVersionUID = 1L ;
private JSpinner spnDate = null ;
private JLabel jLabel = null ;
private JSpinner spnHour = null ;
private JLabel jLabel1 = null ;
private JSpinner spnMinute = null ;
public DateTimePanel () {
super ();
initialize ();
}
private void initialize () {
jLabel1 = new JLabel ();
jLabel1 . setText ( ":" );
jLabel = new JLabel ();
jLabel . setText ( "-" );
this . setSize ( 372 , 87 );
this . setLayout ( new FlowLayout ());
this . setBorder ( BorderFactory . createTitledBorder ( "-" ));
this . setSize ( new Dimension ( 334 , 87 ));
this . add ( getSpnDate (), null );
this . add ( jLabel , null );
this . add ( getSpnHour (), null );
this . add ( jLabel1 , null );
this . add ( getSpnMinute (), null );
}
private JSpinner getSpnDate () {
if ( spnDate == null ) {
Calendar cal = Calendar . getInstance ();
cal . set ( Calendar . HOUR_OF_DAY , 0 );
cal . set ( Calendar . MINUTE , 0 );
cal . set ( Calendar . SECOND , 0 );
cal . set ( Calendar . MILLISECOND , 0 );
Calendar calLast = ( Calendar ) cal . clone ();
calLast . add ( Calendar . YEAR , + 100 );
spnDate = new JSpinner ( new SpinnerDateModel ( cal . getTime (),
cal . getTime (), calLast . getTime (), Calendar . DAY_OF_MONTH ));
spnDate . setEditor ( new JSpinner . DateEditor ( spnDate , "dd/MM/yyyy" ));
}
return spnDate ;
}
private JSpinner getSpnHour () {
if ( spnHour == null ) {
Calendar cal = Calendar . getInstance ();
SpinnerNumberModel snm = new SpinnerNumberModel ( cal . get ( Calendar . HOUR_OF_DAY ), 0 , 23 , 1 );
spnHour = new JSpinner ( snm );
(( JSpinner . NumberEditor ) spnHour . getEditor ()). getFormat (). applyPattern ( "00" );
}
return spnHour ;
}
private JSpinner getSpnMinute () {
if ( spnMinute == null ) {
Calendar cal = Calendar . getInstance ();
SpinnerNumberModel snm = new SpinnerNumberModel ( cal . get ( Calendar . MINUTE ), 0 , 59 , 1 );
spnMinute = new JSpinner ( snm );
(( JSpinner . NumberEditor ) spnMinute . getEditor ()). getFormat (). applyPattern ( "00" );
}
return spnMinute ;
}
public Date getTime () {
Date dt = ( Date ) spnDate . getValue ();
Calendar cal = Calendar . getInstance ();
cal . setTime ( dt );
cal . set ( Calendar . HOUR_OF_DAY , (( Number ) spnHour . getValue ()). intValue ());
cal . set ( Calendar . MINUTE , (( Number ) spnMinute . getValue ()). intValue ());
cal . set ( Calendar . SECOND , 0 );
cal . set ( Calendar . MILLISECOND , 0 );
return cal . getTime ();
}
public void setTitle ( String title ) {
(( TitledBorder ) getBorder ()). setTitle ( title );
this . repaint ();
}
public String getTitle () {
return (( TitledBorder ) getBorder ()). getTitle ();
}
}
Aqui usei 3 JSpinners.
Sua ideia é perfeita. Vou usar isso mesmo! MUITO OBRIGADO!
bom exemplo… mas vale lembrar q existem bibliotecas q ja fazem isso…
nao tenho certeza da jcalendar… mas a time picker… e outras… isso já está pronto…
foi o que citei no primeiro comentario… só procurar os docs e implementar…
nao há necessidade de reinventar a roda…
andrestrindade 25 de nov. de 2010
Murilo obrigado! Neste caso a alternativa do JSpinner atende melhor!