Color of the moving bar in waitbar

8 次查看(过去 30 天)
Lukas Cierny
Lukas Cierny 2020-12-17
回答: Anay 2025-2-19
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.

回答(1 个)

Anay
Anay 2025-2-19
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!

类别

Help CenterFile Exchange 中查找有关 App Building 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by