Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

display system date and time on Status Bar - How do I 1

Status
Not open for further replies.

karlomutschler

Programmer
Jun 5, 2001
75
DE
Hi all,

I wish to display the current system date in a text field of status bar.
Similarly, I wish to display the current system time.
It should not be static, but be updated similar to the windows time display normally in the right bottom corner (of Windows)

Any help much appreciated.
Kind regards
Karlo

meistertools@gmx.net
 
That is a reasonable wish. Now what is your question?

-pete
 
Hi Pete,

thanks for you valued response.

I have a JFrame with a StatusBar added in the SOUTH position.
The StatusBar consists of textfields managed by a Box Layout.
At dateLongField I wish to display the current system date.
At timeField I wish to display the current system time.

The question is:
Where do I find the current system date and how/where do I implement it?
Similarly, where do I find the current system time and how/where in the code do I implement it.

The following is the code I have at the moment:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.*;

public class StatusBar extends JFrame {

//Object Variables
JTextField messageField;
JTextField userField;
JTextField dateLongField;
JTextField capsLockField;
JTextField numLockField;
JTextField scrollLockField;
JTextField timeField;
Toolkit toolkit = Toolkit.getDefaultToolkit();

public StatusBar() {
super("Status Bar");

addKeyListener(new CapsLockKeyListener());
addKeyListener(new NumLockKeyListener());
addKeyListener(new ScrollLockKeyListener());

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container contentPane = getContentPane();
Border raisedBorder = new BevelBorder(BevelBorder.RAISED);
Border loweredBorder = new BevelBorder(BevelBorder.LOWERED);
Border emptyBorder = new EmptyBorder(2, 2, 2, 2);

//StatusBar
Box sbar = Box.createHorizontalBox();
messageField = new JTextField(30);
messageField.setBackground(Color.LIGHT_GRAY);
messageField.setBorder(new CompoundBorder(loweredBorder, emptyBorder));

userField = new JTextField(12);
userField.setText("Karlo Mutschler");
userField.setBackground(Color.LIGHT_GRAY);
userField.setBorder(new CompoundBorder(loweredBorder, emptyBorder));

dateLongField = new JTextField(18);
dateLongField.setText("Donnerstag, 31. November 2002");
dateLongField.setBackground(Color.LIGHT_GRAY);
dateLongField.setBorder(new CompoundBorder(loweredBorder, emptyBorder));

capsLockField = new JTextField(5);
if (toolkit.getLockingKeyState(KeyEvent.VK_CAPS_LOCK)) {
capsLockField.setText("Caps On");
} else {
capsLockField.setText("Caps Off");
}
capsLockField.setBackground(Color.LIGHT_GRAY);
capsLockField.setBorder(new CompoundBorder(loweredBorder, emptyBorder));

numLockField = new JTextField(5);
if (toolkit.getLockingKeyState(KeyEvent.VK_NUM_LOCK)) {
numLockField.setText("Num On");
} else {
numLockField.setText("Num Off");
}
numLockField.setBackground(Color.LIGHT_GRAY);
numLockField.setBorder(new CompoundBorder(loweredBorder, emptyBorder));

scrollLockField = new JTextField(5);
if (toolkit.getLockingKeyState(KeyEvent.VK_SCROLL_LOCK)) {
scrollLockField.setText("Scroll On");
} else {
scrollLockField.setText("Scroll Off");
}
scrollLockField.setBackground(Color.LIGHT_GRAY);
scrollLockField.setBorder(new CompoundBorder(loweredBorder, emptyBorder));

timeField = new JTextField(5);
timeField.setText("23:47");
timeField.setBackground(Color.LIGHT_GRAY);
timeField.setBorder(new CompoundBorder(loweredBorder, emptyBorder));

messageField.setEditable(false);
sbar.add(messageField);
sbar.add(Box.createGlue());
userField.setEditable(false);
sbar.add(userField);
dateLongField.setEditable(false);
sbar.add(dateLongField);
capsLockField.setEditable(false);
sbar.add(capsLockField);
numLockField.setEditable(false);
sbar.add(numLockField);
scrollLockField.setEditable(false);
sbar.add(scrollLockField);
timeField.setEditable(false);
sbar.add(timeField);

sbar.setBorder(new CompoundBorder(raisedBorder, emptyBorder));

contentPane.add(sbar, BorderLayout.SOUTH);
}
public static void main(String[] arguments) {
StatusBar sb = new StatusBar();
sb.setVisible(true);
sb.setExtendedState(Frame.MAXIMIZED_BOTH);
}
public boolean isFocusable()
{
return true;
}

//Inner Classes

class CapsLockKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent e) {
if (toolkit.getLockingKeyState(KeyEvent.VK_CAPS_LOCK)){
capsLockField.setText("Caps On");
} else {
capsLockField.setText("Caps Off");
}
}
}

class NumLockKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent e){
if(toolkit.getLockingKeyState(KeyEvent.VK_NUM_LOCK)){
numLockField.setText("Num On");
} else {
numLockField.setText("Num Off");
}
}
}

class ScrollLockKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent e){
if(toolkit.getLockingKeyState(KeyEvent.VK_SCROLL_LOCK)){
scrollLockField.setText("Scroll On");
} else {
scrollLockField.setText("Scroll Off");
}
}
}
}






meistertools@gmx.net
 
>> Where do I find the current system date
Code:
java.util.Calender dt = java.util.Calendar.getInstance();

If you look at the API for the Calendar class you will find everything you need for displaying the current Date/Time information.

>> how/where in the code do I implement it.

Well displaying the Date would be simple but to do the time you will need to implement:

java.util.TimerTask
Or
javax.swing.Timer

hope this helps
-pete
 
Thanks Pete,

does the following look correct ?

GregorianCalendar sysDate = new GregorianCalendar();
Date date = sysDate.getTime();
SimpledateFormat formatter = new SimpleDateFormat("dd.MM.yy");
String dateLongField = formatter.format(date);
dateLongField = new JTextField(18);

// dateLongField.setText("Donnerstag, 31. November 2002");
dateLongField.setBackground(Color.LIGHT_GRAY);
dateLongField.setBorder(new CompoundBorder(loweredBorder, emptyBorder));

Kind regards
Karlo meistertools@gmx.net
 
No it does not look correct. It does not compile does it?

String dateLongField = formatter.format(date);
dateLongField = new JTextField(18);


your changing the "type" of dateLongField, so that should be a compile error.

-pete
 
Thanks Pete,
indeed, it did not compile !

The latest version, which does compile is as follows:
However, the "dateLongField" does not appear in the StatusBar at all.

import java.awt.*;
import java.awt.event.*;

import java.text.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;

public class StatusBar extends JFrame {

//Object Variables
JTextField messageField;
JTextField userField;
// JTextField dateLongField;
String dateLongField;
JTextField capsLockField;
JTextField numLockField;
JTextField scrollLockField;
JTextField timeField;
Toolkit toolkit = Toolkit.getDefaultToolkit();

public StatusBar() {
super("Status Bar");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container contentPane = getContentPane();

//StatusBar
Box sbar = Box.createHorizontalBox();
messageField = new JTextField(30);

userField = new JTextField(12);
userField.setText("John Citizen");

// ============================================================================
// dateLongField = new JTextField(18);
GregorianCalendar sysDate = new GregorianCalendar();
Date date = sysDate.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yy");
String dateLongField = formatter.format(date);

// dateLongField.setText("Friday, 14th March, 2003");
// dateLongField.setText(date);
// ============================================================================

capsLockField = new JTextField(5);
capsLockField.setText("Caps On");

numLockField = new JTextField(5);
numLockField.setText("Num On");

scrollLockField = new JTextField(5);
scrollLockField.setText("Scroll On");

timeField = new JTextField(5);
timeField.setText("23:47");

messageField.setEditable(false);
sbar.add(messageField);
sbar.add(Box.createGlue());
userField.setEditable(false);
sbar.add(userField);
// dateLongField.setEditable(false);
// sbar.add(dateLongField);
capsLockField.setEditable(false);
sbar.add(capsLockField);
numLockField.setEditable(false);
sbar.add(numLockField);
scrollLockField.setEditable(false);
sbar.add(scrollLockField);
timeField.setEditable(false);
sbar.add(timeField);

contentPane.add(sbar, BorderLayout.SOUTH);
}
public static void main(String[] arguments) {
StatusBar sb = new StatusBar();
sb.setVisible(true);
sb.setExtendedState(Frame.MAXIMIZED_BOTH);
}
}

Kind regards
Karlo meistertools@gmx.net
 
Hi,
with the following code I have verified that the appropriate date has been obtained (System.out.println .... but nothin is displayed and in addition there is an error message.

Would you be able to show me where the mistake is located and also how to implement the timer tread to update the time.

Also, is the format correct to display the date as required?

import java.awt.*;
import java.awt.event.*;

import java.text.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;

public class StatusBar extends JFrame {

//Object Variables
JTextField messageField;
JTextField userField;
JTextField dateLongField;
JTextField capsLockField;
JTextField numLockField;
JTextField scrollLockField;
JTextField timeField;
Toolkit toolkit = Toolkit.getDefaultToolkit();

public StatusBar() {
super("Status Bar");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container contentPane = getContentPane();

//StatusBar
Box sbar = Box.createHorizontalBox();
messageField = new JTextField(30);

userField = new JTextField(12);
userField.setText("John Citizen");

// ============================================================================
dateLongField = new JTextField(18);

Calendar calendar = new GregorianCalendar();
Date sysDate = new Date();
calendar.setTime(sysDate);

System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));

SimpleDateFormat formatter = new SimpleDateFormat("EEEE, dd ''th MMMM, yyyy");
dateLongField = formatter.format(sysDate); //line 46

// dateLongField.setText("Friday, 14th March, 2003");

/*
StatusBar.java [46:1] incompatible types
found : java.lang.String
required: javax.swing.JTextField
dateLongField = formatter.format(sysDate);
*/

// ============================================================================

capsLockField = new JTextField(5);
capsLockField.setText("Caps On");

numLockField = new JTextField(5);
numLockField.setText("Num On");

scrollLockField = new JTextField(5);
scrollLockField.setText("Scroll On");

timeField = new JTextField(5);
timeField.setText("23:47");

messageField.setEditable(false);
sbar.add(messageField);
sbar.add(Box.createGlue());
userField.setEditable(false);
sbar.add(userField);
dateLongField.setEditable(false);
sbar.add(dateLongField);
capsLockField.setEditable(false);
sbar.add(capsLockField);
numLockField.setEditable(false);
sbar.add(numLockField);
scrollLockField.setEditable(false);
sbar.add(scrollLockField);
timeField.setEditable(false);
sbar.add(timeField);

contentPane.add(sbar, BorderLayout.SOUTH);
}
public static void main(String[] arguments) {
StatusBar sb = new StatusBar();
sb.setVisible(true);
sb.setExtendedState(Frame.MAXIMIZED_BOTH);
}
}

Thanks and kind regards
Karlo

meistertools@gmx.net
 
dateLongField is a JTestField, hence you can't use..

dateLongField = formatter.format(sysDate);

As formatter.format(); returns a String !

You will need to first instantiate a the dateLongField, and then set it's Text, e.g.

dateLongField = new JTextField();
dateLongField .setText(formatter.format(sysDate));

This can be achieved in one line, as JTextField has an alternative constructor...

dateLongField = new JTextField(formatter.format(sysDate));






 
Hi thekobbler,

Thank you very much - you've been very helpful !!
Enjoy your weekend

Regards
Karlo
meistertools@gmx.net
 
Hi,

it works like a charm - thanks again for your assistance.

// ============================================================================
dateLongField = new JTextField(18);

Calendar calendar = new GregorianCalendar();
Date sysDate = new Date();
calendar.setTime(sysDate);

// System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
// System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));

SimpleDateFormat formatter = new SimpleDateFormat("EEEE, dd MMMM, yyyy");
dateLongField = new JTextField(formatter.format(sysDate));

// ============================================================================

There is still a matter with the formatting of the date, but that will be handled at a later date.

timeField = new JTextField(5);
timeField.setText("23:47");

thanks and kind regards
Karlo

How would I display the current system time, bearing in mind that the display must be updated after every minute meistertools@gmx.net
 
>> How would I display the current system time

theKobbler and I have already answered that question. Please go back and read the thread.

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top