- Undocumented Matlab - https://undocumentedmatlab.com/blog_old -
Javacomponent background color
Posted By Yair Altman On October 12, 2011 | 3 Comments
In this website, I have often shown how Matlab application functionality can be significantly enhanced by including Java components in the Matlab GUI, using the built-in semi-documented javacomponent function (which I described here [3] last year).
Using java components in Matlab is simple and easy, but there are a few annoying catches. One of these is the fact that the default Matlab figure background color ([0.8, 0.8, 0.8]) is a different shade of gray than the default uicontrol background color ([0.9255, 0.9137, 0.847]). javacomponents use the same background color as uicontrols.
hPanel = uipanel('units','pixe', 'pos',[30,30,200,100], 'title','Matlab uipanel'); jLabel = javax.swing.JLabel('Java Label'); [jhlabel,jContainer]=javacomponent(jLabel, [250,50,100,50], gcf);
While Matlab users are familiar with updating Matlab colors, doing so with Java colors is a bit different. We have two basic ways to align the javacomponent background color with the figure’s: either change the figure’s Color property to the Java component’s bgcolor, or vice versa:
% Fix the Java component: % Variant #1 bgcolor = get(gcf, 'Color'); jLabel.setBackground(java.awt.Color(bgcolor(1),bgcolor(2),bgcolor(3))); % Variant #2 bgcolor = num2cell(get(gcf, 'Color')); jLabel.setBackground(java.awt.Color(bgcolor{:})); % Fix the Matlab panel uicontrol set(hPanel, 'BackgroundColor', get(gcf,'Color'));
bgcolor = jLabel.getBackground.getComponents([]); set(gcf, 'Color', bgcolor(1:3));
Look at the list of related posts below for other articles related to colors in Matlab.
Categories: GUI, Java, Low risk of breaking in future versions, Semi-documented function, Undocumented feature
Article printed from Undocumented Matlab: https://undocumentedmatlab.com/blog_old
URL to article: https://undocumentedmatlab.com/blog_old/javacomponent-background-color
URLs in this post:
[1] Image: https://undocumentedmatlab.com/feed/
[2] email feed: https://undocumentedmatlab.com/subscribe_email.html
[3] described here: https://undocumentedmatlab.com/blog/javacomponent/
[4] Borderless button used for plot properties : https://undocumentedmatlab.com/blog_old/borderless-button-used-for-plot-properties
[5] Matlab and the Event Dispatch Thread (EDT) : https://undocumentedmatlab.com/blog_old/matlab-and-the-event-dispatch-thread-edt
[6] Uitable sorting : https://undocumentedmatlab.com/blog_old/uitable-sorting
[7] Types of undocumented Matlab aspects : https://undocumentedmatlab.com/blog_old/types-of-undocumented-matlab-aspects
[8] setPrompt – Setting the Matlab Desktop prompt : https://undocumentedmatlab.com/blog_old/setprompt-setting-matlab-desktop-prompt
[9] Editable combo-box : https://undocumentedmatlab.com/blog_old/editable-combo-box
[10] : https://undocumentedmatlab.com/blog/panel-level-uicontrols/
Click here to print.
Copyright © Yair Altman - Undocumented Matlab. All rights reserved.
3 Comments To "Javacomponent background color"
#1 Comment By Panos On November 9, 2011 @ 5:49 pm
hello,
(How) is it possible to place the panel label(name) someplace else than the top of it?
thanks in advance
#2 Comment By Yair Altman On November 10, 2011 @ 10:15 am
@Panos – you can easily get the handle of the panel label and then modify its Position property, as well as other aspects: color, font, and even component style, as described here: [10]
#3 Comment By Peng Liu On July 19, 2019 @ 11:07 am
Hello Yair,
is it possible to set transparency of javacomponents backgroud color? I tried setBackground(java.awt.Color(1, 0.98, 0.94, 0.5)) of my Spinning icon which I learned from your article as well. Here 0.5 defines the Alpha. But only at start and at end the transparency works. While the Spinning Icon runs, it doesn’t works. May you have some idea or Solutions?