Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
DateFormatter/MaskFormatter question
Jacky Luk
Ranch Hand
Posts: 634
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
In code below, the displayFormatter returns null, wondering why
Any one sheds some lights on this?
Thanks
Jack
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package jewellery.Sales; /** * * @author Jacky */ import org.jdesktop.swingx.calendar.SingleDaySelectionModel; import org.jdesktop.swingx.JXDatePicker; import javax.swing.*; import javax.swing.text.DefaultFormatterFactory; import javax.swing.text.DateFormatter; import java.text.DateFormat; import java.text.ParseException; import java.util.*; import java.awt.*; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.text.MaskFormatter; /** * This is licensed under LGPL. License can be found here: http://www.gnu.org/licenses/lgpl-3.0.txt * * This is provided as is. If you have questions please direct them to charlie.hubbard at gmail dot you know what. */ public class DateTimePicker extends JXDatePicker { private JSpinner timeSpinner; private JPanel timePanel; private DateFormat timeFormat; public DateTimePicker() { super(); getMonthView().setSelectionModel(new SingleDaySelectionModel()); } public DateTimePicker( Date d ) { this(); setDate(d); } public void commitEdit() throws ParseException { commitTime(); super.commitEdit(); } public void cancelEdit() { super.cancelEdit(); setTimeSpinners(); } @Override public JPanel getLinkPanel() { super.getLinkPanel(); if( timePanel == null ) { try { timePanel = createTimePanel(); } catch (ParseException ex) { Logger.getLogger(DateTimePicker.class.getName()).log(Level.SEVERE, null, ex); } } setTimeSpinners(); return timePanel; } private JPanel createTimePanel() throws ParseException { JPanel newPanel = new JPanel(); newPanel.setLayout(new FlowLayout()); //newPanel.add(panelOriginal); SpinnerDateModel dateModel = new SpinnerDateModel(); timeSpinner = new JSpinner(dateModel); if( timeFormat == null ) timeFormat = DateFormat.getTimeInstance( DateFormat.SHORT ); updateTextFieldFormat(); newPanel.add(new JLabel( "Time:" ) ); newPanel.add(timeSpinner); newPanel.setBackground(Color.WHITE); return newPanel; } private void updateTextFieldFormat() throws ParseException { if( timeSpinner == null ) return; JFormattedTextField tf = ((JSpinner.DefaultEditor) timeSpinner.getEditor()).getTextField(); DefaultFormatterFactory factory = (DefaultFormatterFactory) tf.getFormatterFactory(); /////////////// null here MaskFormatter displayFormatter = (MaskFormatter) factory.getDisplayFormatter(); displayFormatter.setMask("yyyy年MM月dd日"); DateFormatter formatter = (DateFormatter) factory.getDefaultFormatter(); // Change the date format to only show the hours //formatter.setFormat( timeFormat ); formatter.setFormat(timeFormat); } private void commitTime() { Date date = getDate(); if (date != null) { Date time = (Date) timeSpinner.getValue(); GregorianCalendar timeCalendar = new GregorianCalendar(); timeCalendar.setTime( time ); GregorianCalendar calendar = new GregorianCalendar(); calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, timeCalendar.get( Calendar.HOUR_OF_DAY ) ); calendar.set(Calendar.MINUTE, timeCalendar.get( Calendar.MINUTE ) ); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); Date newDate = calendar.getTime(); setDate(newDate); } } private void setTimeSpinners() { Date date = getDate(); if (date != null) { timeSpinner.setValue( date ); } } public DateFormat getTimeFormat() { return timeFormat; } public void setTimeFormat(DateFormat timeFormat) throws ParseException { this.timeFormat = timeFormat; updateTextFieldFormat(); } public static void main(String[] args) throws ParseException { Date date = new Date(); JFrame frame = new JFrame(); frame.setTitle("Date Time Picker"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DateTimePicker dateTimePicker = new DateTimePicker(); dateTimePicker.setFormats( DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.MEDIUM ) ); dateTimePicker.setTimeFormat( DateFormat.getTimeInstance( DateFormat.MEDIUM ) ); dateTimePicker.setDate(date); frame.getContentPane().add(dateTimePicker); frame.pack(); frame.setVisible(true); } }
Jacky Luk
Ranch Hand
Posts: 634
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
private void updateTextFieldFormat() throws ParseException { if( timeSpinner == null ) return; JFormattedTextField tf = ((JSpinner.DefaultEditor) timeSpinner.getEditor()).getTextField(); DefaultFormatterFactory factory = (DefaultFormatterFactory) tf.getFormatterFactory(); MaskFormatter displayFormatter = new MaskFormatter("yyyy年MM月dd日"); factory.setDisplayFormatter(displayFormatter); factory.setEditFormatter(displayFormatter); DateFormatter formatter = (DateFormatter) factory.getDefaultFormatter(); // Change the date format to only show the hours //formatter.setFormat( timeFormat ); formatter.setFormat(timeFormat); }
Still doesn't work, the field is empty
Thanks
Jack
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
JSpinner dateeditor issues
Does the repaint() method run the paintComponent() method automatically?
Adding 'time' to a timestamp
completely lost in terms of trying to understand Collections.synchronizedList
creating a calander inside a ComboBox
More...