Java Swing Date Chooser Example

Java Swing Date Picker

843804

Hi all,
can any one tell me the best date picker for my swing application, it should be very simple and work fast for my application

Comments

  • 794342

    here's a simple one, extremely doubtful it goes anywhere near 'best'

    import java.awt.*; import java.awt.event.*; import javax.swing.*; class DatePickerSimple extends JFrame {   JButton[] btn = new JButton[49];   int month = java.util.Calendar.getInstance().get(java.util.Calendar.MONTH);   int year = java.util.Calendar.getInstance().get(java.util.Calendar.YEAR);;   JLabel lbl = new JLabel("",JLabel.CENTER);   public DatePickerSimple()   {     buildGUI();     setDates();   }   public void buildGUI()   {     setLocation(300,100);     setDefaultCloseOperation(EXIT_ON_CLOSE);     String[] header = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};     JPanel midPanel = new JPanel(new GridLayout(7,7));     midPanel.setPreferredSize(new Dimension(400,400));     for(int x = 0; x < btn.length; x++)     {       final int selection = x;       btn[x] = new JButton();       btn[x].setFocusPainted(false);       if(x>6)btn[x].addActionListener(new ActionListener(){         public void actionPerformed(ActionEvent ae){           displayDatePicked(btn[selection].getActionCommand());}});       if(x < 7){btn[x].setFont(new Font("Lucida",Font.PLAIN,10)); btn[x].setText(header[x]);}       midPanel.add(btn[x]);     }     JPanel lowPanel = new JPanel(new GridLayout(1,3));     JButton prevBtn = new JButton("<< Previous");     prevBtn.addActionListener(new ActionListener(){       public void actionPerformed(ActionEvent ae){         month--;setDates();}});     lowPanel.add(prevBtn);     lowPanel.add(lbl);     JButton nextBtn = new JButton("Next >>");     nextBtn.addActionListener(new ActionListener(){       public void actionPerformed(ActionEvent ae){         month++;setDates();}});     lowPanel.add(nextBtn);     getContentPane().add(midPanel,BorderLayout.CENTER);     getContentPane().add(lowPanel,BorderLayout.SOUTH);     pack();   }   public void setDates()   {     for(int x = 7; x < btn.length; x++) btn[x].setText("");     java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("MMMM yyyy");     java.util.Calendar cal = java.util.Calendar.getInstance();     cal.set(year,month,1);     int dayOfWeek = cal.get(java.util.Calendar.DAY_OF_WEEK);     int daysInMonth = cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH);     for(int x = 6+dayOfWeek,day = 1; day <= daysInMonth; x++,day++) btn[x].setText(""+day);     lbl.setText(sdf.format(cal.getTime()));     setTitle("Date Picker  - "+lbl.getText());   }   public void displayDatePicked(String day)   {     if(day.equals("") == false)     {       java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("EEEE d MMMM, yyyy");       java.util.Calendar cal = java.util.Calendar.getInstance();       cal.set(year,month,Integer.parseInt(day));       JOptionPane.showMessageDialog(this,"You picked "+sdf.format(cal.getTime()));     }   }   public static void main(String[] args){new DatePickerSimple().setVisible(true);} }
  • 843804

    Hi all,
    can any one tell me the best date picker for my swing
    application, it should be very simple and work fast
    for my application

    It's kind of difficult to say what would work best for your application since we don't have a clue what your application is. But JCalendar is worth checking out. Search for it on google.

  • 843805

    Hi its simple and nice code...........
    thanks for tht.........
    ur class is going in very big ERP
    it required some modifications but overall nice
    thanks

  • 843805

    Hi its simple and nice code...........
    thanks for tht.........
    ur class is going in very big ERP
    it required some modifications but overall nice
    have u code for Login page in java swing ??
    thanks

  • 843806

    hello all;

    apparently im not getting this.. some help would be great.

    I put the code above in a new class within my project.

    I have one jpane that shows some text boxes, after the user clicks ok on that I want to pop up the date picker, then wait for the users choice, and return the date a s a string.

    I cannot figure out how to pull the selected value form the class.

    My program just pops the window up and keeps on running(without any dates).

    formatting is not an issue, im familar witht hat.

    how do I;
    a) make my class wait for the picker to finish
    b) retrieve the final selected value.
    (note I must do this twice, start date and end date.)

    basicaaly I think i want my main class of the picker to look like this...

                        public static String main(){ 	  DatePickerSimple dps =new DatePickerSimple(); 	  dps.setVisible(true); 	            -- wait somehow ?--  	  return finalDate; 	 		     	}

    my main class calls it as such

    DatePickerSimple dps = new DatePickerSimple(); 	sqlDetails.put("startDate", dps.main()); 			sqlDetails.put("endDate", dps.main());

    Thanks alot for any help.

    Also would Jcalendar be easier than using swing to do this?
    ::edit::

    Also feel free to tell me I'm a moron, if you are to suggest a much better way to do all of this :-)

    ::edit::

    Message was edited by:
    Masterkeedu

  • 794342

    here's one way to do what you want, but JCalendar from www.toedter.com
    is more or less the standard and would be the better option.

    I've left all the changes (commented out) and the added bits are set at the margin,
    so you can see the differences.

    run the program, click the '...' button/s

    import java.awt.*; import java.awt.event.*; import javax.swing.*; class DatePickerSimple //extends JFrame {   JButton[] btn = new JButton[49];   int month = java.util.Calendar.getInstance().get(java.util.Calendar.MONTH);   int year = java.util.Calendar.getInstance().get(java.util.Calendar.YEAR);;   JLabel lbl = new JLabel("",JLabel.CENTER); String day = "";//added JDialog d;   //public DatePickerSimple() public DatePickerSimple(JFrame parent)   {     //buildGUI();     //setDates();//moved   //}   //public void buildGUI()   //public void buildGUI(JFrame parent)   //{     //setLocation(300,100);     // setDefaultCloseOperation(EXIT_ON_CLOSE); d = new JDialog(); d.setModal(true);     String[] header = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};     JPanel midPanel = new JPanel(new GridLayout(7,7));     midPanel.setPreferredSize(new Dimension(400,400));     for(int x = 0; x < btn.length; x++)     {       final int selection = x;       btn[x] = new JButton();       btn[x].setFocusPainted(false);       if(x>6)btn[x].addActionListener(new ActionListener(){         public void actionPerformed(ActionEvent ae){           //displayDatePicked(btn[selection].getActionCommand());}}); day = btn[selection].getActionCommand(); d.dispose();}});       if(x < 7){btn[x].setFont(new Font("Lucida",Font.PLAIN,10)); btn[x].setText(header[x]);}       midPanel.add(btn[x]);     }     JPanel lowPanel = new JPanel(new GridLayout(1,3));     JButton prevBtn = new JButton("<< Previous");     prevBtn.addActionListener(new ActionListener(){       public void actionPerformed(ActionEvent ae){         month--;setDates();}});     lowPanel.add(prevBtn);     lowPanel.add(lbl);     JButton nextBtn = new JButton("Next >>");     nextBtn.addActionListener(new ActionListener(){       public void actionPerformed(ActionEvent ae){         month++;setDates();}});     lowPanel.add(nextBtn);     //getContentPane().add(midPanel,BorderLayout.CENTER); d.add(midPanel,BorderLayout.CENTER);     //getContentPane().add(lowPanel,BorderLayout.SOUTH); d.add(lowPanel,BorderLayout.SOUTH);     //pack(); d.pack(); d.setLocationRelativeTo(parent); setDates();//moved to here d.setVisible(true);    }   public void setDates()   {     for(int x = 7; x < btn.length; x++) btn[x].setText("");     java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("MMMM yyyy");     java.util.Calendar cal = java.util.Calendar.getInstance();     cal.set(year,month,1);     int dayOfWeek = cal.get(java.util.Calendar.DAY_OF_WEEK);     int daysInMonth = cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH);     for(int x = 6+dayOfWeek,day = 1; day <= daysInMonth; x++,day++) btn[x].setText(""+day);     lbl.setText(sdf.format(cal.getTime()));     //setTitle("Date Picker  - "+lbl.getText()); d.setTitle("Date Picker  - "+lbl.getText());   }   //public void displayDatePicked(String day) public String displayDatePicked()   {     //if(day.equals("") == false) if(day.equals("")) return day;     //{       java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("EEEE d MMMM, yyyy");       java.util.Calendar cal = java.util.Calendar.getInstance();       cal.set(year,month,Integer.parseInt(day));       //JOptionPane.showMessageDialog(this,"You picked "+sdf.format(cal.getTime())); return sdf.format(cal.getTime());     //}   }   //public static void main(String[] args){new DatePickerSimple().setVisible(true);} } class Testing {   public void buildGUI()   {     JLabel startLbl = new JLabel("Start Date:");     JLabel endLbl = new JLabel("End Date:");     final JLabel startDate = new JLabel("                                                                ",JLabel.CENTER);     final JLabel endDate = new JLabel("",JLabel.CENTER);     JButton startBtn = new JButton("...");     JButton endBtn = new JButton("...");     startBtn.setMargin(new Insets(0,0,0,0));     endBtn.setMargin(new Insets(0,0,0,0));     JPanel leftPanel = new JPanel(new GridLayout(2,1));     JPanel midPanel = new JPanel(new GridLayout(2,1));     JPanel rightPanel = new JPanel(new GridLayout(2,1));     leftPanel.add(startLbl);     leftPanel.add(endLbl);     midPanel.add(startDate);     midPanel.add(endDate);     rightPanel.add(startBtn);     rightPanel.add(endBtn);     final JFrame f = new JFrame();     f.getContentPane().add(leftPanel,BorderLayout.WEST);     f.getContentPane().add(midPanel,BorderLayout.CENTER);     f.getContentPane().add(rightPanel,BorderLayout.EAST);     f.pack();     f.setLocationRelativeTo(null);     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     f.setVisible(true);     startBtn.addActionListener(new ActionListener(){       public void actionPerformed(ActionEvent ae){         startDate.setText(new DatePickerSimple(f).displayDatePicked());       }     });     endBtn.addActionListener(new ActionListener(){       public void actionPerformed(ActionEvent ae){         endDate.setText(new DatePickerSimple(f).displayDatePicked());       }     });   }   public static void main(String[] args)   {     SwingUtilities.invokeLater(new Runnable(){       public void run(){         new Testing().buildGUI();       }     });   } }
  • Puce

    I think there is such a component at SwingX/ SwingLabs:
    https://swingx.dev.java.net/
    http://swinglabs.org/

    And don't forget to vote for this issue:
    http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4730041
    ;-)

    -Puce

  • 843806

    Thanks Michael, jCalendar was just what I needed, and what a breeze to work with.

    >

    And don't forget to vote for this issue:
    http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=473
    0041
    ;-)

    -Puce

    Puce what are you refering to? I did not know we were discussing an issue.

  • Puce

    >>

    And don't forget to vote for this issue:

    http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4730041

    Puce what are you refering to? I did not know we were
    discussing an issue.

    You were looking for a date picker since this is still not a part of the standard jdk. If you think, as I do, that such a component is a standard component and should therefore be a part of the standard jdk, so people don't have to search long for such a component or - even worse - try to use a text field and parse/ validate that String themselves, then please vote for that issue. The more votes the more likely it will become a standard component one day!
    (Note: eg. .NET provides such a component in the standard .NET library!)

    -Puce

  • 843806

    Well I can appreciate your motivation, I don't know If I agree that should be a priority.

    JCalendar is out there, and works great with very little setup. Time and energy could be better spent elsewhere. I don't think that is a 'bug' per say. But thats just my opinion.

This discussion has been closed.

gudinoyouttle.blogspot.com

Source: https://community.oracle.com/tech/developers/discussion/1353334/java-swing-date-picker

0 Response to "Java Swing Date Chooser Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel