Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java swing date picker

The date picker lets the user select the Date through an easy interface that 
pops up with a Calendar. The user can navigate through the Calendar and select 
a date. It contains an editable date picker, a date field, a multiple month 
calendar and a month component. You can select any date from the Date Picker.


package swing_1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
class DatePicker {
    int month = java.util.Calendar.getInstance().get(java.util.Calendar.MONTH);
    int year = java.util.Calendar.getInstance().get(java.util.Calendar.YEAR);;
    JLabel l = new JLabel("", JLabel.CENTER);
    String day = "";
    JDialog d;
    JButton[] button = new JButton[49];
 
    public DatePicker(JFrame parent) {
        d = new JDialog();
        d.setModal(true);
        String[] header = { "Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat" };
        JPanel p1 = new JPanel(new GridLayout(7, 7));
        p1.setPreferredSize(new Dimension(430, 120));
 
        for (int x = 0; x <button> 6)
                button[x].addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent ae) {
                        day = button[selection].getActionCommand();
                        d.dispose();
                    }
                });
            if (x < 7) {
                button[x].setText(header[x]);
                button[x].setForeground(Color.red);
            }
            p1.add(button[x]);
        }
        JPanel p2 = new JPanel(new GridLayout(1, 3));
        JButton previous = new JButton("<>");
        next.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                month++;
                displayDate();
            }
        });
        p2.add(next);
        d.add(p1, BorderLayout.CENTER);
        d.add(p2, BorderLayout.SOUTH);
        d.pack();
        d.setLocationRelativeTo(parent);
        displayDate();
        d.setVisible(true);
    }
 
    public void displayDate() {
        for (int x = 7; x < button.length; x++)
            button[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++)
            button[x].setText("" + day);
        l.setText(sdf.format(cal.getTime()));
        d.setTitle("Date Picker");
    }
 
    public String setPickedDate() {
        if (day.equals(""))
            return day;
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
                "dd-MM-yyyy");
        java.util.Calendar cal = java.util.Calendar.getInstance();
        cal.set(year, month, Integer.parseInt(day));
        return sdf.format(cal.getTime());
    }
}
 
class DatePickerExample {
    public static void main(String[] args) {
        JLabel label = new JLabel("Selected Date:");
        final JTextField text = new JTextField(20);
        JButton b = new JButton("popup");
        JPanel p = new JPanel();
        p.add(label);
        p.add(text);
        p.add(b);
        final JFrame f = new JFrame();
        f.getContentPane().add(p);
        f.pack();
        f.setVisible(true);
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                text.setText(new DatePicker(f).setPickedDate());
            }
        });
    }
}
 
</button>
Comment

PREVIOUS NEXT
Code Example
Java :: android get id of view 
Java :: java get class by string name 
Java :: Java Access HashMap Elements 
Java :: Leap year or not program in java using if-else 
Java :: sololearn bowling game 
Java :: style jbuttons 
Java :: get image from resourcestream javafx 
Java :: round off and round up 
Java :: graph with dependies problem 
Java :: spring amqp exchange not found 
Sql :: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning. 
Sql :: mysql reset auto increment value 
Sql :: How to select rows with no matching entry in another table? 
Sql :: convert utc to est sql 
Sql :: apex execute batch job 
Sql :: SQL order random 
Sql :: view column data type sql 
Sql :: mysql list databases 
Sql :: neo4j display all nodes and relationships 
Sql :: how to remove tables from postgresql 
Sql :: postgres add not null to existing column 
Sql :: sqlite rename column 
Sql :: search stored procedures by name 
Sql :: postgres set sequence value to max id 
Sql :: crontab every month 
Sql :: mysql import gz 
Sql :: PLS-00225 type 
Sql :: postgresql select all column names 
Sql :: command line mysql import 
Sql :: grab all records from one table to another oracle sql 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =