problem using an action listener and swing timer on a jpanel
hi all, im making a school management system using netbeans java desktop application
just need help displaying moving text on a jpanel at the bottom of my mainform. i used a swing timer class bt doesnt work despite there being no errors.
i also have another jpanel below the jpanel thtat is supposed to show the moving text that displays the current user and current time and date which also uses a swing timer class and an actionperformed method to update the time every 1 second and it works perfectly.
my code for the mainform is as follows:
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* MainForm.java
*
* Created on Mar 19, 2011, 8:48:29 PM
*/
package sms;
import java.awt.event.ActionListener;
import java.util.Locale;
import javax.swing.Timer;
//import sms.MainForm.MovMsgJPanel.TimerListener;
/**
*
* @author makswizzy
*/
public class MainForm extends javax.swing.JFrame implements ActionListener {
private static class MovMsgJPanel extends javax.swing.JPanel {
private String message = "Welcome to java";
private int xCoordinate = 0;
private int yCoordinate = 20;
public MovMsgJPanel() {
//this.message = message;
Timer timer = new Timer(1000, new TimerListener());
//Timer timer = new Timer(1000, (ActionListener)this);
timer.start();
}
//paint message
@Override
public void paintComponent(java.awt.Graphics g){
super.paintComponent(g);
if(xCoordinate >getWidth()){
xCoordinate = -20;
}
xCoordinate +=5;
g.drawString("message moving", xCoordinate, yCoordinate);
}
class TimerListener implements ActionListener{
//handle ActionEvent
public void actionPerformed(java.awt.event.ActionEvent e){
repaint();
}
}
}
private java.sql.Connection dbconn = null;
Timer timer = new Timer(1000, (ActionListener) this);
// Timer timer2 = new Timer(1000, (ActionListener)this);
/** Creates new form MainForm */
//java.sql.Connection databaseConnection
public MainForm(java.sql.Connection databaseConnection) {
dbconn = databaseConnection;
initComponents();
// MovMsgJPanel.addComponentListener(JPanel);
this.add(new MovMsgJPanel());
CurrentUserLbl.setText("Current user" + " "+System.getProperty("active user"));//setting the current user as the logged in user
//java.util.Calendar calendar = new java.util.GregorianCalendar();//creating a calender object and setting it to the gregorian calendar
//java.text.DateFormat formatter = java.text.DateFormat.getDateTimeInstance(java.text.DateFormat.FULL, java.text.DateFormat.MEDIUM,Locale.getDefault());///formatting the date and time
//java.util.TimeZone timezone = java.util.TimeZone.getDefault();//getting the default time zone as per the system time zone and assigning it to the variable timezone
//formatter.setTimeZone(timezone);//setting the time zone to the format and can also be done as below
//formatter.setTimeZone(java.util.TimeZone.getDefault());
//CurrentTimeLbl.addAncestorListener(ActionListener);
//javax.swing.JLabel c = new javax.swing.JLabel(formatter.format(calendar.getTime()));
//c = CurrentTimeLbl;
timer.start();
//timer2.start();
//CurrentTimeLbl.setText(formatter.format(calendar.getTime()));//making the date and time appear on the jlabel
}
public void actionPerformed(java.awt.event.ActionEvent e){
java.util.Calendar calendar = new java.util.GregorianCalendar();//creating a calender object and setting it to the gregorian calendar
java.text.DateFormat formatter = java.text.DateFormat.getDateTimeInstance(java.text.DateFormat.FULL, java.text.DateFormat.MEDIUM,Locale.getDefault());///formatting the date and time
java.util.TimeZone timezone = java.util.TimeZone.getDefault();//getting the default time zone as per the system time zone and assigning it to the variable timezone
formatter.setTimeZone(timezone);//setting the time zone to the format and can also be done as below
//formatter.setTimeZone(java.util.TimeZone.getDefault());
CurrentTimeLbl.setText(formatter.format(calendar.getTime()));
}
/** 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() {
java.awt.GridBagConstraints gridBagConstraints;
TaskBarJPanel = new javax.swing.JPanel();
CurrentUserLbl = new javax.swing.JLabel();
CurrentTimeLbl = new javax.swing.JLabel();
MainDesktopPane = new javax.swing.JDesktopPane();
MovMsgJPanel = new javax.swing.JPanel();
MenuBar = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenu2 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(MainForm.class);
setTitle(resourceMap.getString("Form.title")); // NOI18N
setName("Form"); // NOI18N
getContentPane().setLayout(new java.awt.GridBagLayout());
TaskBarJPanel.setName("TaskBarJPanel"); // NOI18N
TaskBarJPanel.setLayout(new java.awt.GridBagLayout());
CurrentUserLbl.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
CurrentUserLbl.setText(resourceMap.getString("CurrentUserLbl.text")); // NOI18N
CurrentUserLbl.setName("CurrentUserLbl"); // NOI18N
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
TaskBarJPanel.add(CurrentUserLbl, gridBagConstraints);
CurrentTimeLbl.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
CurrentTimeLbl.setText(resourceMap.getString("CurrentTimeLbl.text")); // NOI18N
CurrentTimeLbl.setName("CurrentTimeLbl"); // NOI18N
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
TaskBarJPanel.add(CurrentTimeLbl, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 3;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
getContentPane().add(TaskBarJPanel, gridBagConstraints);
MainDesktopPane.setBackground(resourceMap.getColor("MainDesktopPane.background")); // NOI18N
MainDesktopPane.setName("MainDesktopPane"); // NOI18N
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridheight = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 150.0;
gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
getContentPane().add(MainDesktopPane, gridBagConstraints);
MovMsgJPanel.setName("MovMsgJPanel"); // NOI18N
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
getContentPane().add(MovMsgJPanel, gridBagConstraints);
MenuBar.setName("MenuBar"); // NOI18N
jMenu1.setText(resourceMap.getString("jMenu1.text")); // NOI18N
jMenu1.setName("jMenu1"); // NOI18N
MenuBar.add(jMenu1);
jMenu2.setText(resourceMap.getString("jMenu2.text")); // NOI18N
jMenu2.setName("jMenu2"); // NOI18N
MenuBar.add(jMenu2);
setJMenuBar(MenuBar);
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 MainForm().setVisible(true);
}
});
}
*
*/
// Variables declaration - do not modify
private javax.swing.JLabel CurrentTimeLbl;
private javax.swing.JLabel CurrentUserLbl;
private javax.swing.JDesktopPane MainDesktopPane;
private javax.swing.JMenuBar MenuBar;
private javax.swing.JPanel MovMsgJPanel;
private javax.swing.JPanel TaskBarJPanel;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
// End of variables declaration
}