【问题标题】:Create a java Date Picker component?创建一个 java Date Picker 组件?
【发布时间】:2011-02-23 03:55:19
【问题描述】:

我有MM/dd/yyyy 格式的日期选择器组件,但我想在java 中创建dd/MM/yyyy 形式的日期选择器。那么我在哪里可以获得建议或相关解决方案。请给出基本的想法。 在此先感谢..

【问题讨论】:

  • 哪个日期选择器组件?是不是有一个配置选项可以传递给它以显示日期格式?
  • 我有 JXDatePicker。但我想在我的应用程序中为选择日历日期配置自己的日期选择器。

标签: java datepicker


【解决方案1】:

JXDatePicker 中,您可以使用setFormats 更改日期格式。见this pagethis one

如果您是在谈论从头开始编写自己的组件,那么您需要实现整个事情,这似乎有点浪费时间?

【讨论】:

    【解决方案2】:

    试试 JCalendar

    您可以从http://www.toedter.com/en/jcalendar/index.html下载它

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 抱歉,我以为问题是 JavaScript,而不是 Java。如果您正在寻找可以在网络上使用的东西,那么这可能仍然是一个可行的选择。但是,如果您正在查看一个独立的 Java 应用程序,那么我的回答就太离谱了。很抱歉有任何混淆。
      【解决方案4】:

      我想分享一下我刚刚为我的项目写的东西,我必须为一个可以就地编辑的 JTable 制作一个简单快捷的日期编辑器,它也可以用作普通控件。 如您所见,可以修改日期格式,切换 SimpleDateFormat 中使用的字符串的顺序,但不要忘记切换范围数组。 代码如下:

      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());
      

      我必须提醒你这是一个单元格“编辑器”,它只会在单元格处于编辑状态时显示。日期值将由普通单元格“渲染器”显示为格式为 yyyy/MM/dd 的简单字符串。

      我希望这一切对某人有所帮助:)

      【讨论】:

        猜你喜欢
        • 2019-07-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多