Page 1 of 1

Java visual stuff

Posted: Sun Jan 14, 2007 9:56 pm
by shiznatix
Ok so I am working with a progress bar and I have got it to work no problem. Super! The problem though is that after I start the program that uses the progress bar, the rest of the elements freeze. Like the menu bar, if I minimize the applet as it runs the file upload then restore it the menu bar will be transparent kinda as it will not be the menu bar anymore but what was behind it (like when a program freezes and you try to maximize it again or something). Everything goes back to normal when the program finishes the main loop.

I was wondering how I could make this menu stay visible at all times without it going into freeze mode. Here is the relative code:

Code: Select all

//THE UPLOADING LOOP
while ((current_byte = bufferedinputstream.read()) != -1)
            {
            	iteration = iteration + 1;
            	
            	float percent = percent_per_iteration * iteration;
            	
            	shiznatix.VisualElements.SetProgressBarValue((int)percent);
            	shiznatix.VisualElements.SetContent("");
            	
                bufferedoutputstream.write(current_byte);
            }
//END THE UPLOADING LOOP

//THE VISUAL ELEMENTS METHODS
public void SetProgressBarValue(int Percentage)
    {
    	progress_bar.setValue(Percentage);
    	
    	Rectangle progressRect = progress_bar.getBounds();
		progressRect.x = 0;
		progressRect.y = 0;
		progress_bar.paintImmediately(progressRect);
    }

public void SetContent(String str)
    {
    	if (str != "")
    	{
    		content.append(str + "\n");
    	}
        
        Rectangle progressRect = content.getBounds();
		progressRect.x = 0;
		progressRect.y = 0;
		content.paintImmediately(progressRect);
    }
I use the SetContent in the loop to make the content text block not do the same thing the menu bar is doing.

Posted: Mon Jan 15, 2007 12:40 am
by timvw
Run your upload loop in a different thread because right now it is in your ui-dispatch-thread.. And as you can see it keeps your application from repainting itself etc...

Posted: Mon Jan 15, 2007 3:50 am
by Jenk
Yay for multi-threading.

Posted: Mon Jan 15, 2007 3:57 am
by Chris Corbyn
You are aware that a JProgressBar class exists right? ;)

http://java.sun.com/j2se/1.5.0/docs/api ... ssBar.html

Posted: Mon Jan 15, 2007 1:10 pm
by shiznatix
d11wtq wrote:You are aware that a JProgressBar class exists right? ;)

http://java.sun.com/j2se/1.5.0/docs/api ... ssBar.html
:) yes, that is what progress_bar is, a JProgressBar. But I am using the paintImmidiatly() stuff because the event listener everything is too much for me right now. Maybe tomorrow.

Multi-threading and all of that, could you give me an example of how I could run this in another thread. I don't really understand and the online stuff is going over my head.

Posted: Mon Jan 15, 2007 1:59 pm
by timvw
Simply follow the link d11wtq provided you... http://java.sun.com/docs/books/tutorial ... gress.html and notice the writings about SwingWorker...

Posted: Mon Jan 15, 2007 3:56 pm
by Chris Corbyn
I never used to like the Java documentation but now I do and I think the only reason I didn't like it before was because it felt a bit unfamiliar after being so deep in PHP for so long. I do wish they'd make it easier to search from any one page to the next though; unless I'm missing something you have to go back to the search page to start a new search. User comments would be good too for obvious reaons :)