Results 1 to 11 of 11
- 04-02-2011, 09:26 PM #1
Member
- Join Date
- Jul 2008
- Posts
- 7
- Rep Power
- 0
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:
Java 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 }
-
What is the Timer supposed to do? All I see is the method repaint() inside your Timer's ActionListener's actionPerformed, and this won't do anything other than, well, repaint things.
- 04-02-2011, 09:55 PM #3
Member
- Join Date
- Jul 2008
- Posts
- 7
- Rep Power
- 0
the timer is supposed to make the message(text) move across the jpanel and update the text every second and move the text by incrementing the distance in the x direction by the value of x which i have set to 5.
-
Oh, you've got the program logic in the paintComponent method. Don't ever do this as you don't ever have full control on when or even if this will be called. You need to increment your x values in the Timer and then use the x value in the paintComponent method.
-
Also, how big do you think this MovMsgJPanel is? You're adding it to the contentPane, a container whose layout has been set to GridBagLayout, and you're adding it without using GridBagConstraints. In my run of your program, this JPanel is quite small. Try putting a colored border around it to see what I mean:
Java Code:public MainForm() { initComponents(); MovMsgJPanel mmPanel = new MovMsgJPanel(); mmPanel.setBorder(BorderFactory.createLineBorder(Color.red)); this.add(mmPanel);
But again, don't do program logic inside of paintComponent.
- 04-02-2011, 10:11 PM #6
Member
- Join Date
- Jul 2008
- Posts
- 7
- Rep Power
- 0
here is what ive tried doing, i have put the if statement which increments the text in the x direction in the TimerListener class but it still doesnt work .There are no errors. am i i doing the right thing?
Java Code: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); g.drawString("message moving", xCoordinate, yCoordinate); } class TimerListener implements ActionListener{ //handle ActionEvent public void actionPerformed(java.awt.event.ActionEvent e){ if(xCoordinate >getWidth()){ xCoordinate = -20; } xCoordinate +=5; repaint(); } } }
-
Best way to answer that question is to create a small program that tests it out.
e.g.,
Java Code:import java.awt.Dimension; import java.awt.event.*; import javax.swing.*; public class MovingMessagePanelTest { private static void createAndShowUI() { MovMsgJPanel mmp = new MovMsgJPanel(200, "My Message"); mmp.setPreferredSize(new Dimension(500, 100)); JFrame frame = new JFrame("Moving Message Panel Test"); frame.getContentPane().add(mmp); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } } class MovMsgJPanel extends javax.swing.JPanel { private String message; private int xCoordinate = 0; private int yCoordinate = 20; public MovMsgJPanel(int delay, String message) { this.message = message; Timer timer = new Timer(delay, new TimerListener()); timer.start(); } // paint message @Override public void paintComponent(java.awt.Graphics g) { super.paintComponent(g); if (message != null) { g.drawString(message, xCoordinate, yCoordinate); } } class TimerListener implements ActionListener { // handle ActionEvent public void actionPerformed(java.awt.event.ActionEvent e) { if (xCoordinate > getWidth()) { xCoordinate = -20; } xCoordinate += 5; repaint(); } } }
- 04-02-2011, 10:38 PM #8
Member
- Join Date
- Jul 2008
- Posts
- 7
- Rep Power
- 0
i have tested the sample program n it works great. could the problem be the positioning and size of the jpanel? if so please advice on best thing to do.
-
- 04-02-2011, 11:01 PM #10
Member
- Join Date
- Jul 2008
- Posts
- 7
- Rep Power
- 0
yes i have and found out that the panel is located on the top right corner of my main form and it is quite small in size. should i set the default size of the panel or just change the layout all together? how can i use the GridBagConstarints?
-
There is no one single simple answer to this since you use the layout(s) that will best display your components in the way you want them. Often it is better to use nested JPanels that use simple layout managers, but not saying not to use GridBagLayout where it is needed. Firstly you will need to decide what you want your GUI to look like, then play with your layout managers to get it right.
Similar Threads
-
Problem in action listener
By cool in forum AWT / SwingReplies: 4Last Post: 11-16-2010, 07:44 AM -
Simple Action/Listener Help GUI
By aanders5 in forum New To JavaReplies: 24Last Post: 10-18-2010, 06:43 PM -
Please help!!!! how to kill Timer listener?
By jackkyip in forum AWT / SwingReplies: 12Last Post: 06-29-2010, 06:05 PM -
Action Listener
By greatmajestics in forum AWT / SwingReplies: 8Last Post: 03-25-2010, 05:39 PM -
Action Listener? how to use this?
By jeffrey in forum New To JavaReplies: 2Last Post: 10-12-2009, 08:51 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks