Hello
guys !!
What’s
up!!
Welcome
back to masunduh2,last post,I’ve
shared tutorial about about how to add tooltipstext into component. today, I
will show you the next simple tutorial netbeans java swing how to display
dynamic time.
How
do that ??
Ok
lets get started.
Firstly,
open your netbeans IDE software
Then,
create new project,file > new project or press Ctrl+shift+N. and click finish.
Create
Java Class In your Java source package,
right
click > New > Java Class. > rename to “Date” > and click OK.
Open
your java class (Date.java)
Type
or copy all syntax below
/*
* www.masunduh2.blogspot.com
* subscribe Masunduh2 on YouTube Channel
*/
package paket_table;
import java.util.Calendar;
/**
*
* @author Administrator
*/
public class Date {
private Calendar calendar = Calendar.getInstance();
public String getDay()
{
if(calendar.get(Calendar.DAY_OF_WEEK)==1)
{
return "Sunday";
}
else if(calendar.get(Calendar.DAY_OF_WEEK)==2)
{
return "Monday";
}
else if(calendar.get(Calendar.DAY_OF_WEEK)==3)
{
return "Tuesday";
}
else if(calendar.get(Calendar.DAY_OF_WEEK)==4)
{
return "Wednesday";
}
else if(calendar.get(Calendar.DAY_OF_WEEK)==5)
{
return "Thursday";
}
else if(calendar.get(Calendar.DAY_OF_WEEK)==6)
{
return "Friday";
}
else
{
return "Saturday";
}
}
public String getMonth()
{
if(calendar.get(Calendar.MONTH)==0)
{
return "January";
}
else if(calendar.get(Calendar.MONTH)==1)
{
return "February";
}
else if(calendar.get(Calendar.MONTH)==2)
{
return "March";
}
else if(calendar.get(Calendar.MONTH)==3)
{
return "April";
}
else if(calendar.get(Calendar.MONTH)==4)
{
return "May";
}
else if(calendar.get(Calendar.MONTH)==5)
{
return "June";
}
else if(calendar.get(Calendar.MONTH)==6)
{
return "July";
}
else if(calendar.get(Calendar.MONTH)==7)
{
return "August";
}
else if(calendar.get(Calendar.MONTH)==8)
{
return "September";
}
else if(calendar.get(Calendar.MONTH)==9)
{
return "October";
}
else if(calendar.get(Calendar.MONTH)==10)
{
return "November";
}
else
{
return "December";
}
}
public String getTime()
{
String day;
String date;
String month;
String year;
day = getDay();
date = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
month = getMonth();
year = String.valueOf(calendar.get(Calendar.YEAR));
return (day+","+month+" "+date+","+year);
}
}
and
paste into source tab in java class which you’ve make.
Then,same
step like above
Just
create Jframe Form In your Java source package,
right
click > New > Jframe Form. > rename to “table_form” > and click OK.
Open
your jframe form(table_form.java)
Add
two jlabel component in palette tools for display date in jlabel1 and time in
jlabel2
Set
name of jlabel1 to “date_label”,
And
set name of jlabel2 to “time_label”.
If
you do that,
Next,
click source tab in Jframe form.
Create
new method for time
Just
copied or type all syntax below
public void Time()
{
Date dates = new Date();
date_label.setText(dates.getTime());
Thread clock = new Thread()
{
@Override
public void run()
{
for(;;)
{
Calendar calendar = new GregorianCalendar();
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int sec = calendar.get(Calendar.SECOND);
int am_pm = calendar.get(Calendar.AM_PM);
String day_night="";
if(am_pm==1)
{
day_night="PM";
}
else
{
day_night="AM";
}
time_label.setText(" "+"Time:"+""+hour+":"+""+minute+":"+""+sec+""+day_night);
try {
sleep(1000);
} catch (InterruptedException e)
{
System.out.println(""+e);
}
}
}
};
clock.start();
}
And
paste that all syntax below name of your public class jframe form.
If
you do that, go to next step
Copy
or type time(); method into public table_form(), precisely below initComponents();
Now,
if no error in your java swing project you can run that programs
Click
Run > choose Clean and build
project.
Then,
click run again > and select Run File.
Like
image below
Screenshot
Its
easy right ??
Watch
video tutorial how to display dynamic time with
netbeans java swing.
Please write comment,share it, and subscribed !
Don’t forget join on my fanspage now,ok guys !
See you the next awesome and simple tutorials. Bye
!
0 Response to "tutorial netbeans-how to display dinamic time (vol.10)"
Post a Comment