summaryrefslogtreecommitdiff
path: root/libs/FLib/JCalendar/doc/example/Example1.java
blob: c64e64c3d4a97b6ea0cd4cbb416a0fb426c70f6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//**********************************************************************
// Package
//**********************************************************************

package doc.example;

//**********************************************************************
// Import list
//**********************************************************************

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.Calendar;
import java.util.Locale;
import org.freixas.jcalendar.*;

/**
 * This example shows various instances of the JCalendar class.
 * <hr>
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the Artistic License. You should have
 * received a copy of the Artistic License along with this program. If
 * not, a copy is available at
 * <a href="http://opensource.org/licenses/artistic-license.php">
 * opensource.org</a>.
 *
 * @author Antonio Freixas
 */

// Copyright © 2004 Antonio Freixas
// All Rights Reserved.

public class Example1
        extends JFrame
{

//**********************************************************************
// main
//**********************************************************************

    public static void
    main(
        String[] args)
    {
        new Example1();
    }

//**********************************************************************
// Constructors
//**********************************************************************

    /**
     * Create various instances of a JCalendar.
     */

    public
    Example1()
    {
        // Set up the frame

        setTitle("Example1");
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        Container contentPane = getContentPane();
        contentPane.setLayout(new GridLayout(2, 2, 5, 5));

        // Create a border for all calendars

        Border etchedBorder =
            BorderFactory.createEtchedBorder();
        Border emptyBorder =
            BorderFactory.createEmptyBorder(10, 10, 10, 10);
        Border compoundBorder =
            BorderFactory.createCompoundBorder(etchedBorder, emptyBorder);

        // Create a date listener to be used for all calendars

        MyDateListener listener = new MyDateListener();

        // Display date and time using the default calendar and locale.
        // Display today's date at the bottom.

        JCalendar calendar1 =
            new JCalendar(
            JCalendar.DISPLAY_DATE | JCalendar.DISPLAY_TIME,
            true);
        calendar1.addDateListener(listener);
        calendar1.setBorder(compoundBorder);

        // Set fonts rather than using defaults

        calendar1.setTitleFont(new Font("Serif", Font.BOLD|Font.ITALIC, 24));
        calendar1.setDayOfWeekFont(new Font("SansSerif", Font.ITALIC, 12));
        calendar1.setDayFont(new Font("SansSerif", Font.BOLD, 16));
        calendar1.setTimeFont(new Font("DialogInput", Font.PLAIN, 10));
        calendar1.setTodayFont(new Font("Dialog", Font.PLAIN, 14));

        // Display date only

        JCalendar calendar2 = new JCalendar(JCalendar.DISPLAY_DATE, false);
        calendar2.addDateListener(listener);
        calendar2.setBorder(compoundBorder);

        // Display time only and set the time pattern to use as a duration
        // from 00:00 to 23:59

        JCalendar calendar3 =
            new JCalendar(
            Calendar.getInstance(),
            Locale.getDefault(),
            JCalendar.DISPLAY_TIME,
            false,
            "HH:mm");
        calendar3.addDateListener(listener);
        calendar3.setBorder(compoundBorder);

        // Display a French calendar

        JCalendar calendar4 =
            new JCalendar(
            Calendar.getInstance(Locale.FRENCH),
            Locale.FRENCH,
            JCalendar.DISPLAY_DATE | JCalendar.DISPLAY_TIME,
            false);
        calendar4.addDateListener(listener);
        calendar4.setBorder(compoundBorder);

        // Add all the calendars to the content pane

        JPanel panel1 = new JPanel(new FlowLayout());
        panel1.add(calendar1);
        contentPane.add(panel1);

        JPanel panel2 = new JPanel(new FlowLayout());
        panel2.add(calendar2);
        contentPane.add(panel2);

        JPanel panel3 = new JPanel(new FlowLayout());
        panel3.add(calendar3);
        contentPane.add(panel3);

        JPanel panel4 = new JPanel(new FlowLayout());
        panel4.add(calendar4);
        contentPane.add(panel4);

        // Make the window visible

        pack();
        setVisible(true);
    }

//**********************************************************************
// Inner Classes
//**********************************************************************

    private class MyDateListener
            implements DateListener
    {

        public void
        dateChanged(
            DateEvent e)
        {
            Calendar c = e.getSelectedDate();
            if (c != null) {
                System.out.println(c.getTime());
            }
            else {
                System.out.println("No time selected.");
            }
        }

    }

//**********************************************************************
// End Inner Classes
//**********************************************************************

}