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 7 of 7

Thread: BlinkingLabel-StackOverflow Error

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default BlinkingLabel-StackOverflow Error

    Hello, I am getting a stack overflow error on line 30(frame = new JFrame("BlinkLabel") And I dont know why, everything looks fine to me. I think it is something simple, but I just can't see it. Any help is very appreciated =].

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.event.KeyEvent;
    import java.awt.Color;

    public class BlinkingLabel extends JLabel
    {
    //instance variables to store foreground and background color
    private Color backGroundC;
    private Color foreGroundC;
    private JPanel panel1;
    BlinkingLabel myBlinkingLabel;
    JFrame frame;
    private int blinkRate = 1000; //ms

    public BlinkingLabel(String s)
    {
    //super(s);

    backGroundC=Color.BLACK;
    foreGroundC=Color.GRAY;

    frame = new JFrame("BlinkLabel");
    myBlinkingLabel = new BlinkingLabel("I'm blinking!");

    panel1= new JPanel();
    panel1.add(myBlinkingLabel);
    frame.add(panel1);

    //frame.getContentPane().setLayout(new java.awt.FlowLayout());
    //frame.getContentPane().add(myBlinkingLabel);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    setOpaque(true);
    new TimerThread().start();

    }


    private class TimerThread extends Thread
    {
    public void run()
    {
    try
    {
    while(!isInterrupted())
    {
    BlinkingLabel.super.setForeground(foreGroundC);
    setForeground(foreGroundC);
    sleep(blinkRate);
    BlinkingLabel.super.setForeground(backGroundC);
    }
    }
    catch(InterruptedException ie)
    {
    System.out.println("timer interrupted");
    }
    }
    }

    public void setBackground(Color c)
    {
    backGroundC=c; //updates my instance variable, used in timer
    super.setBackground(c); //actually changes the background color of the JLabel
    }

    public void setForeground(Color c)
    {
    foreGroundC=c;
    super.setForeground(c);
    }

    public static void main(String[] args)
    {
    //java.awt.EventQueue.invokeLater(new Runnable()
    //{
    //public void run()
    {
    new BlinkingLabel("");
    //createGUI();
    }
    //});
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,585
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: BlinkingLabel-StackOverflow Error

    Sounds like a recursive loop.
    Try adding some printlns to the constructors and methods to see if there is a loop.
    The print out will show how your code is calling the methods and constructors.

    Search through your code to see where the constructor is called from. If more that one place, make sure it does NOT call itself.
    Last edited by Norm; November 14th, 2011 at 10:58 AM.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: BlinkingLabel-StackOverflow Error

    Yeah, you are right. There is a recursive loop. I put prints in my constructor and in both of my setColor Methods. And they print like this

    there is a loop in my ForeGroundMethod
    there is a loop in my constructor
    there is a loop in my BackGroundMethod
    there is a loop in my ForeGroudMethod
    there is a loop constructor
    repeats.....

    ill find it =].

    Edit:
    Is there something wrong with this? If this is wrong then how am I supposed to
    instantiate my blinkingLabel? Should I only instantiate from my main?

    myBlinkingLabel = new BlinkingLabel("");

    Side note, sorry about the indentation. It was organized but when I posted it it must have realigned.
    Last edited by MichaelWU; November 14th, 2011 at 11:21 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,585
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: BlinkingLabel-StackOverflow Error

    Search through your code to see where the constructor is called from. If in more that one place, make sure it does NOT call itself.


    Put [code] tags around the code to preserve formatting.
    See: BB Code List - Java Programming Forums - The Java Community

  5. #5
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: BlinkingLabel-StackOverflow Error

    Ok first of you need to wrap your code in something (I much prefer highlight tags over code tags, because it highlights, but whatever works). Now onto the actual problem.

    You are attempting to do way to much in your class, the constructer for blinking label should do just that, construct a blinklabel. You need to either do the actual driving in the main method or another driver class. That way you could do something like this

    /**
     * has-a relationship with blinking light.  Is the main driving 
     * class to show an example of blinking light. Creating a Thread 
     * wrapper of this and calling the start() method will begin 
     * running the program, setting the Driver visible and whatnot.
     * The GUI should me moved to another class if it is more major 
     * tha a simple example of BlinkingLight.
     * @author
     */
    public class Driver extends JFrame implements Runnable
    {
      /**
       * No-args constructor used to instianate class driver.  Overrides 
       * the default constucter in case at some later time some 
       * variables need to have their values set to some arbitrary initial 
       * value.
       */
      public Driver()
      {
        /*Implementation not shown*/
      }
      /*
       * Sets the JFrame to visible, shows an example of blinking light,
       * do whatever you need to do for running the program here. 
       * The GUI should be very basic or moved to a different class,
       * if moved to a different class make this extend Thread instead 
       * of implementing Runnable and extending JFrame.
       */
      @Override
      public void run()
      {
         /*Implementation not shown*/
      }
      public static void main(String[] args)
      {
        Thread thread = new Thread(new Driver());
         //Start the driver!
        thread.start();
      }
    }

    EDIT: Can't get the commenting to look good outside of edit on iPad (not sure about computer)

    EDIT 2: Shortened comments a bit more, fixing it!
    Last edited by Tjstretch; November 15th, 2011 at 01:58 AM. Reason: noticed that commenting on run shouldnt be /**

  6. The Following User Says Thank You to Tjstretch For This Useful Post:

    MichaelWU (November 15th, 2011)

  7. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: BlinkingLabel-StackOverflow Error

    @MichaelWU: Instead of looking into your code, you must look at Lesson: Using Swing Components

  8. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: BlinkingLabel-StackOverflow Error

    Cool, thanks alot guys. Very helpful.

Similar Threads

  1. Why am I getting StackOverflow errors?
    By techcom0 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 12th, 2011, 09:18 AM