trance12
asked on
swing calendar
hi,
i have a date field..what i need it to do is when a button next to the field i clicked, it opens up a calendar,the user can then choose a date and on selection the input date field gets that date value...
i have a date field..what i need it to do is when a button next to the field i clicked, it opens up a calendar,the user can then choose a date and on selection the input date field gets that date value...
http://www.toedter.com/en/jcalendar/
ASKER
second link doesnt work??
it does look like they are down, try again later should come back up
Hi, trance12. Below is code modified from that posted by GrandStrumpf some time ago. Basically my BlackBerry class has a button "Select Date" by a JTextfield; when users click the button the calendar pops up, they click a date, and the value is sent back to the BlackBerry JTextfield. I use a Property Change Listener to do this.
Code for the Calendar class (with its own helper classes):
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.*;
public class TemplateCal extends JFrame implements MouseListener
{
JScrollPane scroll;
JPanel p=new JPanel(new BorderLayout());
// Variables
//------------------------ ---------- ----------
private GregorianCalendar todayCalendar = new GregorianCalendar();
private int currentYear = todayCalendar.get(Calendar .YEAR);
private int currentMonth = todayCalendar.get(Calendar .MONTH);
private int currentMonthStartWeekday;
private int thisDay;
// GUI fields
//------------------------ ---------- ----------
// Sub-JPanels
private JPanel yearJPanel = new JPanel();
private JPanel monthJPanel = new JPanel();
private JPanel dayJPanel = new JPanel();
private JPanel bottomJPanel = new JPanel();
// Year
private JLabel yearJLabel = new JLabel("8888", JLabel.CENTER);
private JButton yearMinusJButton = new JButton("<");
private JButton yearPlusJButton = new JButton(">");
// Month
private JButton[] monthJButtons = new JButton[12];
// Day
private JLabel[] weekdayJLabels = new JLabel[7];
private JButton[] dayJButtons = new JButton[42];
// Bottom
private JButton cancelJButton = new JButton("Cancel");
private JButton todayJButton = new JButton("Today");
//private JButton todayJButton;
// Action Listeners
//------------------------ ---------- ----------
private ActionListener yearListener = new YearListener();
private ActionListener monthListener = new MonthListener();
private ActionListener dayListener = new DayListener();
private ActionListener JButtonListener = new JButtonListener();
private Color metalCopy;
private Color keepColor;
private Color keepForeground;
private Font keepFont;
private Font bigFont = new Font("Arial", 1, 12);
// Update the chooser with the new View
//------------------------ ---------- ----------
private void updateView() {
getRootPane().setDefaultBu tton(today JButton);
Color origColor = cancelJButton.getBackgroun d();
metalCopy = new Color (223,245,247);
// Year
yearJLabel.setText("" + currentYear);
// Months
for (int cnt = 0; cnt < monthJButtons.length; cnt++)
{
monthJButtons[cnt].addMous eListener( this);
if (cnt == currentMonth)
{
//monthJButtons[cnt].setBa ckground(C olor.CYAN) ;
monthJButtons[cnt].setFore ground(Col or.blue);
monthJButtons[cnt].setBord erPainted( false);
monthJButtons[cnt].setBack ground(met alCopy);
monthJButtons[cnt].setFont (new java.awt.Font("Arial", 1, 12));
} else
{
monthJButtons[cnt].setBack ground(ori gColor);
monthJButtons[cnt].setFore ground(Col or.black);
monthJButtons[cnt].setBord erPainted( true);
}
}
// Days
GregorianCalendar tempCal = new GregorianCalendar(currentY ear, currentMonth, 1);
currentMonthStartWeekday = tempCal.get(Calendar.DAY_O F_WEEK);
for (int cnt = 0; cnt < dayJButtons.length; cnt++)
{
dayJButtons[cnt].addMouseL istener(th is);
// Disable JButtons preceding start of month
if (cnt < currentMonthStartWeekday - 1)
{
dayJButtons[cnt].setBackgr ound(origC olor );
dayJButtons[cnt].setText(" ");
dayJButtons[cnt].setEnable d(false);
continue;
}
// Populate dates on JButtons
if (tempCal.get(Calendar.MONT H) == currentMonth)
{
dayJButtons[cnt].setEnable d(true);
dayJButtons[cnt].setText(" " + tempCal.get(Calendar.DATE) );
if (tempCal.get(Calendar.DATE ) == todayCalendar.get(Calendar .DATE)
&& tempCal.get(Calendar.MONTH ) == todayCalendar.get(Calendar .MONTH)
&& tempCal.get(Calendar.YEAR) == todayCalendar.get(Calendar .YEAR))
{
dayJButtons[cnt].setForegr ound(Color .blue);
dayJButtons[cnt].setBackgr ound(metal Copy);
dayJButtons[cnt].setFont(n ew java.awt.Font("Arial", 1, 12));
dayJButtons[cnt].setBorder Painted(fa lse);
//dayJButtons[cnt].setBack ground(new javax.swing.plaf.ColorUIRe source(0,0 ,0));
//dayJButtons[cnt].setCont entAreaFil led(false) ; //hides the button shape, so you can use image
dayJButtons[cnt].setText(" " + tempCal.get(Calendar.DATE) );
thisDay = tempCal.get(Calendar.DATE) ;
}
else
{
dayJButtons[cnt].setBackgr ound(origC olor );
dayJButtons[cnt].setForegr ound(Color .black );
}
tempCal.add(Calendar.DATE, 1);
continue;
}
// Disable JButtons following end of month
dayJButtons[cnt].setBackgr ound(origC olor );
dayJButtons[cnt].setText(" ");
dayJButtons[cnt].setEnable d(false);
}
}
// Constructor
//------------------------ ---------- ----------
public TemplateCal()
{
getRootPane().setDefaultBu tton(today JButton);
setDefaultCloseOperation(E XIT_ON_CLO SE);
// Building JPanel for years
//++++++++++++++++++++++++ ++++++++++ ++++++++++ +
yearJPanel.setLayout(new java.awt.FlowLayout());
yearJPanel.add(yearMinusJB utton);
yearMinusJButton.setFocusP ainted(fal se);
yearMinusJButton.setDefaul tCapable(f alse);
yearJPanel.add(yearJLabel) ;
yearJPanel.add(yearPlusJBu tton);
yearMinusJButton.addAction Listener(y earListene r);
yearMinusJButton.addMouseL istener(th is);
yearPlusJButton.addActionL istener(ye arListener );
yearPlusJButton.addMouseLi stener(thi s);
// Building JPanel for months
//++++++++++++++++++++++++ ++++++++++ ++++++++++ +
monthJPanel.setLayout(new java.awt.GridBagLayout());
GridBagConstraints monthConstraints = new GridBagConstraints();
monthConstraints.gridx=0;
monthConstraints.fill=Grid BagConstra ints.HORIZ ONTAL;
monthConstraints.weightx=0 ;
monthConstraints.weighty=0 ;
monthJButtons[0] = new JButton("Jan");
monthJButtons[1] = new JButton("Feb");
monthJButtons[2] = new JButton("Mar");
monthJButtons[3] = new JButton("Apr");
monthJButtons[4] = new JButton("May");
monthJButtons[5] = new JButton("Jun");
monthJButtons[6] = new JButton("Jul");
monthJButtons[7] = new JButton("Aug");
monthJButtons[8] = new JButton("Sep");
monthJButtons[9] = new JButton("Oct");
monthJButtons[10] = new JButton("Nov");
monthJButtons[11] = new JButton("Dec");
for(int month = 0; month < 12; month++)
{
if(month <= 5)
{
monthConstraints.gridy = 0;
monthConstraints.gridx = month + 1;
monthJButtons[month].setPr eferredSiz e(new Dimension(57, 25));
monthJButtons[month].setMa ximumSize( new java.awt.Dimension(57, 25));
monthJButtons[month].setMi nimumSize( new java.awt.Dimension(57, 25));
monthJPanel.add(monthJButt ons[month] ,monthCons traints);
}
else
{
monthConstraints.gridy = 1;
monthConstraints.gridx = (month-6) + 1;
monthJButtons[month].setPr eferredSiz e(new Dimension(57, 25));
monthJButtons[month].setMa ximumSize( new java.awt.Dimension(57, 25));
monthJButtons[month].setMi nimumSize( new java.awt.Dimension(57, 25));
monthJPanel.add(monthJButt ons[month] ,monthCons traints);
}
monthJButtons[month].addAc tionListen er(monthLi stener);
}
// Building JPanel for days
//++++++++++++++++++++++++ ++++++++++ ++++++++++ +
dayJPanel.setLayout(new java.awt.GridLayout(0,7));
weekdayJLabels[0] = new JLabel("S", JLabel.CENTER);
weekdayJLabels[1] = new JLabel("M", JLabel.CENTER);
weekdayJLabels[2] = new JLabel("T", JLabel.CENTER);
weekdayJLabels[3] = new JLabel("W", JLabel.CENTER);
weekdayJLabels[4] = new JLabel("T", JLabel.CENTER);
weekdayJLabels[5] = new JLabel("F", JLabel.CENTER);
weekdayJLabels[6] = new JLabel("S", JLabel.CENTER);
for(int day = 0; day < 7; day++) {
dayJPanel.add(weekdayJLabe ls[day]);
}
// JButtons for days
for(int cnt = 0; cnt < dayJButtons.length; cnt++) {
//dayJButtons[cnt] = new JButton("" + cnt);
dayJButtons[cnt] = new JButton("" + cnt);
dayJPanel.add(dayJButtons[ cnt]);
dayJButtons[cnt].addAction Listener(d ayListener );
}
// Building JPanel for bottom JButtons
//++++++++++++++++++++++++ ++++++++++ ++++++++++ +
bottomJPanel.setLayout(new java.awt.BorderLayout());
{
JPanel alignJPanel = new JPanel();
alignJPanel.setLayout(new java.awt.GridLayout(1,0));
alignJPanel.add(cancelJBut ton);
cancelJButton.addMouseList ener(this) ;
alignJPanel.add(todayJButt on);
todayJButton.addMouseListe ner(this);
todayJButton.setFocusPaint ed(true);
bottomJPanel.add(alignJPan el, java.awt.BorderLayout.CENT ER);
cancelJButton.addActionLis tener(JBut tonListene r);
todayJButton.addActionList ener(JButt onListener );
}
this.getContentPane().setL ayout(new GridBagLayout());
GridBagConstraints myConstraints = new GridBagConstraints();
myConstraints.gridx = 0;
myConstraints.fill = GridBagConstraints.NONE;
myConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
myConstraints.gridy = 0;
this.getContentPane().add( yearJPanel , myConstraints);
myConstraints.gridy = 1;
this.getContentPane().add( monthJPane l, myConstraints);
myConstraints.gridy = 2;
this.getContentPane().add( dayJPanel, myConstraints);
myConstraints.gridy = 3;
this.getContentPane().add( bottomJPan el, myConstraints);
this.setSize(350,375);
this.setDefaultCloseOperat ion(DISPOS E_ON_CLOSE );
this.setTitle("Calendar");
this.setLocation(550, 180);
// Call the update view method
//++++++++++++++++++++++++ ++++++++++ ++++++++++ +
this.updateView();
}
// Main Method
//------------------------ ---------- ----------
public static void main(final String[] args) {
new TemplateCal().setVisible(t rue);
}
public void paintBorder(MouseEvent e, boolean borderPainted, Color buttonColor, Color buttonTextColor, Font textFont)
{
Object source = e.getSource();
if (source instanceof JButton)
{
Color initColor = ((JButton)source).getBackg round();
Color initForeground = ((JButton)source).getForeg round();
Font initFont = ((JButton)source).getFont( );
if (!(initColor == Color.gray))
{
keepColor = initColor;
keepForeground = initForeground;
keepFont = initFont;
}
((JButton)source).setBackg round(butt onColor);
((JButton)source).setBorde rPainted(b orderPaint ed);
((JButton)source).setForeg round(butt onTextColo r);
((JButton)source).setFont( textFont);
}
}
public void mouseEntered(MouseEvent e)
{
paintBorder(e, false, Color.gray, Color.yellow, bigFont);
}
public void mouseExited(MouseEvent e)
{
paintBorder(e, true, keepColor ,keepForeground,keepFont);
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e)
{
}
class YearListener implements ActionListener
{
public void actionPerformed(ActionEven t e)
{
Object buttonPressed = e.getSource();
if (buttonPressed == yearMinusJButton) {
currentYear--;
}
else if (buttonPressed == yearPlusJButton) {
currentYear++;
}
updateView();
}
}
class MonthListener implements ActionListener
{
public void actionPerformed(ActionEven t e)
{
Object buttonPressed = e.getSource();
for (int cnt = 0; cnt < monthJButtons.length; cnt++) {
if (buttonPressed == monthJButtons[cnt]) {
currentMonth = cnt;
break;
}
}
updateView();
}
}
class DayListener implements ActionListener
{
public void actionPerformed(ActionEven t e)
{
Object buttonPressed = e.getSource();
for (int cnt = 0; cnt < dayJButtons.length; cnt++) {
if (buttonPressed == dayJButtons[cnt]) {
String datePicked = "" + (currentMonth+1) + "/" + ((cnt+1) - (currentMonthStartWeekday- 1)) + "/" + currentYear;
//System.out.println(messa ge);
String dateChosen = datePicked;
//BlackBerry.displayMessag e(nameOfCo urse);
firePropertyChange("select edDate",nu ll,dateCho sen);
break;
}
}
}
}
class JButtonListener implements ActionListener
{
public void actionPerformed(ActionEven t e)
{
Object buttonPressed = e.getSource();
if (buttonPressed == todayJButton)
{
//the line below would allow someone to select some other month and year
//and press the Today JButton and get incorrect results, e.g. 12/30/2021
//String datePicked = (currentMonth+1) + "/" + thisDay + "/" + currentYear;
String datePicked = (todayCalendar.get(Calenda r.MONTH)+1 ) + "/" + thisDay + "/" + todayCalendar.get(Calendar .YEAR);
String dateChosen = datePicked;
firePropertyChange("select edDate",nu ll,dateCho sen);
}
else if (buttonPressed == cancelJButton)
{
String dateChosen = "";
firePropertyChange("cancel Date",null ,dateChose n);
}
}
}
}
class MyPanel extends JPanel
{
int paintX=0;
int paintY=0;
public MyPanel()
{
super();
setOpaque(true);
}
public void paint(Graphics g)
{
super.paint(g);
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
}
}
Code for the Calendar class (with its own helper classes):
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.*;
public class TemplateCal extends JFrame implements MouseListener
{
JScrollPane scroll;
JPanel p=new JPanel(new BorderLayout());
// Variables
//------------------------
private GregorianCalendar todayCalendar = new GregorianCalendar();
private int currentYear = todayCalendar.get(Calendar
private int currentMonth = todayCalendar.get(Calendar
private int currentMonthStartWeekday;
private int thisDay;
// GUI fields
//------------------------
// Sub-JPanels
private JPanel yearJPanel = new JPanel();
private JPanel monthJPanel = new JPanel();
private JPanel dayJPanel = new JPanel();
private JPanel bottomJPanel = new JPanel();
// Year
private JLabel yearJLabel = new JLabel("8888", JLabel.CENTER);
private JButton yearMinusJButton = new JButton("<");
private JButton yearPlusJButton = new JButton(">");
// Month
private JButton[] monthJButtons = new JButton[12];
// Day
private JLabel[] weekdayJLabels = new JLabel[7];
private JButton[] dayJButtons = new JButton[42];
// Bottom
private JButton cancelJButton = new JButton("Cancel");
private JButton todayJButton = new JButton("Today");
//private JButton todayJButton;
// Action Listeners
//------------------------
private ActionListener yearListener = new YearListener();
private ActionListener monthListener = new MonthListener();
private ActionListener dayListener = new DayListener();
private ActionListener JButtonListener = new JButtonListener();
private Color metalCopy;
private Color keepColor;
private Color keepForeground;
private Font keepFont;
private Font bigFont = new Font("Arial", 1, 12);
// Update the chooser with the new View
//------------------------
private void updateView() {
getRootPane().setDefaultBu
Color origColor = cancelJButton.getBackgroun
metalCopy = new Color (223,245,247);
// Year
yearJLabel.setText("" + currentYear);
// Months
for (int cnt = 0; cnt < monthJButtons.length; cnt++)
{
monthJButtons[cnt].addMous
if (cnt == currentMonth)
{
//monthJButtons[cnt].setBa
monthJButtons[cnt].setFore
monthJButtons[cnt].setBord
monthJButtons[cnt].setBack
monthJButtons[cnt].setFont
} else
{
monthJButtons[cnt].setBack
monthJButtons[cnt].setFore
monthJButtons[cnt].setBord
}
}
// Days
GregorianCalendar tempCal = new GregorianCalendar(currentY
currentMonthStartWeekday = tempCal.get(Calendar.DAY_O
for (int cnt = 0; cnt < dayJButtons.length; cnt++)
{
dayJButtons[cnt].addMouseL
// Disable JButtons preceding start of month
if (cnt < currentMonthStartWeekday - 1)
{
dayJButtons[cnt].setBackgr
dayJButtons[cnt].setText("
dayJButtons[cnt].setEnable
continue;
}
// Populate dates on JButtons
if (tempCal.get(Calendar.MONT
{
dayJButtons[cnt].setEnable
dayJButtons[cnt].setText("
if (tempCal.get(Calendar.DATE
&& tempCal.get(Calendar.MONTH
&& tempCal.get(Calendar.YEAR)
{
dayJButtons[cnt].setForegr
dayJButtons[cnt].setBackgr
dayJButtons[cnt].setFont(n
dayJButtons[cnt].setBorder
//dayJButtons[cnt].setBack
//dayJButtons[cnt].setCont
dayJButtons[cnt].setText("
thisDay = tempCal.get(Calendar.DATE)
}
else
{
dayJButtons[cnt].setBackgr
dayJButtons[cnt].setForegr
}
tempCal.add(Calendar.DATE,
continue;
}
// Disable JButtons following end of month
dayJButtons[cnt].setBackgr
dayJButtons[cnt].setText("
dayJButtons[cnt].setEnable
}
}
// Constructor
//------------------------
public TemplateCal()
{
getRootPane().setDefaultBu
setDefaultCloseOperation(E
// Building JPanel for years
//++++++++++++++++++++++++
yearJPanel.setLayout(new java.awt.FlowLayout());
yearJPanel.add(yearMinusJB
yearMinusJButton.setFocusP
yearMinusJButton.setDefaul
yearJPanel.add(yearJLabel)
yearJPanel.add(yearPlusJBu
yearMinusJButton.addAction
yearMinusJButton.addMouseL
yearPlusJButton.addActionL
yearPlusJButton.addMouseLi
// Building JPanel for months
//++++++++++++++++++++++++
monthJPanel.setLayout(new java.awt.GridBagLayout());
GridBagConstraints monthConstraints = new GridBagConstraints();
monthConstraints.gridx=0;
monthConstraints.fill=Grid
monthConstraints.weightx=0
monthConstraints.weighty=0
monthJButtons[0] = new JButton("Jan");
monthJButtons[1] = new JButton("Feb");
monthJButtons[2] = new JButton("Mar");
monthJButtons[3] = new JButton("Apr");
monthJButtons[4] = new JButton("May");
monthJButtons[5] = new JButton("Jun");
monthJButtons[6] = new JButton("Jul");
monthJButtons[7] = new JButton("Aug");
monthJButtons[8] = new JButton("Sep");
monthJButtons[9] = new JButton("Oct");
monthJButtons[10] = new JButton("Nov");
monthJButtons[11] = new JButton("Dec");
for(int month = 0; month < 12; month++)
{
if(month <= 5)
{
monthConstraints.gridy = 0;
monthConstraints.gridx = month + 1;
monthJButtons[month].setPr
monthJButtons[month].setMa
monthJButtons[month].setMi
monthJPanel.add(monthJButt
}
else
{
monthConstraints.gridy = 1;
monthConstraints.gridx = (month-6) + 1;
monthJButtons[month].setPr
monthJButtons[month].setMa
monthJButtons[month].setMi
monthJPanel.add(monthJButt
}
monthJButtons[month].addAc
}
// Building JPanel for days
//++++++++++++++++++++++++
dayJPanel.setLayout(new java.awt.GridLayout(0,7));
weekdayJLabels[0] = new JLabel("S", JLabel.CENTER);
weekdayJLabels[1] = new JLabel("M", JLabel.CENTER);
weekdayJLabels[2] = new JLabel("T", JLabel.CENTER);
weekdayJLabels[3] = new JLabel("W", JLabel.CENTER);
weekdayJLabels[4] = new JLabel("T", JLabel.CENTER);
weekdayJLabels[5] = new JLabel("F", JLabel.CENTER);
weekdayJLabels[6] = new JLabel("S", JLabel.CENTER);
for(int day = 0; day < 7; day++) {
dayJPanel.add(weekdayJLabe
}
// JButtons for days
for(int cnt = 0; cnt < dayJButtons.length; cnt++) {
//dayJButtons[cnt] = new JButton("" + cnt);
dayJButtons[cnt] = new JButton("" + cnt);
dayJPanel.add(dayJButtons[
dayJButtons[cnt].addAction
}
// Building JPanel for bottom JButtons
//++++++++++++++++++++++++
bottomJPanel.setLayout(new
{
JPanel alignJPanel = new JPanel();
alignJPanel.setLayout(new java.awt.GridLayout(1,0));
alignJPanel.add(cancelJBut
cancelJButton.addMouseList
alignJPanel.add(todayJButt
todayJButton.addMouseListe
todayJButton.setFocusPaint
bottomJPanel.add(alignJPan
cancelJButton.addActionLis
todayJButton.addActionList
}
this.getContentPane().setL
GridBagConstraints myConstraints = new GridBagConstraints();
myConstraints.gridx = 0;
myConstraints.fill = GridBagConstraints.NONE;
myConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
myConstraints.gridy = 0;
this.getContentPane().add(
myConstraints.gridy = 1;
this.getContentPane().add(
myConstraints.gridy = 2;
this.getContentPane().add(
myConstraints.gridy = 3;
this.getContentPane().add(
this.setSize(350,375);
this.setDefaultCloseOperat
this.setTitle("Calendar");
this.setLocation(550, 180);
// Call the update view method
//++++++++++++++++++++++++
this.updateView();
}
// Main Method
//------------------------
public static void main(final String[] args) {
new TemplateCal().setVisible(t
}
public void paintBorder(MouseEvent e, boolean borderPainted, Color buttonColor, Color buttonTextColor, Font textFont)
{
Object source = e.getSource();
if (source instanceof JButton)
{
Color initColor = ((JButton)source).getBackg
Color initForeground = ((JButton)source).getForeg
Font initFont = ((JButton)source).getFont(
if (!(initColor == Color.gray))
{
keepColor = initColor;
keepForeground = initForeground;
keepFont = initFont;
}
((JButton)source).setBackg
((JButton)source).setBorde
((JButton)source).setForeg
((JButton)source).setFont(
}
}
public void mouseEntered(MouseEvent e)
{
paintBorder(e, false, Color.gray, Color.yellow, bigFont);
}
public void mouseExited(MouseEvent e)
{
paintBorder(e, true, keepColor ,keepForeground,keepFont);
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e)
{
}
class YearListener implements ActionListener
{
public void actionPerformed(ActionEven
{
Object buttonPressed = e.getSource();
if (buttonPressed == yearMinusJButton) {
currentYear--;
}
else if (buttonPressed == yearPlusJButton) {
currentYear++;
}
updateView();
}
}
class MonthListener implements ActionListener
{
public void actionPerformed(ActionEven
{
Object buttonPressed = e.getSource();
for (int cnt = 0; cnt < monthJButtons.length; cnt++) {
if (buttonPressed == monthJButtons[cnt]) {
currentMonth = cnt;
break;
}
}
updateView();
}
}
class DayListener implements ActionListener
{
public void actionPerformed(ActionEven
{
Object buttonPressed = e.getSource();
for (int cnt = 0; cnt < dayJButtons.length; cnt++) {
if (buttonPressed == dayJButtons[cnt]) {
String datePicked = "" + (currentMonth+1) + "/" + ((cnt+1) - (currentMonthStartWeekday-
//System.out.println(messa
String dateChosen = datePicked;
//BlackBerry.displayMessag
firePropertyChange("select
break;
}
}
}
}
class JButtonListener implements ActionListener
{
public void actionPerformed(ActionEven
{
Object buttonPressed = e.getSource();
if (buttonPressed == todayJButton)
{
//the line below would allow someone to select some other month and year
//and press the Today JButton and get incorrect results, e.g. 12/30/2021
//String datePicked = (currentMonth+1) + "/" + thisDay + "/" + currentYear;
String datePicked = (todayCalendar.get(Calenda
String dateChosen = datePicked;
firePropertyChange("select
}
else if (buttonPressed == cancelJButton)
{
String dateChosen = "";
firePropertyChange("cancel
}
}
}
}
class MyPanel extends JPanel
{
int paintX=0;
int paintY=0;
public MyPanel()
{
super();
setOpaque(true);
}
public void paint(Graphics g)
{
super.paint(g);
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
}
}
Here is the code in the BlackBerry class button "Select Date":
final TemplateCal newTemplateCal = new TemplateCal();
newTemplateCal.setVisible( true);
setEnabled(false);
newTemplateCal.addWindowLi stener(new java.awt.event.WindowAdapt er()
{
public void windowClosed(WindowEvent e)
{
setEnabled(true);
}
});
newTemplateCal.addProperty ChangeList ener(
new PropertyChangeListener()
{
public void propertyChange(PropertyCha ngeEvent evt)
{
if(evt.getPropertyName().e quals("sel ectedDate" ) )
{
newTemplateCal.dispose(); //tears down the Calendar so user sees a re-enabled BlackBerry instance
setVisible(true); //have to do this to get the running BlackBerry instance to have the focus
, otherwise the user has to click the form
BlackBerry.displayMessage( (String)ev t.getNewVa lue());
}
else if(evt.getPropertyName().e quals("can celDate") )
{
newTemplateCal.dispose();
setVisible(true);
stopDateJTextField.request Focus();
}
}
});
final TemplateCal newTemplateCal = new TemplateCal();
newTemplateCal.setVisible(
setEnabled(false);
newTemplateCal.addWindowLi
{
public void windowClosed(WindowEvent e)
{
setEnabled(true);
}
});
newTemplateCal.addProperty
new PropertyChangeListener()
{
public void propertyChange(PropertyCha
{
if(evt.getPropertyName().e
{
newTemplateCal.dispose(); //tears down the Calendar so user sees a re-enabled BlackBerry instance
setVisible(true); //have to do this to get the running BlackBerry instance to have the focus
, otherwise the user has to click the form
BlackBerry.displayMessage(
}
else if(evt.getPropertyName().e
{
newTemplateCal.dispose();
setVisible(true);
stopDateJTextField.request
}
}
});
Sorry--please delete the MyPanel code at the end of the TemplateCal/Calendar code; I was testing something:
class MyPanel extends JPanel
{
int paintX=0;
int paintY=0;
public MyPanel()
{
super();
setOpaque(true);
}
public void paint(Graphics g)
{
super.paint(g);
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
}
}
class MyPanel extends JPanel
{
int paintX=0;
int paintY=0;
public MyPanel()
{
super();
setOpaque(true);
}
public void paint(Graphics g)
{
super.paint(g);
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks..but where is the action listener for the button...?..what is the button supposed to invoke?
Thanks for accepting. I apologize for not being more clear. The code for the Calendar button's ActionListener is:
calendarJButton.setText("S elect Date");
calendarJButton.addActionL istener(ne w java.awt.event.ActionListe ner() {
public void actionPerformed(java.awt.e vent.Actio nEvent evt) {
calendarJButtonActionPerfo rmed(evt);
}
});
Also, my second post (9:01 am today) was the body of the calendarJButtonActionPefor med event. I should have prefaced that block of code with:
private void calendarJButtonActionPerfo rmed(java. awt.event. ActionEven t evt) {
I'll loath to submit the entire code of my BlackBerry class (which houses the Calendar button), but if it will help make the interactions between the two classes (BlackBerry and TemplateCal), just say the word. To answer your second question, the calendarJButton instantiates the TemplateCal class, which is a calendar. See this EE post for an explanation of how the PropertyChangeEvent works:
https://www.experts-exchange.com/questions/21255771/Looking-for-way-to-implement-a-method-for-a-class-after-I've-obtained-the-class-name-as-a-String.html
calendarJButton.setText("S
calendarJButton.addActionL
public void actionPerformed(java.awt.e
calendarJButtonActionPerfo
}
});
Also, my second post (9:01 am today) was the body of the calendarJButtonActionPefor
private void calendarJButtonActionPerfo
I'll loath to submit the entire code of my BlackBerry class (which houses the Calendar button), but if it will help make the interactions between the two classes (BlackBerry and TemplateCal), just say the word. To answer your second question, the calendarJButton instantiates the TemplateCal class, which is a calendar. See this EE post for an explanation of how the PropertyChangeEvent works:
https://www.experts-exchange.com/questions/21255771/Looking-for-way-to-implement-a-method-for-a-class-after-I've-obtained-the-class-name-as-a-String.html