Show calendar to select date in java

In other languages, such as VB, C #, in cases where you want the user to enter a date, say, in a text field, we can make the calendar appear after clicking on it. Thus, the user can click on the corresponding date, and this date will be placed in the text box.

Thus, we can get rid of problems that can be caused due to dates in the wrong format. I need to know how we can achieve this in java?

Actually, I need to combine this with JTable. There is a column in which the date should be entered. But users can enter dates in various formats. So I thought about going into something like that. Hope there is a way to do this, easy.

Someone please show me how to do this. Any help is appreciated.

Thanks.

+6
source share
5 answers

I found JXDatePicker as the best solution for this. It gives what you need and is very easy to use.

import java.text.SimpleDateFormat; import java.util.Calendar; import javax.swing.JFrame; import javax.swing.JPanel; import org.jdesktop.swingx.JXDatePicker; public class DatePickerExample extends JPanel { public static void main(String[] args) { JFrame frame = new JFrame("JXPicker Example"); JPanel panel = new JPanel(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(400, 400, 250, 100); JXDatePicker picker = new JXDatePicker(); picker.setDate(Calendar.getInstance().getTime()); picker.setFormats(new SimpleDateFormat("dd.MM.yyyy")); panel.add(picker); frame.getContentPane().add(panel); frame.setVisible(true); } } 
+14
source

I wrote a DateTextField component.

 import java.awt.BorderLayout; import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Frame; import java.awt.GridLayout; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSpinner; import javax.swing.JTextField; import javax.swing.SpinnerNumberModel; import javax.swing.SwingConstants; import javax.swing.SwingUtilities; import javax.swing.border.LineBorder; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class DateTextField extends JTextField { private static String DEFAULT_DATE_FORMAT = "MM/dd/yyyy"; private static final int DIALOG_WIDTH = 200; private static final int DIALOG_HEIGHT = 200; private SimpleDateFormat dateFormat; private DatePanel datePanel = null; private JDialog dateDialog = null; public DateTextField() { this(new Date()); } public DateTextField(String dateFormatPattern, Date date) { this(date); DEFAULT_DATE_FORMAT = dateFormatPattern; } public DateTextField(Date date) { setDate(date); setEditable(false); setCursor(new Cursor(Cursor.HAND_CURSOR)); addListeners(); } private void addListeners() { addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent paramMouseEvent) { if (datePanel == null) { datePanel = new DatePanel(); } Point point = getLocationOnScreen(); point.y = point.y + 30; showDateDialog(datePanel, point); } }); } private void showDateDialog(DatePanel dateChooser, Point position) { Frame owner = (Frame) SwingUtilities .getWindowAncestor(DateTextField.this); if (dateDialog == null || dateDialog.getOwner() != owner) { dateDialog = createDateDialog(owner, dateChooser); } dateDialog.setLocation(getAppropriateLocation(owner, position)); dateDialog.setVisible(true); } private JDialog createDateDialog(Frame owner, JPanel contentPanel) { JDialog dialog = new JDialog(owner, "Date Selected", true); dialog.setUndecorated(true); dialog.getContentPane().add(contentPanel, BorderLayout.CENTER); dialog.pack(); dialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT); return dialog; } private Point getAppropriateLocation(Frame owner, Point position) { Point result = new Point(position); Point p = owner.getLocation(); int offsetX = (position.x + DIALOG_WIDTH) - (px + owner.getWidth()); int offsetY = (position.y + DIALOG_HEIGHT) - (py + owner.getHeight()); if (offsetX > 0) { result.x -= offsetX; } if (offsetY > 0) { result.y -= offsetY; } return result; } private SimpleDateFormat getDefaultDateFormat() { if (dateFormat == null) { dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT); } return dateFormat; } public void setText(Date date) { setDate(date); } public void setDate(Date date) { super.setText(getDefaultDateFormat().format(date)); } public Date getDate() { try { return getDefaultDateFormat().parse(getText()); } catch (ParseException e) { return new Date(); } } private class DatePanel extends JPanel implements ChangeListener { int startYear = 1980; int lastYear = 2050; Color backGroundColor = Color.gray; Color palletTableColor = Color.white; Color todayBackColor = Color.orange; Color weekFontColor = Color.blue; Color dateFontColor = Color.black; Color weekendFontColor = Color.red; Color controlLineColor = Color.pink; Color controlTextColor = Color.white; JSpinner yearSpin; JSpinner monthSpin; JButton[][] daysButton = new JButton[6][7]; DatePanel() { setLayout(new BorderLayout()); setBorder(new LineBorder(backGroundColor, 2)); setBackground(backGroundColor); JPanel topYearAndMonth = createYearAndMonthPanal(); add(topYearAndMonth, BorderLayout.NORTH); JPanel centerWeekAndDay = createWeekAndDayPanal(); add(centerWeekAndDay, BorderLayout.CENTER); reflushWeekAndDay(); } private JPanel createYearAndMonthPanal() { Calendar cal = getCalendar(); int currentYear = cal.get(Calendar.YEAR); int currentMonth = cal.get(Calendar.MONTH) + 1; JPanel panel = new JPanel(); panel.setLayout(new FlowLayout()); panel.setBackground(controlLineColor); yearSpin = new JSpinner(new SpinnerNumberModel(currentYear, startYear, lastYear, 1)); yearSpin.setPreferredSize(new Dimension(56, 20)); yearSpin.setName("Year"); yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, "####")); yearSpin.addChangeListener(this); panel.add(yearSpin); JLabel yearLabel = new JLabel("Year"); yearLabel.setForeground(controlTextColor); panel.add(yearLabel); monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1, 12, 1)); monthSpin.setPreferredSize(new Dimension(35, 20)); monthSpin.setName("Month"); monthSpin.addChangeListener(this); panel.add(monthSpin); JLabel monthLabel = new JLabel("Month"); monthLabel.setForeground(controlTextColor); panel.add(monthLabel); return panel; } private JPanel createWeekAndDayPanal() { String colname[] = { "S", "M", "T", "W", "T", "F", "S" }; JPanel panel = new JPanel(); panel.setFont(new Font("Arial", Font.PLAIN, 10)); panel.setLayout(new GridLayout(7, 7)); panel.setBackground(Color.white); for (int i = 0; i < 7; i++) { JLabel cell = new JLabel(colname[i]); cell.setHorizontalAlignment(JLabel.RIGHT); if (i == 0 || i == 6) { cell.setForeground(weekendFontColor); } else { cell.setForeground(weekFontColor); } panel.add(cell); } int actionCommandId = 0; for (int i = 0; i < 6; i++) for (int j = 0; j < 7; j++) { JButton numBtn = new JButton(); numBtn.setBorder(null); numBtn.setHorizontalAlignment(SwingConstants.RIGHT); numBtn.setActionCommand(String .valueOf(actionCommandId)); numBtn.setBackground(palletTableColor); numBtn.setForeground(dateFontColor); numBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { JButton source = (JButton) event.getSource(); if (source.getText().length() == 0) { return; } dayColorUpdate(true); source.setForeground(todayBackColor); int newDay = Integer.parseInt(source.getText()); Calendar cal = getCalendar(); cal.set(Calendar.DAY_OF_MONTH, newDay); setDate(cal.getTime()); dateDialog.setVisible(false); } }); if (j == 0 || j == 6) numBtn.setForeground(weekendFontColor); else numBtn.setForeground(dateFontColor); daysButton[i][j] = numBtn; panel.add(numBtn); actionCommandId++; } return panel; } private Calendar getCalendar() { Calendar calendar = Calendar.getInstance(); calendar.setTime(getDate()); return calendar; } private int getSelectedYear() { return ((Integer) yearSpin.getValue()).intValue(); } private int getSelectedMonth() { return ((Integer) monthSpin.getValue()).intValue(); } private void dayColorUpdate(boolean isOldDay) { Calendar cal = getCalendar(); int day = cal.get(Calendar.DAY_OF_MONTH); cal.set(Calendar.DAY_OF_MONTH, 1); int actionCommandId = day - 2 + cal.get(Calendar.DAY_OF_WEEK); int i = actionCommandId / 7; int j = actionCommandId % 7; if (isOldDay) { daysButton[i][j].setForeground(dateFontColor); } else { daysButton[i][j].setForeground(todayBackColor); } } private void reflushWeekAndDay() { Calendar cal = getCalendar(); cal.set(Calendar.DAY_OF_MONTH, 1); int maxDayNo = cal.getActualMaximum(Calendar.DAY_OF_MONTH); int dayNo = 2 - cal.get(Calendar.DAY_OF_WEEK); for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { String s = ""; if (dayNo >= 1 && dayNo <= maxDayNo) { s = String.valueOf(dayNo); } daysButton[i][j].setText(s); dayNo++; } } dayColorUpdate(false); } public void stateChanged(ChangeEvent e) { dayColorUpdate(true); JSpinner source = (JSpinner) e.getSource(); Calendar cal = getCalendar(); if (source.getName().equals("Year")) { cal.set(Calendar.YEAR, getSelectedYear()); } else { cal.set(Calendar.MONTH, getSelectedMonth() - 1); } setDate(cal.getTime()); reflushWeekAndDay(); } } } 
+9
source

The LGoodDatePicker library includes a DatePicker (swing) component that allows the user to select dates from the calendar. (By default, users can also enter dates using the keyboard, but you can turn off keyboard input if desired). DatePicker has automatic data verification, which means (by the way) that any date entered by the user will always be converted to the desired date format.

Fair disclosure: I am the main developer.

Since DatePicker is a swing component, you can add it to any other swing container, including (in your script) JTable cells.

The most commonly used date formats are automatically supported, and additional date formats can be added if necessary.

To provide the desired date format, you most likely want the selected format to be the standard โ€œdisplay formatโ€ for DatePicker. Formats can be specified using JavaT templates to publish a page .

The project homepage is on Github:
https://github.com/LGoodDatePicker/LGoodDatePicker .


DateTimePicker screenshot


DateTimePicker examples


Demo screenshot

+5
source

Another easy way to Netbeans is also available here. Inside Netbeans there are libraries that provide solutions for this type of situation. Also select the appropriate one. It is much simpler. After completing the above steps in the link, restart Netbeans.

 Step1:- Select Tools->Palette->Swing/AWT Components Step2:- Click 'Add from JAR'in Palette Manager Step3:- Browse to [NETBEANS HOME]\ide\modules\ext and select swingx-0.9.5.jar Step4:- This will bring up a list of all the components available for the palette. Lots of goodies here! Select JXDatePicker. Step5:- Select Swing Controls & click finish Step6:- Restart NetBeans IDE and see the magic :) 
+1
source
  • Open your Java source document and navigate to the JTable object that you created inside your Swing class.

  • Create a new TableModel object that contains a DatePickerTable. You must create a DatePickerTable with a range of date values โ€‹โ€‹in MMDDYYYY format. The first value is the start date, and the last is the end date. In code, it looks like this:

     TableModel datePicker = new DatePickerTable("01011999","12302000"); 
  • Set the display interval in the datePicker object. By default, it is displayed every day, but you can set a regular interval. To set a 15-day interval between date parameters, use this code:

     datePicker.interval = 15; 
  • Attach your table model to your JTable:

     JTable newtable = new JTable (datePicker); 

    Your Java application now has a dropdown list date dialog box.

-1
source

Source: https://habr.com/ru/post/921751/


All Articles