Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: jpanel and using the timer class

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default jpanel and using the timer class

    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 timer class bt doesnt work.
    i also have another jpanel that displays the current user and current time and date which also uses a timer class and an actionperformed method to update the time every 1 second.
    my code is as follows


     
    /*
     * 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 {
     
     [B][COLOR="blue"]//CODE FOR THE JPANEL TO DISPLAY THE MOVING MESSAGE[/COLOR][/B]
     
        private static class MovMsgJPanel extends javax.swing.JPanel {
            private String message = "Welcome to java";
            private int xCoordinate = 0;
            private int yCoordinate = 20;
     
            public MovMsgJPanel() {
     
                Timer timer = new Timer(1000, new TimerListener());
     
                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;
     
    [B][COLOR="blue"]TIMER CLASS TO UPDATE THE TIME[/COLOR][/B]
     
         Timer timer = new Timer(1000, (ActionListener) this);
     
     
     
        /** Creates new form MainForm */
     
        public MainForm(java.sql.Connection databaseConnection) {
            dbconn = databaseConnection;
            initComponents();
     
           this.add(new MovMsgJPanel());
     
            CurrentUserLbl.setText("Current user" + " "+System.getProperty("active user"));//setting the current user as the logged in user
     
     
     
              timer.start();
     
     
        }
     
     
     
     
     
     
     
     
        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                   
     
    }
    Last edited by copeg; March 31st, 2011 at 11:55 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: jpanel and using the timer class

    For future posts, please flank your code with the code tags.

    What about this 'doesn't work'? Are there exceptions thrown? If it misbehaves, how? To demonstrate the problem, it helps to break it down to only the necessary portions of code and post an SSCCE

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: jpanel and using the timer class

    there are no errors but no text is shown on the jpanel that is supposed to show a moving message. the code that doesnt work is:

     private static class MovMsgJPanel extends javax.swing.JPanel {
            private String message = "Welcome to java";
            private int xCoordinate = 0;
            private int yCoordinate = 20;
     
            public MovMsgJPanel() {
     
                Timer timer = new Timer(1000, new TimerListener());
     
                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();
                }
            }
        }

Similar Threads

  1. Timer class countdown
    By p0oint in forum Object Oriented Programming
    Replies: 5
    Last Post: August 26th, 2010, 03:31 AM
  2. Timer Class help
    By Deadbob in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 23rd, 2010, 12:18 AM
  3. How to copy image from one jpanel to another jpanel
    By ramanavarayuri1986 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 15th, 2010, 02:36 AM
  4. revalidate() JPanel outside of class it resides
    By Deadbob in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 9th, 2010, 05:14 PM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM