This article will create and enter a TimerLabel into a Palette inside NetBeans from where we can use it in any Project. The Timer label will simply display the current date and time.
Starting the Project: Create a File called TimerLabel.java with the following Contents.
TimerLabel.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package timerbeansproject;
import java.util.Date;
import javax.swing.JLabel;
/**
*
* @author Champak */
public class TimerLabel extends JLabel {
public TimerLabel()
{
TimerThread t=new TimerThread();
t.start();
}
private class TimerThread extends Thread
{
@Override
public void run()
{
while(true)
{
try
{
Date d=new Date();
TimerLabel.this.setText("" + d);
Thread.sleep(1000);
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
}
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package timerbeansproject;
import java.util.Date;
import javax.swing.JLabel;
/**
*
* @author Champak */
public class TimerLabel extends JLabel {
public TimerLabel()
{
TimerThread t=new TimerThread();
t.start();
}
private class TimerThread extends Thread
{
@Override
public void run()
{
while(true)
{
try
{
Date d=new Date();
TimerLabel.this.setText("" + d);
Thread.sleep(1000);
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
}
}
TimerFrame.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* TimerFrame.java
*
* Created on Nov 9, 2010, 1:09:28 PM
*/
package timerbeansproject;
/**
*
* @author Champak */
public class TimerFrame extends javax.swing.JFrame {
/** Creates new form TimerFrame */
public TimerFrame() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
timerLabel1 = new timerbeansproject.TimerLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(102, 102, 102)
.addComponent(timerLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(150, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(timerLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(275, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TimerFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private timerbeansproject.TimerLabel timerLabel1;
// End of variables declaration
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* TimerFrame.java
*
* Created on Nov 9, 2010, 1:09:28 PM
*/
package timerbeansproject;
/**
*
* @author Champak */
public class TimerFrame extends javax.swing.JFrame {
/** Creates new form TimerFrame */
public TimerFrame() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
timerLabel1 = new timerbeansproject.TimerLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(102, 102, 102)
.addComponent(timerLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(150, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(timerLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(275, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TimerFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private timerbeansproject.TimerLabel timerLabel1;
// End of variables declaration
}
The series of Steps
- Starting the New Project
2. Building the Project.
3.Opening the Palette.
4.The Palette Manager
5.Selecting the Project containing the New Bean
6.Choosing the Component.
7.Adding the Timer Label to a JFrame
No comments:
Post a Comment