• Post Reply Bookmark Topic Watch Topic
  • New Topic

Progress bar problem  RSS feed

 
Steve Wood
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

We have a dialog screen that shows a progress bar and recently it's started acting up. Basically, as the progress bar is updated, it flashes so the progress bar appears on top of the text above the progress bar - but only really quickly - it basically flickers between the following:

This:

Text here
[|||||||||| ]

To this:

[|||||||||| ]
[|||||||||| ]

(but the above only shows very briefly when the progress bar is updated using the setPosition method. It then returns to normal.

Here's the code:

package com.test.components;

import com.test.FrameOperation;
import javax.swing.*;
import com.borland.jbcl.layout.*;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Font;
import com.test.Shared;

public class ProgressFrame extends JFrame
{
public ProgressFrame(Image icon, String sTitle, String sText)
{
verticalFlowLayout1 = new VerticalFlowLayout();
txtText = new JTextArea(sText);
prgStatus = new JProgressBar();
_icon = icon;

this.setTitle(sTitle);

try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}

private void jbInit() throws Exception
{
txtText.setBackground(new Color(0, 0, 0, 0));
txtText.setFont(Shared.getSystemFont(Font.PLAIN));
txtText.setEditable(false);

this.getContentPane().setLayout(verticalFlowLayout1);
this.setResizable(false);
this.getContentPane().add(txtText, null);
this.getContentPane().add(prgStatus, null);
this.setIconImage(_icon);
this.setSize(new Dimension(500, 90));

FrameOperation.centreFrame(this);
}

public void setMaximum(int iMaximum)
{
prgStatus.setMaximum(iMaximum);
}

public void setPosition(int iPosition)
{
setPosition(iPosition, "");
}

public void setPosition(int iPosition, String sText)
{
prgStatus.setValue(iPosition);
txtText.setText(sText);

txtText.invalidate();
txtText.validate();
txtText.repaint();

prgStatus.invalidate();
prgStatus.validate();
prgStatus.repaint();

this.invalidate();
this.validate();
this.repaint();
}

private Image _icon;
private VerticalFlowLayout verticalFlowLayout1;
private JTextArea txtText;
private JProgressBar prgStatus;
}


Any ideas why this might be happening?

All the best,

Steve
 
Joe Ess
Bartender
Posts: 9429
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't run your code since I don't have the imports, but your problem is probably caused by your copious use of invalidate(), validate() and repaint(). Invalidate marks the component as needing the layout manager to place it and validate invokes the layout manager. There's no reason to invoke them. Invoking repaint directly is probably not necessary, as both JProgressBar and JTextArea will update when their content has changed.
If you found it necessary to invoke repaint directly because JProgressBar would not update itself otherwise, you are probably using the Swing event dispatch thread to run your own tasks. Have a look at this discussion for some explanation and options if that is the case.
 
Steve Wood
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Joe,

I'll have a look.

Steve
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!