代码之家  ›  专栏  ›  技术社区  ›  Venkat

创建java日期选择器组件?

  •  3
  • Venkat  · 技术社区  · 16 年前

    MM/dd/yyyy ,但我想用java创建日期选择器 dd/MM/yyyy . 那么我在哪里可以得到建议或者相关的解决方案呢。请介绍一下这方面的基本情况。 提前谢谢。。

    4 回复  |  直到 16 年前
        1
  •  0
  •   Ravindra Gullapalli    16 年前
        2
  •  3
  •   chris    16 年前

    JXDatePicker 你可以用 setFormats 更改日期格式。看见 this page this one

    如果您要从头开始编写自己的组件,那么您需要实现整个组件,这看起来有点浪费时间?

        3
  •  0
  •   Andrew M. Andrews III    16 年前

    Any+Time™ DatePicker/TimePicker AJAX Calendar Widget 允许您以任何格式指定日期。它还支持WAI/ARIA键盘和广泛的CSS定制选项,包括jqueryui支持。

        4
  •  0
  •   Bigger    14 年前

    可以通过切换SimpleDateFormat中使用的字符串的顺序来修改日期格式,但是不要忘记切换ranges数组。 代码如下:

    public class DateControl extends JPanel {
        @SuppressWarnings("unchecked")
        private JComboBox<String>[] combos = new JComboBox[3];
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
        final int ranges[][] = {{2012,2050},{1,12},{1,31}};
        public DateControl() {
            super();
            combos[0] =  new JComboBox<String>();
            combos[1] =  new JComboBox<String>();
            combos[2] =  new JComboBox<String>();
    
            // Fill the combos
            for (int i = 0; i<combos.length; i++)
                for (int j = ranges[i][0]; j<ranges[i][1]; j++) 
                    combos[i].addItem((j<9?"0":"")+Integer.toString(j));
    
            this.setLayout(new BoxLayout(this,BoxLayout.X_AXIS));
            for (JComboBox<String> c: combos) {
    
                // Remove the arrow button from the combo boxes (optional)
                c.setUI(new BasicComboBoxUI() {
                    protected JButton createArrowButton() {
                        return new JButton() {
                            public int getWidth() {
                                return 0;
                            }
                        };
                    }
                }); 
    
                this.add(c);
            }
    
            //This is just for a nice look touch (optional)
            this.setBorder(BorderFactory.createRaisedBevelBorder());
    
            // Set to today's date
            setDate(new Date());
        }
    
        // Date argument constructor
        public DateControl(Date date) {
            this();
            setDate(date);
        }
    
        public void setDate(Date d) {
            String[] date = df.format(d).split("/");
            for (int i=0;i<combos.length; i++)
                combos[i].setSelectedItem(date[i]);
        }
    
        public Date getDate() {
            String str = combos[0].getSelectedItem()+"/"+combos[1].getSelectedItem()+"/"+combos[2].getSelectedItem();
            Date ret = null;
            try {
                ret = df.parse(str);
            } catch (ParseException e) {e.printStackTrace();}
            return ret;
        }
    }
    

    class DateCellEditor extends AbstractCellEditor implements TableCellEditor {
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
        private DateControl dateControl;
    
        public DateCellEditor() {
            dateControl = new DateControl();
        }
    
        @Override
        public Component getTableCellEditorComponent(JTable table,
                Object value,
                boolean isSelected,
                int row,
                int column) {
    
            Date d = new Date();    
    
            try {
               Object str = table.getValueAt(row, column);
               if (str!=null)
                   d = df.parse((String)str);
            } catch (ParseException e) {
                e.printStackTrace();
            }
    
            dateControl.setDate(d);
            return dateControl;
        }
    
        @Override
        public Object getCellEditorValue() {
            return df.format(dateControl.getDate());
        }
    }
    

    新的单元格编辑器可以在表列中使用,如下所示:

            TableColumn c = myTable.getColumnModel().getColumn(0);
            c.setCellEditor(new DateCellEditor());