Clear Filters
Clear Filters

Change colours of jProgressBar

15 views (last 30 days)
Andrés Aguilar
Andrés Aguilar on 2 Feb 2023
Edited: Andrés Aguilar on 2 Feb 2023
Hello:
I am trying to build an customized progress bar. Right now I need to change the foreground and background colours (already done and remaining precentages). This is because I want to do to give the illusion of the progress going from right to left (as suggested here) I know my approach is not the best, but here it is:
%%Messagebox
%Create a messagebox
hMsgRange = msgbox('Progress bar test', 'No signal', 'help');
hMsgRangeMB = findobj(hMsgRange,'Tag','MessageBox');
%Rearrange size and components
hMsgRange.Units = 'normalized'
hMsgRange.Position = [hMsgRange.Position(1), .5-hMsgRange.Position(4), hMsgRange.Position(3), hMsgRange.Position(4)*2]
hMsgRange.Children(2).Units = 'normalized'
hMsgRange.Children(2).Position(2) = 1 - 1.25*hMsgRange.Children(2).Position(4)
hMsgRangeMB.Units = 'normalized'
hMsgRangeMB.FontUnits = 'normalized'
hMsgRangeMB.Position = [hMsgRangeMB.Position(1) (hMsgRange.Children(2).Position(2) + hMsgRange.Children(2).Position(4)/2 - hMsgRangeMB.FontSize/2) 0]
hMsgRangeMB.FontUnits = 'points'
%% Progress bar
%Create a waitbar and then change the parent to have it inside the
%messagebox window.
hBar = waitbar ( 0.5, 'Please Wait....' );
tmp = findall(hBar);
jWrapper = tmp(2);
jWrapper.Parent = hMsgRange
jWrapper.Units = 'normalized';
jWrapper.Position = [0.1 0.25 0.8 0.25]
clear tmp;
jFrame = get(hBar, 'JavaFrame');
jFigPanel = jFrame.getFigurePanelContainer;
jContainer = jFigPanel.getComponent(0);
jPanel = jContainer.getComponent(0);
jProgressContainer = jPanel.getComponent(0);
jProgressBar = jProgressContainer.getComponent(0);
jProgressBar.setValue(50)
jProgressBar.setStringPainted(1) %Needed to allow me to change colours
I can change the colour of the forground of the jProgressBar object like this:
jProgressBar.setForeground(java.awt.Color(0.75,0.1,0.85));
But when I change the backround like this:
jProgressBar.setBackground(java.awt.Color(1,0.0,0.0));
The result I get is this:
But I was expecting this:
I am not very knowledgeable it java, so after looking into some posts, here and Stackoverflow, I am at a loss.
Does anyone have any idea if what I am trying to do is feasible?
In case anyone wonders, my final goal is a dialog with an icon, some text, a "compound progress bar" and a pushbutton:
The "compound progress bar" is made up of three jProgressBar objects, the red one has the colours inverted so when it reaches 100% the bar should be fully gray and appear to "grow" from right to left. The green one should be larger and the other one should be blue.
As a value changes (starting at -1) the user should start with a red rectangle to the left and two gray to the middle and right. As the value increases the red rectangle diminishes its horizontal size from left to right leaving a gray behind. Wen the value is in [-0.5, 0.5] the green bar should fill from left to right and from 0.5 to 1, the blue bar should grow from left to right.
The behaviour and colours are not whimsical as they respond to the readout of a sensor the user should set within a predefined range (the green bar)
Thanks a lot for taking the time to read my question!

Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!