2013-04-19 35 views
5

私はSwingX JXDatePickerを使用しており、次/前年ボタンを提供する方法を理解できません(デフォルトでは、次/前月ボタンのみ提供しています)。Java Swing JXDatePicker

また、SwingXはもはや維持されていないと思われます。私は日付ピッカーとして最近のコンポーネントを使用していますか?

助けてください/ヒントは非常に高く評価されるでしょう。 おかげで、トーマス

UPDATE:質問を明確にするために

は、私がJXDatePickerのスクリーンショットを追加し、赤で次/前の月ボタンを強調しました。 質問はこうです:カレンダーを次/前年にもたらすボタンを追加する方法は? 新しいボタンは、特定のLook &フィールマネージャ(この場合はInsusbtantial)によってレンダリングする必要があるため、標準コンポーネントである必要があります。

感謝

enter image description here

+0

チェックアウトまでのリストについては、[この](http://stackoverflow.com/questions/1339354/what-are-good-java-date-chooser-swing-gui-widgets)質問最新の代替案。私はそれらのいくつかを試してみることをお勧めしたい – Kezz101

+0

それは_is_ maintain - 現在のバージョン1.6.5-1ですが、あなたが何をしているのか理解していません。正確に何をしたいですか?なぜ、swingxのデモ(プロジェクトページのwebstartableリンク)で説明されているカスタマイズが役に立たないのですか? – kleopatra

+0

@MrDなぜ関連性のないリンクを広げるのですか?それは完全に無関係の問題についてです... – kleopatra

答えて

2

私はMicroba日付ピッカーを使用することをお勧めします。それは高度にカスタマイズ可能で、あなたが望むものを正確に行います。

http://microba.sourceforge.net/

enter image description here enter image description here

編集:私は見

[OK]をクリックします。さて、私はいくつかの調査を行いました。あなたが探している機能はJXDatePickerに実際には存在しないようです。

私が見つけた別の選択肢は、JDatePicker at http://sourceforge.net/projects/jdatepicker/です。

メインウェブサイト:https://jdatepicker.org/

これはまだサポートして、必要な機能を持っているようです。

+0

残念ながら、このコンポーネントはもはや保守されていません(過去5年間はリリースされていません)。Java/Swingの将来のバージョンがそれを破る可能性があり、修正されない可能性があるのであまりにも危険です... – Tom

+0

@Omid can Swingコンポーネントにこれを追加し、jframeでドラッグする方法を教えてください。JPallete Managerを使って追加しましたが、このコンポーネントはjframeにドラッグしません。 –

+0

私はNet Beans 7.1を歌います、これはNeBeans 7.1とSwingフレームワークと互換性があります。 –

1

これは数年前にまとめたものです。ニーズに合わせて変更することができます。 calendar icon

enter image description here

あなたは、メインのテストを実行するには、このイメージが必要になります。 datepicker.gifという名前のソースと同じディレクトリに置くだけです。

import java.awt.Color; 
import java.awt.Container; 
import java.awt.Dimension; 
import java.awt.FlowLayout; 
import java.awt.Font; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import java.awt.Point; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.net.URL; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Calendar; 
import java.util.Date; 
import java.util.List; 

import javax.swing.BorderFactory; 
import javax.swing.BoxLayout; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.Popup; 
import javax.swing.PopupFactory; 

public class DatePicker extends JPanel { 

    private static final long serialVersionUID = 1L; 

    protected boolean controlsOnTop; 
    protected boolean removeOnDaySelection; 

    protected Calendar currentDisplayDate; 

    protected JButton prevMonth; 
    protected JButton nextMonth; 
    protected JButton prevYear; 
    protected JButton nextYear; 

    protected JTextField textField; 

    protected List<ActionListener> popupListeners = 
     new ArrayList<ActionListener>(); 

    protected Popup popup; 

    protected SimpleDateFormat dayName = new SimpleDateFormat("d"); 
    protected SimpleDateFormat monthName = new SimpleDateFormat("MMMM"); 

    protected String iconFile = "datepicker.gif"; 
    protected String[] weekdayNames = 
     {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; 

    public DatePicker() { 
     super(); 
     currentDisplayDate = Calendar.getInstance(); 
     controlsOnTop  = true; 
     removeOnDaySelection = true; 
     createPanel(); 
    } 

    public DatePicker(Calendar date) { 
     super(); 
     setDate(date); 
     controlsOnTop  = true; 
     removeOnDaySelection = true; 
     createPanel(); 
    } 

    public DatePicker(int month, int day, int year) { 
     super(); 
     setDate(month, day, year); 
     controlsOnTop  = true; 
     removeOnDaySelection = true; 
     createPanel(); 
    } 

    public void setDate(String date) { 
     currentDisplayDate = Calendar.getInstance(); 
     editDate(date); 
    } 

    public void setDate(Calendar date) { 
     currentDisplayDate = date; 
     createPanel(); 
     validate(); 
     repaint(); 
    } 

    public void setDate(int month, int day, int year) { 
     currentDisplayDate = Calendar.getInstance(); 
     currentDisplayDate.set(expandYear(year), month - 1, day); 
     createPanel(); 
     validate(); 
     repaint(); 
    } 

    protected int expandYear(int year) { 
     if (year < 100) {     // 2 digit year 
      int currentYear = Calendar.getInstance().get(Calendar.YEAR); 
      int current2DigitYear = currentYear % 100; 
      int currentCentury = currentYear/100 * 100; 
      // set 2 digit year range +20/-80 from current year 
      int high2DigitYear = (current2DigitYear + 20) % 100; 
      if (year <= high2DigitYear) { 
       year += currentCentury; 
      } 
      else { 
       year += (currentCentury - 100); 
      } 
     } 
     return year; 
    } 

    public void setControlsOnTop(boolean flag) { 
     controlsOnTop = flag; 
     createPanel(); 
     validate(); 
     repaint(); 
    } 

    public void setRemoveOnDaySelection(boolean flag) { 
     removeOnDaySelection = flag; 
    } 

    public Popup getPopup(Container c) { 
     if (popup == null) { 
      Point p = c.getLocation(); 
      PopupFactory factory = PopupFactory.getSharedInstance(); 
      popup = factory.getPopup(c, this, p.x, p.y); 
     } 
     return popup; 
    } 

    public void popupShow(Container c) { 
     getPopup(c); 
     popup.show(); 
    } 

    public void popupHide() { 
     popup.hide(); 
    } 

    public Calendar getCalendarDate() { 
     return currentDisplayDate; 
    } 

    public Date getDate() { 
     return currentDisplayDate.getTime(); 
    } 

    public String getFormattedDate() { 
     return Integer.toString(getMonth()) + "/" + 
      Integer.toString(getDay()) + "/" + 
      Integer.toString(getYear()); 
    } 

    public int getMonth() { 
     return currentDisplayDate.get(Calendar.MONTH) + 1; 
    } 

    public int getDay() { 
     return currentDisplayDate.get(Calendar.DAY_OF_MONTH); 
    } 

    public int getYear() { 
     return currentDisplayDate.get(Calendar.YEAR); 
    } 

    public ImageIcon getImage() { 
     return createImageIcon(iconFile, "Calendar date picker"); 
    } 

    /* 
    * Returns an ImageIcon, or null if the path was invalid. 
    */ 
    protected ImageIcon createImageIcon(String path, String description) { 
     URL imgURL = getClass().getResource(path); 
     String fileName = imgURL.getFile().replace("bin/", "src/"); 
     fileName = fileName.replace("%20", " ").substring(1); 
     ImageIcon icon = new ImageIcon(fileName, description); 
     return icon; 
    } 

    protected void createPanel() { 
     removeAll(); 
     setBorder(BorderFactory.createLineBorder(Color.black, 3)); 
     setFocusable(true); 
     setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 
     if (controlsOnTop) { 
      add(createControls()); 
      add(createCalendar()); 
     } else { 
      add(createCalendar()); 
      add(createControls()); 
     } 
     Dimension d = getPreferredSize(); 
     setPreferredSize(new Dimension(d.width, d.height + 8)); 
    } 

    protected JPanel createControls() {   
     JPanel c = new JPanel(); 
     c.setBorder(BorderFactory.createRaisedBevelBorder()); 
     c.setFocusable(true); 
     c.setLayout(new FlowLayout(FlowLayout.CENTER)); 

     prevYear = new JButton("<<"); 
     c.add(prevYear); 
     prevYear.setMargin(new Insets(0,0,0,0)); 
     prevYear.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       addYear(-1);   
      } 
     }); 

     prevMonth = new JButton("<"); 
     c.add(prevMonth); 
     prevMonth.setMargin(new Insets(0,0,0,0)); 
     prevMonth.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       addMonth(-1);  
      } 
     }); 


     textField = new JTextField(getFormattedDate(), 10); 
     c.add(textField); 
     textField.setEditable(true); 
     textField.setEnabled(true); 
     textField.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       editDate(textField.getText()); 
      } 
     }); 

     nextMonth = new JButton(">"); 
     c.add(nextMonth); 
     nextMonth.setMargin(new Insets(0,0,0,0)); 
     nextMonth.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       addMonth(+1); 
      } 
     }); 

     nextYear = new JButton(">>"); 
     c.add(nextYear); 
     nextYear.setMargin(new Insets(0,0,0,0)); 
     nextYear.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       addYear(+1);  
      } 
     }); 

     return c; 
    } 

    protected JPanel createCalendar() { 
     JPanel x = new JPanel(); 
     GridBagLayout gridbag = new GridBagLayout(); 
     GridBagConstraints c = new GridBagConstraints(); 

     x.setFocusable(true); 
     x.setLayout(gridbag); 

     String month = monthName.format(currentDisplayDate.getTime()); 
     String year = Integer.toString(getYear()); 

     c.gridx  = 0; 
     c.gridy  = 0; 
     c.gridwidth = 7; 
     c.gridheight = 1; 
     JLabel title = new JLabel(month + " " + year); 
     x.add(title, c); 
     Font font  = title.getFont(); 
//  Font titleFont = new Font(font.getName(), font.getStyle(), 
//    font.getSize() + 2); 
     Font weekFont = new Font(font.getName(), font.getStyle(), 
       font.getSize() - 2); 
     title.setFont(font); 

     c.gridy  = 1; 
     c.gridwidth = 1; 
     c.gridheight = 1; 
     for (c.gridx = 0; c.gridx < 7; c.gridx++) { 
      JLabel label = new JLabel(weekdayNames[c.gridx]); 
      x.add(label, c); 
      label.setFont(weekFont); 
     } 

     Calendar draw = (Calendar) currentDisplayDate.clone(); 
     draw.set(Calendar.DATE, 1); 
     draw.add(Calendar.DATE, -draw.get(Calendar.DAY_OF_WEEK) + 1); 
     int monthInt = currentDisplayDate.get(Calendar.MONTH); 
//  monthInt = 0; 
//  System.out.println("Current month: " + monthInt); 

     c.gridwidth = 1; 
     c.gridheight = 1; 
     int width = getFontMetrics(weekFont).stringWidth(" Wed "); 
     int width1 = getFontMetrics(weekFont).stringWidth("Wed"); 
     int height = getFontMetrics(weekFont).getHeight() + 
       (width - width1); 

     for (c.gridy = 2; c.gridy < 8; c.gridy++) { 
      for (c.gridx = 0; c.gridx < 7; c.gridx++) { 
       JButton dayButton; 
//    System.out.print("Draw month: " + draw.get(Calendar.MONTH)); 
       if (draw.get(Calendar.MONTH) == monthInt) { 
        String dayString = dayName.format(draw.getTime()); 
        if (draw.get(Calendar.DAY_OF_MONTH) < 10) 
         dayString = " " + dayString; 
        dayButton = new JButton(dayString); 
       } else { 
        dayButton = new JButton(); 
        dayButton.setEnabled(false); 
       } 
//    System.out.println(", day: " + dayName.format(draw.getTime())); 
       x.add(dayButton, c); 
       Color color = dayButton.getBackground(); 
       if ((draw.get(Calendar.DAY_OF_MONTH) == getDay()) && 
         (draw.get(Calendar.MONTH) == monthInt)) { 
        dayButton.setBackground(Color.yellow); 
//     dayButton.setFocusPainted(true); 
//     dayButton.setSelected(true); 
       } else 
        dayButton.setBackground(color); 
       dayButton.setFont(weekFont); 
       dayButton.setFocusable(true); 
       dayButton.setPreferredSize(new Dimension(width, height)); 
       dayButton.setMargin(new Insets(0,0,0,0)); 
       dayButton.addActionListener(new ActionListener() { 
        public void actionPerformed(ActionEvent e) { 
         changeDay(e.getActionCommand()); 
        } 

       }); 
       draw.add(Calendar.DATE, +1); 
      } 
//   if (draw.get(Calendar.MONTH) != monthInt) break; 
     } 
     return x; 
    } 

    public void addMonth(int month) { 
     currentDisplayDate.add(Calendar.MONTH, month); 
     createPanel(); 
     validate(); 
     repaint(); 
    } 

    public void addYear(int year) { 
     currentDisplayDate.add(Calendar.YEAR, year); 
     createPanel(); 
     validate(); 
     repaint(); 
    } 

    public void editDate(String date) { 
     parseDate(date); 
     createPanel(); 
     validate(); 
     repaint(); 
    } 

    protected void parseDate(String date) { 
     String[] parts = date.split("/"); 
     if (parts.length == 3) { 
      currentDisplayDate.set(Calendar.MONTH, 
        Integer.valueOf(parts[0]) - 1); 
      currentDisplayDate.set(Calendar.DAY_OF_MONTH, 
        Integer.valueOf(parts[1])); 
      currentDisplayDate.set(Calendar.YEAR, 
        expandYear(Integer.valueOf(parts[2]))); 
     } else if (parts.length == 2) { 
      currentDisplayDate = Calendar.getInstance(); 
      currentDisplayDate.set(Calendar.MONTH, 
        Integer.valueOf(parts[0]) - 1); 
      currentDisplayDate.set(Calendar.DAY_OF_MONTH, 
        Integer.valueOf(parts[1])); 
     } else { 
      // invalid date 
      currentDisplayDate = Calendar.getInstance(); 
     } 
    } 

    public void changeDay(String day) { 
     currentDisplayDate.set(Calendar.DAY_OF_MONTH, 
       Integer.valueOf(day.trim())); 
     if (removeOnDaySelection) { 
      firePopupEvent(new ActionEvent(this, 1, "hide")); 
      popup = null; 
     } else { 
      createPanel(); 
      validate(); 
      repaint(); 
     } 
    } 

    public void addPopupListener(ActionListener l) { 
     popupListeners.add(l); 
    } 

    public void removePopupListener(ActionListener l) { 
     popupListeners.remove(l); 
    } 

    public void firePopupEvent(ActionEvent e) { 
     for (int i = popupListeners.size() - 1; i >= 0; i--) { 
      ActionListener l = popupListeners.get(i); 
      l.actionPerformed(e); 
     } 
    } 

    public static void main(String[] args) { 
     final JFrame frame = new JFrame("Date Picker"); 
     Container pane = frame.getContentPane(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setPreferredSize(new Dimension(450, 250)); 
     pane.setLayout(new FlowLayout(FlowLayout.LEFT)); 
     pane.add(new JLabel("Birthdate: ")); 
     final JTextField testDate = new JTextField(10); 
     pane.add(testDate); 
     final DatePicker dp = new DatePicker(); 
     ImageIcon ii = dp.getImage(); 
//  System.out.println(ii.getIconWidth()); 
//  System.out.println(ii.getIconHeight()); 
     final JButton datePicker = new JButton(ii); 
     pane.add(datePicker); 
     datePicker.setPreferredSize(new Dimension(30, 24)); 
     datePicker.setMargin(new Insets(0,0,0,0)); 
     datePicker.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       dp.setDate(testDate.getText()); 
       dp.popupShow(datePicker); 
      } 
     }); 
     dp.addPopupListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       testDate.setText(dp.getFormattedDate()); 
       dp.popupHide(); 
      } 
     }); 
     frame.pack(); 
     frame.setFocusable(true); 
     frame.setResizable(true); 
     frame.setVisible(true); 
    } 
} 
5

私は、これは古い質問ですけど、私は最終的にSwingsLabデモについてクレオパトラさんのコメントから、ここで答えを見つけました。

(これはグローバルであり、アプリケーションごとの)SwingsLabデモごとの成分に基づいて、カスタムカレンダーヘッダを設定する方法の例を提供するが、これは私が実際に使用されるコードである:

UIManager.put(CalendarHeaderHandler.uiControllerID, SpinningCalendarHeaderHandler.class.getName()); 
datePicker = new JXDatePicker(); 
datePicker.getMonthView().setZoomable(true); //this is needed for custom header 

これは実験コード(まだ完全には公開されていない)のようですので、自己責任で使用してください。それが役に立てば幸い。

SwingsLab Demo

+0

nice、someone _really_デモ:-) – kleopatra

+0

@Radiace Wei Q Ong投稿していただきありがとうございますが、あなたのコードを使用すると、画像に表示される年が表示されません。私は同じ要件を持っています。 –

+0

@Syed Muhammad Mubashirそれは私のために働く。たぶんSwingsLabのデモでコードを試してみませんか?写真はデモのものです。 –

0

改善:

のDateTimePickerが離れすぎてボタンからです ので、ライン139で

public Popup getPopup(Container c) { 

    if (popup == null) { 

     Point p = c.getLocation(); 

     PopupFactory factory = PopupFactory.getSharedInstance(); 


// FramePrinc.Location.x ==> public static Point Location ; in class FramePrinc 

    popup = factory.getPopup(c, this, FramePrinc.Location.x,FramePrinc.Location.y); 

    } 

    return popup; 

} 

とアクション内であなたがの場所を取得しますコンポーネント

だから、

  • 別のクラスを作成します。たとえばFramePrinc
  • はパブリック静的ポイントの場所
  • を作成するには
  • パネルの内側にあなたdateTimePikerを追加するには、私はボタンアクション
  • 内言っラインを入れます

幸運

Grine_Amine改善:)

0

JXDatePickerの組み込み前/次年ボタンがないことが、私のソフトウェアにそのパッケージを使用することになった主な理由です。それ以外の場合は、そこにあるより良い日付ピッカーの1つです。

代わりに、私はLGoodDatePickerをお勧めします。それは、年に前後に行くためのボタンと、任意の年を選択するためのメニューがあります。公正な開示:私はこのプロジェクトの主な開発者です。それがあなたのニーズに合っているかどうか教えてください。

LGoodDatePickerホームページ:https://github.com/LGoodDatePicker/LGoodDatePicker

リリースセクションの作業のデモ(実行可能なjarファイル)、およびgitubページ上のコア機能のリストがあります。

スクリーンショットは以下のとおりです。

enter image description here