Color of the moving bar in waitbar

8 views (last 30 days)
Lukas Cierny
Lukas Cierny on 17 Dec 2020
Answered: Anay on 19 Feb 2025
Hello,
I would like to know how I could change the color of the moving bar in the object waitbar in Matlab 2019b and newer. I have tried something like h = waitbar(...) and then get a property of the object, but not successfully. Thank you in advance for any response.

Answers (1)

Anay
Anay on 19 Feb 2025
Hi Lukas,
I understand that you are trying to change color of the progress-bar of the “waitbar” figure.
However, I did not find any inbuilt functionality to change the colour of the moving bar of the “waitbar”. But, I found that the workaround suggested in the following answer works to overcome this issue:
The solution mentioned in this thread requires setting “setStringPainted” member of the underlying java object to “true”. This means that the percentage of waiting done will appear as a string on the progress-bar. Alternatively, you can customize the appearance of the MATLAB "waitbar" figure by directly accessing and modifying the underlying Java Swing component. The "waitbar" contains a “javax.swing.JProgressBar” embedded within its “hgjavacomponent”. Once you obtain this component, you can adjust its appearance by setting various properties, such as foreground color. You may use the following code for refernce:
h = waitbar(0, 'message string');
javaComponent = findall(h, 'Type', 'hgjavacomponent');
if ~isempty(javaComponent)
% Get the JProgressBar in the hierarchy
progressBar = javaComponent.JavaPeer;
% Chnage the progress bar color using UI customization
if ~isempty(progressBar)
% Create a custom UI delegate to change the color
ui = javax.swing.plaf.metal.MetalProgressBarUI();
progressBar.setUI(ui);
% Set the progress bar color
progressBar.setForeground(java.awt.Color(1, 0, 0)); % Set to red color
else
disp('JProgressBar not found.');
end
end
% Driver code to visualize the waitbar
for i = 1:100
pause(0.05); % Simulate some process
waitbar(i/100, h, sprintf('Progress: %d%%', i));
end
% Close the waitbar
close(h);
I hope this helps!

Categories

Find more on App Building in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!