Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Changing preferences from command line
Date: Thu, 19 Mar 2009 01:37:02 +0000 (UTC)
Organization: TACT Computer Systems Ltd
Lines: 47
Message-ID: <gps7ju$7vl$1@fred.mathworks.com>
References: <f9vhri$orl$1@fred.mathworks.com> <fa415r$kk8$1@fred.mathworks.com> <fa48lm$834$1@fred.mathworks.com> <fa498v$hr8$1@fred.mathworks.com> <g803om$4e2$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1237426622 8181 172.30.248.38 (19 Mar 2009 01:37:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 19 Mar 2009 01:37:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 642467
Xref: news.mathworks.com comp.soft-sys.matlab:526022


"Ben Steiner" <nospam@nospam.com> wrote in message <g803om$4e2$1@fred.mathworks.com>...
> Yair 
> Many, many thanks for this clear & concise explanation. It 
> was exactly what I was looking for. Im running two instances
> of Matlab and using it to set a different background color
> in each. Using these java calls on the preferences has got
> me tantalisingly close to a solution ... maybe I could ask
> your expert advice to get to completion?
> 
> This is the code im using:
> % dont use system color
> com.mathworks.services.Prefs.setBooleanPref('ColorsUseSystem',0);
> % use specified color
> com.mathworks.services.Prefs.setColorPref('ColorsBackground',<whatever>)
> 
> where <whatever> is a java.awt.Color object returned by
> com.mathworks.services.Prefs.getColorPref('ColorsBackground')
> 
> My problem now is that the new color (or preference) does
> not take effect on the desktop immediately. Instead, it
> comes into effect either after a restart of Matlab or if I
> open the Preferences dialog manually and then push 'Apply'.
...

I found a solution and so I'm posting it here for those interested:

After modifying the preferences, we need to notify all the components who use them to update themselves. This is standard practice in Java. In short, the relevant functions to call are these (for foreground & background colors):

com.mathworks.services.ColorPrefs.notifyColorListeners('ColorsText');
com.mathworks.services.ColorPrefs.notifyColorListeners('ColorsBackground');

However, if you only wish to set the colors in the command window and not in all the other Matlab text panes, then forget the prefs because they affect all the Matlab text panes at once. Instead, simply use the following short code snippet (you may need to tweak it for particular Matlab versions):

listeners=com.mathworks.mde.cmdwin.CmdWinDocument.getInstance.getDocumentListeners;
jTextArea = listeners(3);
% note: a safer way would be to loop on listeners until finding a JTextArea instance

Now you can set the colors (which apply immediately) using several alternative ways:
jTextArea.setBackground(java.awt.Color.yellow);
jTextArea.setBackground(java.awt.Color(1,1,0));
set(jTextArea,'Background','yellow');
set(jTextArea,'Background',[1,1,0]);

You can do the same with the Foreground property.

Yair Altman
http://undocumented-matlab.com