Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You should add Date Objects into <strong>JComboBox</strong> instead of the String for current Date and date for the next day and then use customized <strong>ListCellRenderer</strong> for rendering the Date in desired format.</p> <p><strong>Sample Code:</strong></p> <pre><code>import java.awt.Component; import java.text.SimpleDateFormat; import java.util.Date; import java.util.GregorianCalendar; import javax.swing.DefaultListCellRenderer; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; public class DateComboExample { // Create Date Renderer for formatting Date public static class DateComboBoxRenderer extends DefaultListCellRenderer { // desired format for the date private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus ) { Object item = value; // if the item to be rendered is date then format it if( item instanceof Date ) { item = dateFormat.format( ( Date ) item ); } return super.getListCellRendererComponent( list, item, index, isSelected, cellHasFocus); } } public static void main( String[] str ) { JComboBox combo = new JComboBox(); // Add current date GregorianCalendar calendar = new GregorianCalendar(); combo.addItem( calendar.getTime() ); // Add Next date calendar.roll( GregorianCalendar.DAY_OF_MONTH, 1 ); combo.addItem( calendar.getTime() ); // Set Renderer for formating the date in combobox combo.setRenderer( new DateComboBoxRenderer() ); JFrame frame = new JFrame( "Date Rendere Example" ); JPanel panel = new JPanel(); panel.add( new JLabel( "Date Combo: ") ); panel.add( combo ); frame.add( panel ); frame.pack(); frame.setVisible( true ); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload