How to get Current time in a panel label

I have a main class called Mclass.java, from this call am calling a thread which is in SimpleThread.java , and also creating an object for Taskframe.java the TaskFrame.java contions panels

From the SimpleThread.java am getting current time, and I want to display the time in any of the panel , how can I do that ? I mean I want the label name is current system time

[387 byte] By [rayees1234a] at [2007-11-27 10:13:01]
# 1

JLabel currentsystem time = new JLabel(new java.util.Date(System.currentTimeMillis()).toString())

add this to your panel, from your run of your thread set the text of that each time

regards

Aniruddha

Aniruddha-Herea at 2007-7-28 15:24:10 > top of Java-index,Desktop,Core GUI APIs...
# 2

Your description of the problem makes no sense...

Post your code, remembering to use the code formatting tags, which demonstrates the incorrect behaviour

c0demonk3ya at 2007-7-28 15:24:10 > top of Java-index,Desktop,Core GUI APIs...
# 3

I Thanks for your response, This is my problem, How can I bring current (Dynamic time) in Delpan.java (Panel)

Main.java

public static void main(String[] args) {

// TODO Auto-generated method stub

new SimpleThread().start();

Delpan delp = new Delpan();

}

}

SimpleThread.java

class SimpleThread extends Thread {

Delpan del = new Delpan();

public SimpleThread() {

super();

}

public String cur_time;

Calendar cal = Calendar.getInstance(TimeZone.getDefault());

public void run() {

while(true)

{

Calendar cal = Calendar.getInstance(TimeZone.getDefault());

cur_time = String.valueOf(cal.getTime());

try {

sleep(1000);

} catch (InterruptedException ie) {System.out.println("Exception"+ie);}

System.out.println("Time Dispaly "+cur_time);

}

}

}

Delpan.java

public class Delpan extends JPanel {

private static final long serialVersionUID = 1L;

public Delpan() {

super();

initialize();

}

private void initialize() {

JLabel currentsystemtime = new JLabel(new java.util.Date(System.currentTimeMillis()).toString());

currentsystemtime.setBounds(new Rectangle(28, 78, 179, 16));

this.setSize(239, 120);

this.setLayout(null);

this.add(currentsystemtime, null);

}

}

rayees1234a at 2007-7-28 15:24:10 > top of Java-index,Desktop,Core GUI APIs...
# 4

I said use the code formatting codes.... reading unformatted code makes my brain ache

c0demonk3ya at 2007-7-28 15:24:10 > top of Java-index,Desktop,Core GUI APIs...
# 5

Main.java

public static void main(String[] args) {

// TODO Auto-generated method stub

new SimpleThread().start();

Delpan delp = new Delpan();

}

}

SimpleThread.java

class SimpleThread extends Thread {

Delpan del = new Delpan();

public SimpleThread() {

super();

}

public String cur_time;

Calendar cal = Calendar.getInstance(TimeZone.getDefault());

public void run() {

while(true)

{

Calendar cal = Calendar.getInstance(TimeZone.getDefault());

cur_time = String.valueOf(cal.getTime());

try {

sleep(1000);

} catch (InterruptedException ie) {System.out.println("Exception"+ie);}

System.out.println("Time Dispaly "+cur_time);

}

}

}

Delpan.java

public class Delpan extends JPanel {

private static final long serialVersionUID = 1;

public Delpan() {

super();

initialize();

}

private void initialize() {

JLabel currentsystemtime = new JLabel(new java.util.Date(System.currentTimeMillis()).toString());

currentsystemtime.setBounds(new Rectangle(28, 78, 179, 16));

this.setSize(239, 120);

this.setLayout(null);

this.add(currentsystemtime, null);

}

}

rayees1234a at 2007-7-28 15:24:10 > top of Java-index,Desktop,Core GUI APIs...
# 6

Your code example would never work as you don't have any references to you panel within the Thread class.

Try something more like this: -

import java.awt.BorderLayout;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Timer;

import java.util.TimerTask;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.SwingConstants;

import javax.swing.SwingUtilities;

public class ThreadTimerExample {

private Timer timer;

private JFrame jFrame = null;

private JPanel jContentPane = null;

private JLabel jLabel = null;

/**

* This method initializes jFrame

*

* @return javax.swing.JFrame

*/

private JFrame getJFrame() {

if (jFrame == null) {

jFrame = new JFrame();

jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jFrame.setSize(300, 200);

jFrame.setContentPane(getJContentPane());

jFrame.setTitle("Application");

}

return jFrame;

}

/**

* This method initializes jContentPane

*

* @return javax.swing.JPanel

*/

private JPanel getJContentPane() {

if (jContentPane == null) {

jLabel = new JLabel();

jLabel.setText(new SimpleDateFormat("dd MMM yyyy kk:mm:ss").format(Calendar.getInstance().getTime()));

jLabel.setHorizontalAlignment(SwingConstants.CENTER);

jContentPane = new JPanel();

jContentPane.setLayout(new BorderLayout());

jContentPane.add(jLabel, BorderLayout.CENTER);

}

return jContentPane;

}

private void startTimer() {

// Start the clock

timer = new Timer();

timer.schedule(new TimeTask(), 0, 1000);

}

/**

* Launches this application

*/

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

ThreadTimerExample application = new ThreadTimerExample();

application.getJFrame().setVisible(true);

application.startTimer();

}

});

}

class TimeTask extends TimerTask {

public void run() {

jLabel.setText(new SimpleDateFormat("dd MMM yyyy kk:mm:ss").format(Calendar.getInstance().getTime()));

}

}

}

c0demonk3ya at 2007-7-28 15:24:10 > top of Java-index,Desktop,Core GUI APIs...
# 7

> I have a main class called Mclass.java, from this

> call am calling a thread which is in

> SimpleThread.java , and also creating an object for

> Taskframe.java the TaskFrame.java contions panels

>

>

> From the SimpleThread.java am getting current time,

> and I want to display the time in any of the panel ,

> how can I do that ? I mean I want the label name is

> current system time

I made something similiar a few days ago.

It displays the current system time in the JFrame in a Jlabel using a thread. Might not be the best code i created Java wise, but it works.

http://www.engineeringserver.com/t3hc0d3z/using+threads+to+display+the+time-t105.0.html

deAppela at 2007-7-28 15:24:10 > top of Java-index,Desktop,Core GUI APIs...