954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How would you do this?

How would you put a scrolling message on your application with a seperate thread, sort of like an advertisment?

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

Just like that :)
I guess it's a Swing app?
Make a JPanel, put a Timer on it which on every tick rotates the text in a StringBuffer one character (or however much) and sets it to a JLabel on that JPanel.

Add the assembly to your application and turn on the timer.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

Thank you, it was easier than I thought. I made one with the timer thread, and one with the original thread in sleep mode.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

I used a JLabel, and deleted the first character and added it on the end of the text. Then the thread did the rest. It was kind of cool, bu not as hard as I expected.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

If you use a single String you may want to check your performance and memory usage.
Could get substantial as Strings are immutable and thus a new String is being created for each iteration.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

YOUR RIGHT! I didn't even think of that, and I was wondering why it was kind of jumpy, or seemed laggy. I'll change it to a BufferedString and get back to you on the physically visable performance. Anyways, thank you for pointing that out.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

Is there a way I can some how work the StringBuffer in this method, because I'm using substrings, and I guess that would contribute to a performance loss.

public void run()
    {
    	while (true)
    	{
     	   try
      	  {        
      	      	String oldText = adLabel.getText();
				String newText = oldText.substring(1) + oldText.substring(0, 1);
				
				adLabel.setText(newText);
          	  Thread.sleep(250);

          }
             	catch (InterruptedException e)
       		{
           	      stop();
                }
    }
  }
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

One way would be to build an array of characters when setting the text and building up a stringbuffer based on the content of that array (keeping the current index of the first character in memory) during each iteration.
You can then simply call toString() on the StringBuffer in order to set the text.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You