Timers in swing

I encountered the following problem when using a Swing timer: it works fine when I use a delay time of 800, but the program exits without apparently doing anything when using delays close to 1000 or more. The program is as follows:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Traffic extends JFrame{
Timer timer;
Traffic(){
ActionListener a = new ActionListener (){
public void actionPerformed (ActionEvent e){
System.out.println("Hello World");
timer = new Timer(1800, a);
timer.start();
public static void main (String [] args)
new Traffic();
As stated with 1800 as the timer delay it does not work, with 800 it works fine.
As a beginner in java I would really like to know what I am doing wrong.
Thank you, Gert

Yes, it is exactly the code as I put it in. In fact I did it under Windows XP yesterday, on two different computers, and this morning I compiled it under Linux, with exactly the same result. I tried to leave out the Jframe as well, but then the program stops immediately, whereas when I put it in, it waits a while before stopping. If I try to debug it with jdb, and do
"run Traffic", I get the message "exit to system" , and the debugger stops.

Similar Messages

  • Troubles with Swing Timers

    I am trying to learn how to use javax.swing.timer. I'm simply trying to update a JLabel on an interval. The code I have looks like this:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    class GUI extends JFrame implements ActionListener
    Timer clock;
    JLabel label;
    int numberFrames;
    GUI()
         clock = new Timer(25, this);
         JLabel label = new JLabel("0");
         getContentPane().add(label, BorderLayout.NORTH);
         numberFrames = 0;
         clock.setInitialDelay(0);
         clock.setCoalesce(true);
         clock.start();
    public void actionPerformed(ActionEvent e)
         numberFrames++;
         label.setText("" + numberFrames);
    public class Timed
    public static void main(String argv[])
         GUI window = new GUI();
         window.setSize(100,100);
         window.setVisible(true);     
    Unfortuantely, the ActionPerformed executes only once and throws a NullPointerException whenever it tries to update the label. I also looked at Java's site for learning how to use Swing Timers, but I didn't understand what they were doing when they put in multiple actionListeners.
    Please Help.

    The problem is with variable scoping:
    GUI()
    clock = new Timer(25, this);
    JLabel label = new JLabel("0"); <<<<<<<<<<<<<DONT DECLARE A JLABEL HERE
    getContentPane().add(label, BorderLayout.NORTH);
    numberFrames = 0;
    clock.setInitialDelay(0);
    clock.setCoalesce(true);
    clock.start();
    The JLabel declaration in the constructor means the 'real' JLabel never get initialised, hence the NullPointerException

  • Swing timers

    Currently having difficultly with swing timers and getting it to update it's delay
    We have a GUI app that will display screens for differing amounts of time, each screen has it's own time limit. The timer times out at 4000 for each screen( timerArray[0] );
    //I create the timer
    delay = timerArray[0];
    timer = new Timer( delay, this );
    //start the timer in the start buttons action performed.
    timer.start();
    //reset the timer in an inner class that repaints the JPanel
    delay = timerArray[currentScreen];
    timer.setDelay(delay);
    // stop and reset the screen in actionPerformed for main window
    System.out.println( currentScreen+" "+timer.getDelay() );
    contentPane.add( new splashScreen() );
    timer.stop();
    And the print out is
    1 4000
    2 20000
    3 60000
    4 15000
    So why is it not changing it's time?

    Are you sure you have understand that the Timer works like this :
    <---initialDelay--->fire ActionEvent<---delay--->fire ActionEvent<---delay--->fire Action Event<--- ...
    while it is not stopped.
    for example if you want the Timer to count 4 seconds you set the initial delay to 1000 (millis) and the delay to 1000 also.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class SplashScreen extends JWindow {
         int duration = 0;
         int counter = 0;
         javax.swing.Timer timer;
         public SplashScreen() { 
              ImageIcon bg = new ImageIcon("image1.jpg");
              getContentPane().add(new JLabel(bg));
              pack();
              Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
              setLocation(screenSize.width/2 - getWidth()/2,
                              screenSize.height/2 - getHeight()/2);
              timer = new javax.swing.Timer(1000, new ActionListener() {
                   public void actionPerformed(ActionEvent e) {
                        counter++;
                        if (counter>=duration) {
                             closeSplashScreen();
         public void showSplashScreen(int seconds) {
              duration = seconds;
              counter = 0;
              setVisible(true);
              timer.start();
         public void closeSplashScreen() {
              if (timer.isRunning()) {
                   timer.stop();
              setVisible(false);
         public static void main(String[] args) {
              SplashScreen sp = new SplashScreen();
              sp.showSplashScreen(4);
    }------ unformatted version ----
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class SplashScreen extends JWindow {
         int duration = 0;
         int counter = 0;
         javax.swing.Timer timer;
    public SplashScreen() { 
              ImageIcon bg = new ImageIcon("image1.jpg");
              getContentPane().add(new JLabel(bg));
              pack();
              Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
              setLocation(screenSize.width/2 - getWidth()/2,
                             screenSize.height/2 - getHeight()/2);
              timer = new javax.swing.Timer(1000, new ActionListener() {
                   public void actionPerformed(ActionEvent e) {
                        counter++;
                        if (counter>=duration) {
                             closeSplashScreen();
         public void showSplashScreen(int seconds) {
              duration = seconds;
              counter = 0;
              setVisible(true);
              timer.start();
         public void closeSplashScreen() {
              if (timer.isRunning()) {
                   timer.stop();
              setVisible(false);
         public static void main(String[] args) {
              SplashScreen sp = new SplashScreen();
              sp.showSplashScreen(4);
    Hope this helps,
    Denis

  • Mulithreading 2 swing timers

    Hi, My first timer updation2 hangs and doesnt do anything. Looks like it is waiting for second thread to be completed or what ever. How can i multi thread these two ?
    public javax.swing.Timer updation2=new javax.swing.Timer(1000,new ActionListener()
              public void actionPerformed(ActionEvent evt)
                   //synchronized(this)
                        try
                             o_val2 = tabModel.getValueAt(indx,2);
                             rate = ((Integer)(o_val2)) - ((Integer)(o_val1));
                             //rate=0;
                             framing.repaint();
                        catch(Exception eee)
         public javax.swing.Timer updation=new javax.swing.Timer(1000,new ActionListener()
              public void actionPerformed(ActionEvent evt)
                   //synchronized(this)
                        try
                             jp.setString(String.valueOf(pack_siz + " Packets/sec"));
                             jp.setValue(pack_siz);                         
                             //JOptionPane.showMessageDialog(null,String.valueOf((Integer)(o_val2)) + "--" + String.valueOf((Integer)(o_val1)));
                             //framing.setTitle(String.valueOf((Integer)(o_val2)) + "--" + String.valueOf((Integer)(o_val1)));
                             //Setting Piegraph values here as it changes
                             long[] data1 = {100,arp_rate,udp_rate};                         
                             l.changeValue(data1);
                             try
                                  if(iptf.getText().length()==0)
                                       if(jp.getValue()==100)          
                                            jp.setForeground(Color.RED);
                                            jp.setString("Above 100 packets/sec");
                                            Toolkit.getDefaultToolkit().beep();
                                       else if(jp.getValue()<100)
                                            jp.setForeground(Color.green);
                                  else if(iptf.getText().length()!=0)
                                       prog_set = Integer.valueOf(iptf.getText());
                                       if(prog_set<=100)
                                            if(jp.getValue()>=prog_set)          
                                                 jp.setForeground(Color.RED);
                                                 jp.setString("Above "+prog_set+" packets/sec");
                                                 Toolkit.getDefaultToolkit().beep();
                                            else if(jp.getValue()<=prog_set)
                                                 jp.setForeground(Color.green);
                                       else if(prog_set>=100)
                                            JOptionPane.showMessageDialog(null,"Value must be between 0 - 100");
                                            iptf.setText("100");
                                  catch(Exception eee)
                                            if(iptf.getText().length()==0)
                                            else
                                                 JOptionPane.showMessageDialog(null,"Alarm value should be numeric");
                                                 iptf.setText("");
                             pBar2.setValue(rate);
                              pBar2.setString(rate + " Packets");          
                             f.repaint();
                             pack_siz=0;
                             arp_rate=0;
                             udp_rate=0;
                             framing.repaint();
                        catch(Exception ee)
         });

    Why would you do that? Just do this...
    javax.swing.Timer = new javax.swing.Timer(1000, this);where your class implements ActionListener (so you can have it--this--as your Listener for your Timer)
    and do the following in your actionPerformed
    public void actionPerformed(ActionEvent e){
      myThread1.myStuff();
      myThread2.myOtherStuff();
    };[How to use Swing Timers|http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html]

  • Swing Timer issues on a faster machine.

    Hi everyone,
    I'm having some trouble using Swing Timers. I have built a system which uses three javax.swing.Timers, these are as follows:
    timerLeft = new Timer(timerDelay, new ActionListener() {
    public void actionPerformed(ActionEvent evt)
    {if(pathPointerLeft==leftCyclePoints.length) {
    pathPointerLeft=0;
    leftHandX=leftCyclePoints[pathPointerLeft][0];
    leftHandY=leftCyclePoints[pathPointerLeft][1];
    pathPointerLeft++;
    jugglingPanel.repaint();
    timerRight = new Timer(timerDelay, new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
    if(pathPointerRight==rightCyclePoints.length) {
    pathPointerRight=0;
    rightHandX=rightCyclePoints[pathPointerRight][0];
    rightHandY=rightCyclePoints[pathPointerRight][1];
    pathPointerRight++;
    jugglingPanel.repaint();
    myApp.timerBalls = new Timer(myApp.timerDelay, new ActionListener() {
    public void actionPerformed(ActionEvent evt){
    for(int i=0;i<myApp.ballCycleVector.capacity();i++){
    myApp.ballPoints = (int[][])myApp.ballCycleVector.elementAt(i);
    if(myApp.startAnimatingBall){
    if(myApp.ballPoints[myApp.pathPointerBalls][2]==1 && myApp.transitionDone){
    for(int j=0;j<200;j++) {
    if(myApp.ballPoints[myApp.pathPointerBalls][2]!=1) {
    break;
    myApp.pathPointerBalls++;
    if(lastLocationX==myApp.ballPoints[myApp.pathPointerBalls][0] && lastLocationY==myApp.ballPoints[myApp.pathPointerBalls][1]) {
    myApp.pathPointerBalls++;
    if(myApp.pathPointerBalls!=myApp.ballPoints.length) {
    myApp.ballLocations[0]=myApp.ballPoints[myApp.pathPointerBalls][0];
    myApp.ballLocations[1]=myApp.ballPoints[myApp.pathPointerBalls][1];
    lastLocationX= myApp.ballPoints[myApp.pathPointerBalls][0];
    lastLocationY = myApp.ballPoints[myApp.pathPointerBalls][1];
    } else{
    myApp.pathPointerBalls=0;
    for(int j=0;j<200;j++) {
    if(myApp.ballPoints[myApp.pathPointerBalls][2]!=1) {
    break;
    myApp.pathPointerBalls++;
    if(myApp.pathPointerBalls!=0 && myApp.ballPoints[myApp.pathPointerBalls-1][2]==1) {
    myApp.pathPointerBalls++;
    myApp.ballLocations[0]=myApp.ballPoints[myApp.pathPointerBalls][0];
    myApp.ballLocations[1]=myApp.ballPoints[myApp.pathPointerBalls][1];
    lastLocationX= myApp.ballPoints[myApp.pathPointerBalls][0];
    lastLocationY = myApp.ballPoints[myApp.pathPointerBalls][1];
    myApp.pathPointerBalls++;
    if(myApp.pathPointerBalls==myApp.ballPoints.length) {
    myApp.pathPointerBalls=0;
    if(myApp.pathPointerBalls!=0 && myApp.ballPoints[myApp.pathPointerBalls][2]==0 && myApp.ballPoints[myApp.pathPointerBalls-1][2]==1) {
    myApp.transitionDone=true;
    myApp.jugglingPanel.repaint();
    Apologies if these do not coform to standard coding conventions. When I run these on my home PC (P4 2.56GHz, 512MB RAM, 64MB Graphics) it runs beautifully. The same on a laptop (P3 1.8GHz, 512MB RAM, 64MB Graphics card). However, when I run it on the campus machines (3.20GHz, 1.5GB RAM, 128MB Graphics), the animated display (as controlled by the Timers above) stutters and does not remain in sync. I'm guessing that this is because it is being ran on a faster machine. Could anyone suggest a solution to this problem so that the animation remains in sync for a faster machine, or even suggest another reason why this problem is occuring. This is a very important project and I really need to resolve this issue. Thanks in advance.

    The code you posted means nothing:
    a) it is not readable, since you didn't use the "Code Formatting Tags" when you posted the code
    b) its not executable, so we can't see what it is attempting to do.
    c) we don't know the frequency of event firing
    Obviously a faster machine will be able to repaint Components faster then slower machines.
    I use a 3.3Ghz machine and my TimerAnimation example here seems to keep in sync:
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=631379

  • Problem with timers in a udp state pattern

    Hi.
    As a project on my school, i have created a bomberman game, that supports up to 4 players on a network. And it works fine, when there is no network timers involved, that means no packet loss handling.
    The network part of the game, is build up by UDP, to make the application support multicast. The game session flow, is build up by states which are designed like an extended 2 phase commit pattern.
    That means, the server sends Gameinfo to the clients. Then all the cliens replies with an ACK.
    The server then sends ExecuteGameinfo. Then the clients replies with an ACK.
    The server sends/requests playerinfo, and the clients reply with player info. (the server uses the player info to create gameinfo).
    These 3 states, continues to go on, as long as the game is running. And without timers, it is working great.
    But what if a packet is lost? (on a LAN, packetloss is very rare, but for the sake of my project�s issue, custom made reliability in UDP, I have to face the problem). Then the server would just hang, and wait for the client reply.
    To avoid that, i want to use a timer. If the server havent got any reply for like a 100 ms., the server should resend the package.
    And now to the actual problem :)
    Ive tried using java.util.Timer, and javax.swing.Timer. But both timers does not work in my case. Its like:
    Server starts timer.
    Server sends Gameinfo.
    100 ms passes.
    Server resends Gameinfo.
    Server restarts timer.
    100 ms passes.
    (this goes on for 5 times, and then the client is kicked)
    Server receives ACK.
    Server receives ACK.
    Server receives ACK.
    Server receives ACK.
    Server receives ACK.
    On the client side it looks like this:
    Recieves Gameinfo, replies with ACK
    Recieves Gameinfo, replies with ACK
    x 5
    So its like the server is to concentrated on that timer (even though it runs as a thread), so i doesent notice the incoming packets, that eventually stacks up, and is handled after the timer tasks are done.
    And this is exactly the problem that is described in the Timer api: " Timer tasks should complete quickly. If a timer task takes excessive time to complete, it "hogs" the timer's task execution thread. This can, in turn, delay the execution of subsequent tasks, which may "bunch up" and execute in rapid succession when (and if) the offending task finally completes."
    Ive tried using a homemade timer, with a Thread.sleep(), it is a bit better, but the problem is the same. 1 out of 20 incoming packets, is "ignored" by the server.
    I really hope any of you can help.
    I havent included any code, it would be to much code. But if you are interested in looking at something i can easily paste it.
    Thank you very much for your time :)
    Regards.

    Don't use a Timer, use DatagramSocket.setSoTimeout(). Then your receives will only block for that amount of time, throwing SocketTimeoutException if no datagram arrives.

  • Swing application eats CPU on resume after standby

    My application contains a mix of regular Swing components and about 200 (small) custom components (JPanels) that draw themselves. The problem is that if put my laptop into Standby, then restart it, the application eats CPU. It goes from ~4% pre standby to ~48% after standby. Totally repeatable. There is only the main thread, the AWT thread and a couple timers firing: one to get data from a server and one to redraw the custom controls to make the control blink (calls repaint() to cue up a call to paintComponent()). I put timing code in the two timer driven threads and both are running fine. NOTE: each of the custom components is just a rectangle with a short text string in it. Think checkerboard with text. The background color is all that is being changed. It's basically a grid of alarm/status "lights".
    I did some searching and only found a reference to a drawing bug that was fixed back in 1.4.x that had to do with WinXP and dual-headed computers.
    Note that another simple Swing application I wrote (no custom controls), doesn't appear to behave this way, so I suspect it has to do with the custom drawing stuff (rolling my own paintComponent() method).
    Any thoughts on how to figure out what is eating the CPU? I'm using NetBeans 6.7 and JDK 1.6_14 under Windows XP.
    Edited by: garrysimmons on Sep 15, 2009 1:52 PM
    Edited by: garrysimmons on Sep 15, 2009 2:57 PM
    Edited by: garrysimmons on Sep 15, 2009 3:02 PM

    It doesn't matter if it's a Swing timer or a Util timer. It behaves the same either way. That was one of the first things I tried.
    I ended up refactoring the code to go from a grid of individual controls (think checker board where each square is a control that draws itself), to a single "board" control that draws a grid. That seems to have fixed the issue. I can go in/out of Standby and the app doesn't eat a ton of CPU any more. The real problem may still be there, just too small of an effect to notice.
    I don't know why the original design would work fine before a Standby, but not after. Having ~200 little controls doing repaints twice a second must confuse something in the JVM/Windows versus having 4 large controls doing the same amount of drawing.
    It's working, so time to move on.

  • What is the flaw in this simple Swing program ?

    Hi all ,
    I tried one simple animation in Swing .
    Here's what the program has to do :
    when a button called "play" is clicked a circle should be moved from upper left corner down to the lower right corner.
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    public class MyAnimation extends JFrame {
       int width;
       int height;
       MyAnimation animation ;
       MyDrawPanel drawpanel ;
       public static void main(String[] args) {
         MyAnimation gui=new MyAnimation();
         gui.go();
       } //close main()
       public void go() {
           animation = new MyAnimation();
           JButton button = new JButton("Play");
           button.addActionListener(new ButtonListener());
           drawpanel = new MyDrawPanel();
           animation.setTitle("Ball Animation");
           animation.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           animation.getContentPane().add(BorderLayout.SOUTH,button);
           animation.getContentPane().add(BorderLayout.CENTER,drawpanel);
           animation.setSize(300,300);
           animation.setVisible(true);
       } //close go()
       class ButtonListener implements ActionListener {
           public void actionPerformed(ActionEvent ae) {
              for(width=0,height=0;width<130&&height<130;width++,height++) {
                       System.out.println("before calling");
                       animation.repaint();
                       System.out.println("W ="+width+"H ="+height);
                   try {
                       Thread.sleep(50);
                   }catch(Exception ex) { }
             } //close for
        } //close actionPerformed()
    } //close ButtonListener
          class MyDrawPanel extends JPanel {
                public void paintComponent(Graphics g) {
                   g.setColor(Color.blue);
                   g.fillRect(0,0,this.getWidth(),this.getHeight());
                   int w= this.getWidth();
                   int h = this.getHeight();
                   //System.out.print(w,h);
                   g.setColor(Color.green);
                   g.fillOval(width,height,40,40);
                   System.out.println("paintcomponent");
               } //close paintcomponent()
         } //closeMyDrawPanel()
    } //close MyAnimation()I noticed when a button is clicked everything goes fine(code reached Listener and loop is executed) expect that repaint() method is not invoked with in a loop(invoked only once at the end of loop).
    but if I embed the same loop code with in go() method without a Listener class it's works Fine !
    Why it's not happening when the code is within Litener class?
    Two more additional questions :
    What is the difference between calling the repaint() method with JFrame instance and JPanel instance ?
    In that program why System.out.print() method which is in MyDrawPanel class can't be invoked ?
    Thanks in advance

    Axel_Richter wrote:
    May be you could have a chance to animate without threads by using paintImmediately directly. I have never tried this, because to use threads is not so difficult at all.animation without explicit calls to thread:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    public class MyAnimation extends JPanel
        private static final int TIME_PERIOD = 10;
        private static final String PLAY = "Play";
        private static final String PAUSE = "Pause";
        private static final String RESET = "Reset";
        int width = 0;
        int height = 0;
        MyDrawPanel drawpanel = new MyDrawPanel();
        private Timer timer = new Timer(TIME_PERIOD, new TimerAL());
        public MyAnimation()
            super(new BorderLayout());
            setPreferredSize(new Dimension(300, 300));
            JButton button = new JButton("Play");
            button.addActionListener(new ButtonListener());
            add(button, BorderLayout.SOUTH);
            add(drawpanel, BorderLayout.CENTER);
        class ButtonListener implements ActionListener
            public void actionPerformed(ActionEvent event)
                String command = event.getActionCommand();
                if (command.equals(PLAY))
                    timer.start();
                    ((JButton)event.getSource()).setText(PAUSE);
                else if (command.equals(PAUSE))
                    timer.stop();
                    ((JButton)event.getSource()).setText(RESET);
                else if (command.equals(RESET))
                    timer.stop();
                    ((JButton)event.getSource()).setText(PLAY);
                    width = 0;
                    height = 0;
                    repaint();
        class TimerAL implements ActionListener
            public void actionPerformed(ActionEvent arg0)
                if (width < (drawpanel.getWidth() - 40) &&
                        height < (drawpanel.getHeight() - 40))
                    width++;
                    height++;
                    drawpanel.repaint();
                else
                    timer.stop();
        class MyDrawPanel extends JPanel
            public void paintComponent(Graphics g)
                g.setColor(Color.blue);
                g.fillRect(0, 0, this.getWidth(), this.getHeight());
                int w = this.getWidth();
                int h = this.getHeight();
                // System.out.print(w,h);
                Graphics2D g2 = (Graphics2D)g;
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                    RenderingHints.VALUE_ANTIALIAS_ON);
                g2.setColor(Color.green);
                g2.fillOval(width, height, 40, 40);
        private static void createAndShowGUI()
            JFrame frame = new JFrame("Ball Animation");
            frame.getContentPane().add(new MyAnimation());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        public static void main(String[] args)
            SwingUtilities.invokeLater(new Runnable()
                public void run()
                    createAndShowGUI();
    }

  • Timers

    ok, call me dumb or whatever but i just don't understand timers.
    how would i make a timer to fire every second and add 1 to an integer?
    I've tryed to understand it but there are swing timers and regular timers? and i keep getting confused and all the tuts i keep finding are just so confusing. I'm brand new to java so try not to use too many fancy things if you feel like helping.
    oh, and sorry if i'm in the wrong Forum

    Perhaps a working example would help.
    import java.util.*;
    public class TimerExample {
         public static void main(String[] args) {
              Timer timer = new Timer("Test Timer");
              timer.schedule(new TimerTask() {
                   private int counter = 0;
                   public void run() {
                        counter++;
                        System.out.println(new Date() + ": Counter=" + counter);
                        if (counter >= 10) {
                             System.out.println("Finished.");
                             System.exit(0);
              }, 0, 1000);
    }Prints
    Thu Oct 13 22:18:28 BST 2005: Counter=1
    Thu Oct 13 22:18:29 BST 2005: Counter=2
    Thu Oct 13 22:18:30 BST 2005: Counter=3
    Thu Oct 13 22:18:31 BST 2005: Counter=4
    Thu Oct 13 22:18:32 BST 2005: Counter=5
    Thu Oct 13 22:18:33 BST 2005: Counter=6
    Thu Oct 13 22:18:34 BST 2005: Counter=7
    Thu Oct 13 22:18:35 BST 2005: Counter=8
    Thu Oct 13 22:18:36 BST 2005: Counter=9
    Thu Oct 13 22:18:37 BST 2005: Counter=10
    Finished.BTW: If you are New to Java, in future, I suggest you post to the "New To Java" forum.
    This doesn't mean post this question again as this will only annoy people.

  • Memory leak with 1.6.0_07 in applet using Swing

    Java Plug-in 1.6.0_07
    Using JRE version 1.6.0_07 Java HotSpot(TM) Client VM
    Windows XP - SP2
    I have a commercial application that has developed a memory leak with the introduction of the latest plugin. The applets chew up memory and eventually freeze. They did not before. Using jvisualm I see a build up of native arrays, primarily int[][] and char[]. I'm still investigating. Anyone have a similar experience?
    The Applet uses a swing interface, uses buffered images and swing timers, and regularly performs http connections to the server which result in actions via the SwingUtil.invokeLater() method.

    I am Using Internet Explorer Browser Version 6.0.Huge security hole.
    Its not throwing Error / Exception Wrap a try/catch at the highest level possible.
    Catch 'Throwable'. And log/display it somewhere.

  • Troubles with Timers

    Hello all,
    I am trying to run a GUI here that resembles a map. The data for the map (i.e. the speeds on each road) are being inputted from a text file.
    These are problems that I am having:
    1) I want the GUI to be updated each five seconds showing the latest 'road conditions' from my text file via progress bars. Meaning I want the map to refresh with new data from my text file taking 24 data numbers each five seconds. So after five seconds, get the next 24 data numbers from the text file, etc. I tried using timers to do this but cannot figure out why they don't work. I have pasted my code w/out my timer coding below. If anyone could help that would be greatly appreciated as I have been stuck on this for days. Even pointing me in the right direction as to how to use timers, actionlisteners,actionperformers would be great. I have gone to many many websites trying to figure it out but cannot seem to do it.
    Thanks again!
    package fourthyear;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    * <p>Title: </p>
    * <p>Description: </p>
    * <p>Copyright: Copyright (c) 2005</p>
    * <p>Company: </p>
    * @author not attributable
    * @version 1.0
    public class Frame1 extends JFrame {
    JPanel contentPane;
    BorderLayout borderLayout1 = new BorderLayout();
    JPanel jPanel1 = new JPanel();
    JRadioButton jRadioButton1 = new JRadioButton();
    JProgressBar jProgressBar1 = new JProgressBar();
    JRadioButton jRadioButton2 = new JRadioButton();
    JRadioButton jRadioButton3 = new JRadioButton();
    JProgressBar jProgressBar2 = new JProgressBar();
    JProgressBar jProgressBar3 = new JProgressBar();
    JRadioButton jRadioButton4 = new JRadioButton();
    JProgressBar jProgressBar4 = new JProgressBar();
    JRadioButton jRadioButton5 = new JRadioButton();
    JProgressBar jProgressBar5 = new JProgressBar();
    JProgressBar jProgressBar6 = new JProgressBar();
    JProgressBar jProgressBar7 = new JProgressBar();
    JRadioButton jRadioButton6 = new JRadioButton();
    JButton jButton1 = new JButton();
    JLabel jLabel1 = new JLabel();
    JLabel jLabel2 = new JLabel();
    JLabel jLabel3 = new JLabel();
    JLabel jLabel4 = new JLabel();
    JRadioButton jRadioButton7 = new JRadioButton();
    JLabel jLabel5 = new JLabel();
    JProgressBar jProgressBar8 = new JProgressBar();
    JProgressBar jProgressBar9 = new JProgressBar();
    JLabel jLabel7 = new JLabel();
    JProgressBar jProgressBar10 = new JProgressBar();
    JProgressBar jProgressBar11 = new JProgressBar();
    JRadioButton jRadioButton10 = new JRadioButton();
    JRadioButton jRadioButton12 = new JRadioButton();
    JProgressBar jProgressBar14 = new JProgressBar();
    JRadioButton jRadioButton11 = new JRadioButton();
    JProgressBar jProgressBar12 = new JProgressBar();
    JLabel jLabel8 = new JLabel();
    JProgressBar jProgressBar13 = new JProgressBar();
    JProgressBar jProgressBar15 = new JProgressBar();
    JRadioButton jRadioButton8 = new JRadioButton();
    JLabel jLabel6 = new JLabel();
    JRadioButton jRadioButton9 = new JRadioButton();
    JProgressBar jProgressBar16 = new JProgressBar();
    JProgressBar jProgressBar17 = new JProgressBar();
    JLabel jLabel9 = new JLabel();
    JProgressBar jProgressBar18 = new JProgressBar();
    JRadioButton jRadioButton13 = new JRadioButton();
    JProgressBar jProgressBar19 = new JProgressBar();
    JRadioButton jRadioButton14 = new JRadioButton();
    JLabel jLabel10 = new JLabel();
    JRadioButton jRadioButton16 = new JRadioButton();
    JLabel jLabel11 = new JLabel();
    JLabel jLabel12 = new JLabel();
    JRadioButton jRadioButton17 = new JRadioButton();
    JLabel jLabel13 = new JLabel();
    JLabel jLabel14 = new JLabel();
    JLabel jLabel15 = new JLabel();
    JLabel jLabel16 = new JLabel();
    JLabel jLabel17 = new JLabel();
    JLabel jLabel18 = new JLabel();
    JLabel jLabel19 = new JLabel();
    JProgressBar jProgressBar20 = new JProgressBar();
    JProgressBar jProgressBar21 = new JProgressBar();
    JProgressBar jProgressBar22 = new JProgressBar();
    JProgressBar jProgressBar23 = new JProgressBar();
    JProgressBar jProgressBar24 = new JProgressBar();
    JLabel jLabel20 = new JLabel();
    JLabel jLabel21 = new JLabel();
    JLabel jLabel22 = new JLabel();
    JLabel jLabel23 = new JLabel();
    JLabel jLabel24 = new JLabel();
    //Construct the frame
    public Frame1() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
    jbInit();
    catch(Exception e) {
    e.printStackTrace();
    //Component initialization
    private void jbInit() throws Exception {
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(borderLayout1);
    this.setSize(new Dimension(601, 603));
    this.setTitle("Map View");
    jPanel1.setBackground(Color.white);
    jPanel1.setForeground(Color.black);
    jPanel1.setBorder(BorderFactory.createLineBorder(Color.black));
    jPanel1.setDebugGraphicsOptions(0);
    jPanel1.setLayout(null);
    jRadioButton1.setText("");
    jRadioButton1.setBounds(new Rectangle(33, 27, 20, 22));
    jProgressBar1.setBackground(Color.lightGray);
    jProgressBar1.setBounds(new Rectangle(73, 26, 96, 23));
    jRadioButton2.setBounds(new Rectangle(191, 26, 20, 22));
    jRadioButton2.setText("");
    jRadioButton3.setBounds(new Rectangle(348, 27, 20, 22));
    jRadioButton3.setText("");
    jProgressBar2.setBounds(new Rectangle(238, 28, 96, 23));
    jProgressBar3.setOrientation(JProgressBar.HORIZONTAL);
    jProgressBar3.setBounds(new Rectangle(389, 26, 100, 22));
    jRadioButton4.setBounds(new Rectangle(33, 179, 20, 22));
    jRadioButton4.setText("");
    jProgressBar4.setOrientation(JProgressBar.VERTICAL);
    jProgressBar4.setBounds(new Rectangle(33, 68, 25, 99));
    jRadioButton5.setText("");
    jRadioButton5.setBounds(new Rectangle(193, 178, 20, 22));
    jProgressBar5.setOrientation(JProgressBar.VERTICAL);
    jProgressBar5.setBounds(new Rectangle(192, 66, 21, 98));
    jProgressBar6.setOrientation(JProgressBar.VERTICAL);
    jProgressBar6.setBounds(new Rectangle(351, 67, 21, 98));
    jProgressBar7.setOrientation(JProgressBar.VERTICAL);
    jProgressBar7.setBounds(new Rectangle(502, 64, 22, 99));
    jRadioButton6.setBounds(new Rectangle(351, 176, 20, 22));
    jRadioButton6.setText("");
    jButton1.setBounds(new Rectangle(238, 521, 97, 33));
    jButton1.setText("Compute");
    jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
    jLabel1.setBounds(new Rectangle(96, 4, 42, 20));
    jLabel2.setBounds(new Rectangle(263, 5, 42, 20));
    jLabel3.setText("");
    jLabel3.setBounds(new Rectangle(416, 3, 42, 20));
    jLabel4.setText("");
    jLabel4.setBounds(new Rectangle(66, 107, 42, 20));
    jRadioButton7.setText("");
    jRadioButton7.setBounds(new Rectangle(33, 323, 20, 22));
    jLabel5.setText("");
    jLabel5.setBounds(new Rectangle(223, 101, 42, 20));
    jProgressBar8.setBounds(new Rectangle(75, 180, 96, 23));
    jProgressBar9.setBounds(new Rectangle(239, 177, 96, 23));
    jLabel7.setText("");
    jLabel7.setBounds(new Rectangle(531, 103, 42, 20));
    jProgressBar10.setOrientation(JProgressBar.HORIZONTAL);
    jProgressBar10.setBounds(new Rectangle(390, 177, 98, 22));
    jProgressBar11.setOrientation(JProgressBar.VERTICAL);
    jProgressBar11.setBounds(new Rectangle(33, 209, 21, 98));
    jRadioButton10.setText("");
    jRadioButton10.setBounds(new Rectangle(351, 320, 20, 22));
    jRadioButton12.setBounds(new Rectangle(193, 322, 20, 22));
    jRadioButton12.setText("");
    jProgressBar14.setOrientation(JProgressBar.VERTICAL);
    jProgressBar14.setBounds(new Rectangle(503, 211, 21, 98));
    jRadioButton11.setBounds(new Rectangle(503, 319, 20, 22));
    jRadioButton11.setText("");
    jProgressBar12.setOrientation(JProgressBar.VERTICAL);
    jProgressBar12.setBounds(new Rectangle(192, 209, 22, 99));
    jLabel8.setText("");
    jLabel8.setBounds(new Rectangle(98, 152, 42, 20));
    jProgressBar13.setOrientation(JProgressBar.VERTICAL);
    jProgressBar13.setBounds(new Rectangle(351, 211, 21, 99));
    jProgressBar15.setOrientation(JProgressBar.HORIZONTAL);
    jProgressBar15.setBounds(new Rectangle(72, 320, 97, 24));
    jRadioButton8.setText("");
    jRadioButton8.setBounds(new Rectangle(503, 175, 20, 22));
    jLabel6.setText("");
    jLabel6.setBounds(new Rectangle(382, 100, 42, 20));
    jRadioButton9.setText("");
    jRadioButton9.setBounds(new Rectangle(500, 26, 20, 22));
    jProgressBar16.setBounds(new Rectangle(237, 321, 96, 23));
    jProgressBar17.setOrientation(JProgressBar.HORIZONTAL);
    jProgressBar17.setBounds(new Rectangle(382, 320, 114, 22));
    jLabel9.setText("");
    jLabel9.setBounds(new Rectangle(260, 150, 42, 20));
    jProgressBar18.setOrientation(JProgressBar.VERTICAL);
    jProgressBar18.setBounds(new Rectangle(32, 362, 21, 98));
    jRadioButton13.setBounds(new Rectangle(35, 477, 20, 22));
    jRadioButton13.setText("");
    jProgressBar19.setOrientation(JProgressBar.VERTICAL);
    jProgressBar19.setBounds(new Rectangle(192, 359, 23, 104));
    jRadioButton14.setText("");
    jRadioButton14.setBounds(new Rectangle(195, 476, 20, 22));
    jLabel10.setText("");
    jLabel10.setBounds(new Rectangle(417, 148, 42, 20));
    jRadioButton16.setBounds(new Rectangle(352, 476, 20, 22));
    jRadioButton16.setText("");
    jLabel11.setText("");
    jLabel11.setBounds(new Rectangle(59, 247, 42, 20));
    jLabel12.setText("");
    jLabel12.setBounds(new Rectangle(224, 246, 42, 20));
    jRadioButton17.setText("");
    jRadioButton17.setBounds(new Rectangle(506, 476, 20, 22));
    jLabel13.setText("");
    jLabel13.setBounds(new Rectangle(383, 249, 42, 20));
    jLabel14.setText("");
    jLabel14.setBounds(new Rectangle(528, 242, 42, 20));
    jLabel15.setText("");
    jLabel15.setBounds(new Rectangle(100, 290, 42, 20));
    jLabel16.setToolTipText("");
    jLabel16.setText("");
    jLabel16.setBounds(new Rectangle(263, 290, 42, 20));
    jLabel17.setText("");
    jLabel17.setBounds(new Rectangle(417, 283, 42, 20));
    jLabel18.setText("");
    jLabel18.setBounds(new Rectangle(62, 402, 42, 20));
    jLabel19.setText("");
    jLabel19.setBounds(new Rectangle(227, 399, 42, 20));
    jProgressBar20.setOrientation(JProgressBar.VERTICAL);
    jProgressBar20.setBounds(new Rectangle(350, 356, 24, 107));
    jProgressBar21.setOrientation(JProgressBar.VERTICAL);
    jProgressBar21.setBounds(new Rectangle(504, 357, 23, 103));
    jProgressBar22.setOrientation(JProgressBar.HORIZONTAL);
    jProgressBar22.setBounds(new Rectangle(73, 477, 102, 23));
    jProgressBar23.setBounds(new Rectangle(233, 475, 104, 25));
    jProgressBar24.setBounds(new Rectangle(384, 475, 116, 24));
    jLabel20.setText("");
    jLabel20.setBounds(new Rectangle(383, 397, 41, 23));
    jLabel21.setText("");
    jLabel21.setBounds(new Rectangle(533, 398, 41, 23));
    jLabel22.setText("");
    jLabel22.setBounds(new Rectangle(99, 450, 41, 23));
    jLabel23.setText("");
    jLabel23.setBounds(new Rectangle(262, 444, 41, 23));
    jLabel24.setText("");
    jLabel24.setBounds(new Rectangle(420, 445, 41, 23));
    jPanel1.add(jProgressBar6, null);
    jPanel1.add(jProgressBar5, null);
    jPanel1.add(jRadioButton3, null);
    jPanel1.add(jProgressBar2, null);
    jPanel1.add(jRadioButton2, null);
    jPanel1.add(jRadioButton6, null);
    jPanel1.add(jRadioButton5, null);
    jPanel1.add(jProgressBar1, null);
    jPanel1.add(jRadioButton1, null);
    jPanel1.add(jRadioButton4, null);
    jPanel1.add(jLabel2, null);
    jPanel1.add(jProgressBar11, null);
    jPanel1.add(jRadioButton7, null);
    jPanel1.add(jRadioButton12, null);
    jPanel1.add(jRadioButton10, null);
    jPanel1.add(jRadioButton11, null);
    jPanel1.add(jRadioButton9, null);
    jPanel1.add(jRadioButton8, null);
    jPanel1.add(jRadioButton13, null);
    jPanel1.add(jRadioButton14, null);
    jPanel1.add(jRadioButton16, null);
    jPanel1.add(jRadioButton17, null);
    jPanel1.add(jLabel1, null);
    jPanel1.add(jLabel5, null);
    jPanel1.add(jLabel6, null);
    jPanel1.add(jLabel11, null);
    jPanel1.add(jProgressBar18, null);
    jPanel1.add(jLabel18, null);
    jPanel1.add(jButton1, null);
    jPanel1.add(jProgressBar3, null);
    jPanel1.add(jLabel3, null);
    jPanel1.add(jProgressBar4, null);
    jPanel1.add(jLabel4, null);
    jPanel1.add(jProgressBar7, null);
    jPanel1.add(jLabel7, null);
    jPanel1.add(jLabel8, null);
    jPanel1.add(jProgressBar8, null);
    jPanel1.add(jLabel9, null);
    jPanel1.add(jProgressBar9, null);
    jPanel1.add(jProgressBar10, null);
    jPanel1.add(jLabel10, null);
    jPanel1.add(jProgressBar12, null);
    jPanel1.add(jLabel12, null);
    jPanel1.add(jProgressBar13, null);
    jPanel1.add(jLabel13, null);
    jPanel1.add(jProgressBar14, null);
    jPanel1.add(jLabel14, null);
    jPanel1.add(jProgressBar15, null);
    jPanel1.add(jLabel15, null);
    jPanel1.add(jProgressBar16, null);
    jPanel1.add(jLabel16, null);
    jPanel1.add(jProgressBar17, null);
    jPanel1.add(jLabel17, null);
    jPanel1.add(jProgressBar19, null);
    jPanel1.add(jLabel19, null);
    jPanel1.add(jProgressBar20, null);
    jPanel1.add(jProgressBar21, null);
    jPanel1.add(jLabel21, null);
    jPanel1.add(jProgressBar22, null);
    jPanel1.add(jProgressBar23, null);
    jPanel1.add(jLabel23, null);
    jPanel1.add(jProgressBar24, null);
    jPanel1.add(jLabel24, null);
    jPanel1.add(jLabel22, null);
    jPanel1.add(jLabel20, null);
    contentPane.add(jPanel1, BorderLayout.CENTER);
    //display_speed();
    //Overridden so we can exit when window is closed
    protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
    System.exit(0);
    void jButton1_actionPerformed(ActionEvent e) throws IOException{
    String [] a = new String[24]; // defines the string array to intiall hold text elements
    int[] b = new int[24]; // defines the final array to hold road speeds
    int count_cycles = 0; // defines what set number where each set contains 24 elements
    int count_TwoFour = 1; // defines if 24 is counted
    int sum=0; // defines the sum element
    BufferedReader input;
    input = new BufferedReader (new FileReader("C:\\Test.txt"));
    String line = input.readLine();
    while (line != null)
    count_cycles++;
    for (int i=0; i<24; i++){
    a[i] = line;
    line = input.readLine();
    int d = Integer.parseInt(a);
    b[i]=d; // the actual double stored values of SPEED
    count_TwoFour++;
    if (count_TwoFour == 25){
    JProgressBar[] bars = {
    jProgressBar1, jProgressBar2, jProgressBar3, jProgressBar4,
    jProgressBar5, jProgressBar6, jProgressBar7, jProgressBar8,
    jProgressBar9, jProgressBar10, jProgressBar11, jProgressBar12,
    jProgressBar13, jProgressBar14, jProgressBar15, jProgressBar16,
    jProgressBar17, jProgressBar18, jProgressBar19, jProgressBar20,
    jProgressBar21, jProgressBar22, jProgressBar23, jProgressBar24};
    JLabel[] labels = {
    jLabel1, jLabel2, jLabel3, jLabel4, jLabel5, jLabel6, jLabel7,
    jLabel8, jLabel9, jLabel10, jLabel11, jLabel12, jLabel13,
    jLabel14, jLabel15, jLabel16, jLabel17, jLabel18, jLabel19,
    jLabel20, jLabel21, jLabel22, jLabel23, jLabel24};
    int num_roads = b.length;
    for (int increment = 0; increment < num_roads; increment++) {
    labels[increment].setText("");
    bars[increment].setMaximum(60);
    bars[increment].setValue(b[increment]);
    String speedlabel = String.valueOf(b[increment]);
    labels[increment].setText(speedlabel.concat("km/h"));
    labels[increment].setVisible(true);
    labels[increment].updateUI();
    jPanel1.updateUI();
    try{
    Thread.sleep(500); //1000 millisecs
    catch(InterruptedException ex) {
    count_TwoFour = 1;
    class Frame1_jButton1_actionAdapter implements java.awt.event.ActionListener {
    Frame1 adaptee;
    Frame1_jButton1_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
    public void actionPerformed(ActionEvent e) {
    try{adaptee.jButton1_actionPerformed(e);}
    catch (Exception ex){}

    Jonathan if you haven't seen it then this tutorial will probably be most helpful to you http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html
    It's part of a collection of fairly nice tutorials available on this very site which you can get to here http://java.sun.com/docs/books/tutorial/index.html
    In future if you want to post code here please look at how to get it to format nicely. http://forum.java.sun.com/help.jspa?sec=formatting
    Have a happy day.

  • How to kill all swing.Timer(s) at end of App

    I am using a javax.swing.Timer to cause text to flash in the text field. However, I noticed that if this timer is running when I stop my app, the timer's thread is still running. It appears that the class that has the timer is not being garbage collected immediately upon application exit. Is there a way to kill all swing timers?
    Thanks,
    John

    No. This is a class whose implementation I was hoping the main application would not have to know about. I was hoping that since I was using swing timers that when the application on which thread the swing timers are executing exited, all the swing timers attached to that application would be stopped.
    Do you have a suggestion how I might be able to do this? I have an application "A" that uses a library class "B" method that happens to start a timer. I do not want "A" to have to explicitly stop "B" when the application is ending. Is there a way I can tag the timer so that it will stop when "A" exits? Or could I somehow link "B" to "A" so that a dispose method on "B" is called when "A" exits or disposes?
    Thanks,
    John

  • How to create a semi circuler slider using java swing or awt

    I am trying to create a Jslider like thing but in semicirculer shape. Thanks in advance for providing any help.

    Is it possible to create such UI in java swing?Just to further illustrate, Swing only provides the raw foundation you need to be able to build a GUI. With the stock components you can create static user interfaces with standard controls. If you want to go further, you need to either find a component that somebody else already wrote, or you go into custom painting as previous posters suggested. To do an animating component for example, you would need all the frames as BufferedImage objects and you would combine custom painting with a Swing timer to update the displayed frame at a specified framerate.
    if you search for "java swing animation" using google, I'm sure you can find the examples you need. And here is the trail about swing timers:
    http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html

  • How to use 'Wait'/'Delay' in Swing

    Hi all,
    I want to simulate an interation between two peers,
    that exchange information among each other.
    The idea is to have a JPanel where JLabel("Text") are
    added in a fixed but consecutive intervals. Something like
    <Add label>; <Delay/wait a second>;<Add other label>; <Delay>;.....
    Is there an easy/simpler way to do this without using
    threads?
    Does Swing have a simple way to do what I want?
    Can you recomend a link for this?
    Pls note that I don't want to block my entire application.
    Thanks in advance for the help,
    Jorge

    Here is a link to the Swing tutorial on "How to Use Timers":
    http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html
    And here is a link to to thread that uses a Timer to 'continuously scroll text':
    http://forum.java.sun.com/thread.jsp?forum=57&thread=291161

  • Timers and quite a lot code

    i have a certain piece of code that when i invoke it pressing a jbutton everything it's ok.
    i execute it in a background thread .
    but i want to schedule it to execute every minute let's say .
    i tried both java.util.timer and swing timer to start the back ground thread but then my code executes more than one tme in a magic way for me. !!!!!!
    i would be more than grateful if anyone could give me a hint.
    i feel that the problem is because timers are designed to execute only small pieces of code
    Thanks in advance
    mits

    Which schedule method did you use on the Timer? Some schedule methods are for repeated events, some methods are for onetime events.
    Cheers,
    --Arnout                                                                                                                                                                                                                                                                                                               

Maybe you are looking for

  • CS4 crashes when using the text tool

    Everytime that I click on the text tool to use it, the program freezes up and Microsoft Windows pops up a message saying 'Adobe Photoshop has stopped working.' I've given in to just using Illustrator for my text, but am finding that I need in now in

  • Many small black and white dots on LCD

    I bought macbook pro 2 months ago, the 2.33 ghz core 2 duo wid 2 gig ram and ATi Radeon X1600 xt gfx card. Recently i noticed small black and white dots cluttered in one place. The rest of the screen has about 7 - 8 independent dots, I feel as if the

  • IMG - Homepage Framework Service - Direct URL launched in a seperate window

    Hi All, I have added a quick link to Working Time to point to an external site within our Client's network. As a test I have entered "http://google.com" into the Directory Path field. But when I click the link it opens a new window and navigates to g

  • Unable to update Premiere Pro CC 7.0.0 to latest version

    I am on Mountain Lion running a trial installation of PPro CC 7.0.0 I was having issues with Multicam Sync and recommened I upgrade to the latest version. However, my CC installer says I am "up to date" and likewise, within PPro CC it says I am up to

  • OS X Drivers for Sound Blaster Z???

    I bought a Sound Blaster Z yesterday, and noticed on the Creative support page for the card that it supported MAC OS X. Does anyone know where the drivers are for this? Thanks.