I am using a jprogressbar to indicate the availability status. i want to display a text of 40%[assumption] inside the progressbar. how to do it? the text was changed according to the availability value
You can use:
Initialising:
progressBar.setStringPainted(true);
Updating:
progressBar.setValue(newValue);
Use
setStringPainted(true)
to show the Percentage of work completed.Use
setValue()
which will help setting the incremental value andsetString()
to display the end message when done...
Here is an example from my code base :
final JProgressBar bar = new JProgressBar(0 , 100); // 0 - min , 100 - max
bar.setStringPainted(true);
panel.add(bar); // panel is a JPanel's Obj reference variable
JButton butt = new JButton("start");
butt.addActionListener(){
public void actionPerformed(){
new Thread(new Runnable(){
public void run(){
int x = 0;
while(x<=100) {
x++;
bar.setValue(x); // Setting incremental values
if (x == 100 ){
bar.setString("Its Done"); // End message
try{
Thread.sleep(200);
}catch(Exception ex){ }
}
}).start();
}
});
I am using a jprogressbar to indicate the availability status.
please read tutorial about JProgressBar
i want to display a text of 40%[assumption] inside the progressbar.
Using Determinate Progress Bars in the JProgressBar tutorial
how to do it? the text was changed according to the availability value
more in the SwingWorker tutorial
This will show the progress inside the bar
progressBar.setStringPainted(true);
This shows the progress percentage inside the progress bar
progressBar.setStringPainted(true);
I'm unclear if your [assumption]
is part of the string you want displayed. If so, the complete solution would be something like:
private static final String PROGRESS_MASK = "%d%% [assumption]";
public void someMethod() {
...
progressBar.addChangeListener(new ChangeListener() {
@Override
void stateChanged(ChangeEvent e) {
progressBar.setString(String.format(PROGRESS_MASK,
progressBar.getValue()));
}
}
progressBar.setStringPainted(true);
}
... as you would be unable to rely on the default string which merely displays the percentage.
Two things you should notice here. Those are,
1) You have to set paintString variable of JProgressBar using setStringPainted method. You can do that like
jprogressBar.setStringPainted(true)
you have to do this because,
isStringPainted()
method should return true, if the progress bar has to show the values or percentage of progress on it.
2) Now to customize with your custom value, set the your custom on jprogressBar instance using
jprogressBar.setString(customString)
then it should work fine.
Here is the tutorial link which shows how to set the value (i.e. 10% or 40%...) according to the status of the progress bar http://docs.oracle.com/javase/tutorial/uiswing/components/progress.html
© 2022 - 2024 — McMap. All rights reserved.