Java Forum / GUI / May 2004
Metal theme in an applet.
Gaute Strokkenes - 03 May 2004 12:18 GMT I have a class that derives from DefaultMetalTheme (see below). I call MetalLookAndFeel.setCurrentTheme to install it. This works fine in a standalone program, but not at all from an applet! No exception, no nothing--it simply doesn't work.
I get the feeling that I'm missing something obvious here, and I hope that someone can point it out to me. Thanks.
import java.awt.Font; import java.awt.Toolkit;
import javax.swing.plaf.*; import javax.swing.plaf.metal.*;
public class MyTheme extends DefaultMetalTheme { public String getName () { return "MyTheme"; } final private static FontUIResource dialog10FUR; final private static FontUIResource dialog11FUR; final private static FontUIResource dialog11BoldFUR;
final private static double factor;
static { factor = Toolkit.getDefaultToolkit().getScreenResolution() / 72.0; dialog10FUR = new FontUIResource ("dialog", Font.PLAIN, pointsTo72DPI (10)); dialog11FUR = new FontUIResource ("dialog", Font.PLAIN, pointsTo72DPI (11)); dialog11BoldFUR = new FontUIResource ("dialog", Font.BOLD, pointsTo72DPI (11)); } public static int pointsTo72DPI (int points) { return (int) Math.round (points * factor); }
public FontUIResource getControlTextFont () { return dialog11FUR; } public FontUIResource getMenuTextFont () { return dialog11BoldFUR; } public FontUIResource getWindowTitleFont () { return dialog11BoldFUR; }
public FontUIResource getSystemTextFont () { return dialog11FUR; }
public FontUIResource getUserTextFont () { return dialog11FUR; }
public FontUIResource getSubTextFont () { return dialog10FUR; } }
 Signature Gaute Strokkenes
Andrew Thompson - 03 May 2004 12:52 GMT > I have a class that derives from DefaultMetalTheme (see below). I > call MetalLookAndFeel.setCurrentTheme to install it. This works fine > in a standalone program, but not at all from an applet! No exception, > no nothing--it simply doesn't work. > > I get the feeling that I'm missing something obvious here, An URL, and the code for the applet, for starters.
>..and I hope > that someone can point it out to me. Thanks. You are welcome...
Oh OK! It _might_ be that your GUI is already on screen when you update the L&F, and the changes are not percolating through the UI properly. I have an example of updating the L&F here, it may give you some pointers. <http://www.physci.org/launcher.jsp#JPLAFChanger>
 Signature Andrew Thompson http://www.PhySci.org/ Open-source software suite http://www.PhySci.org/codes/ Web & IT Help http://www.1point1C.org/ Science & Technology
Gaute Strokkenes - 03 May 2004 13:45 GMT >> I have a class that derives from DefaultMetalTheme (see below). I >> call MetalLookAndFeel.setCurrentTheme to install it. This works fine [quoted text clipped - 4 lines] > > An URL, and the code for the applet, for starters. I've cooked up this small example that exhibits the problem:
Test.java --8<-- import java.awt.Font; import java.awt.Toolkit;
import javax.swing.*; import javax.swing.plaf.*; import javax.swing.plaf.metal.*;
class MyTheme extends DefaultMetalTheme { public String getName () { return "MyTheme"; } final private static FontUIResource dialog10FUR; final private static FontUIResource dialog11FUR; final private static FontUIResource dialog11BoldFUR;
final private static double factor;
static { factor = Toolkit.getDefaultToolkit().getScreenResolution() / 72.0; dialog10FUR = new FontUIResource ("dialog", Font.PLAIN, pointsTo72DPI (10)); dialog11FUR = new FontUIResource ("dialog", Font.PLAIN, pointsTo72DPI (11)); dialog11BoldFUR = new FontUIResource ("dialog", Font.BOLD, pointsTo72DPI (11)); } public static int pointsTo72DPI (int points) { return (int) Math.round (points * factor); }
public FontUIResource getControlTextFont () { return dialog11FUR; } public FontUIResource getMenuTextFont () { return dialog11BoldFUR; } public FontUIResource getWindowTitleFont () { return dialog11BoldFUR; }
public FontUIResource getSystemTextFont () { return dialog11FUR; }
public FontUIResource getUserTextFont () { return dialog11FUR; }
public FontUIResource getSubTextFont () { return dialog10FUR; } }
public class Test extends JApplet { public static void main (String [] args) { MetalLookAndFeel.setCurrentTheme (new MyTheme ()); JFrame f = new JFrame ("xyzzy"); f.getContentPane ().add (new JButton ("Plugh!")); f.pack (); f.show (); }
public void init () { try { final JApplet _this = this; SwingUtilities.invokeAndWait (new Runnable () { public void run () { MetalLookAndFeel.setCurrentTheme (new MyTheme ()); _this.getContentPane ().add (new JButton ("Plugh!")); } }); } catch (Exception e) { e.printStackTrace (); } } } --8<--
Test.html: --8<-- <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> <html> <head> <title>Fubar</title> </head>
<body> <applet code="Test.class" width="700" height="400">
Your browser is completely ignoring the <code><APPLET></code> tag! </applet> </body> </html> --8<--
If you say "java Test" then the effect is the desired one, but if you do "appletviewer Test.html" then it's not.
>>..and I hope that someone can point it out to me. Thanks. > [quoted text clipped - 6 lines] > I have an example of updating the L&F here, it may give you some > pointers. <http://www.physci.org/launcher.jsp#JPLAFChanger>
 Signature Gaute Strokkenes
Andrew Thompson - 05 May 2004 10:44 GMT >>> I have a class that derives from DefaultMetalTheme (see below). ...hmmmm. That was a good example.
I have played with it this way and that, and must admit it has me stumped. :-(
I cannot, for love nor money, get the L&F to change in the applet.
So ..come on everybody. Somebody must have an answer for this. I refuse to believe that you cannot change the L&F of an Applet..
 Signature Andrew Thompson http://www.PhySci.org/ Open-source software suite http://www.PhySci.org/codes/ Web & IT Help http://www.1point1C.org/ Science & Technology
T.J. Willis - 20 May 2004 23:17 GMT > So ..come on everybody. Somebody must have > an answer for this. I refuse to believe that > you cannot change the L&F of an Applet.. This one time, in band camp, I subclassed Applet instead of JApplet and had this sort of problem. Boy, did I feel silly.
For those interested in themes, I have a drop-in theme manager at http://steelme.sf.net that I'd like some feedback on. (It's not going to work with JApplets, though, yet -- I have to hack in Jar support).
Cheers, TJ Willis
Andrew Thompson - 21 May 2004 07:46 GMT *Was*: Metal theme in an applet. <http://google.com/groups?th=2506ef3a652ff582>
(A.T.)
>> So ..come on everybody. Somebody must have >> an answer for this. I refuse to believe that >> you cannot change the L&F of an Applet.. > > This one time, in band camp, I subclassed Applet instead of JApplet > and had this sort of problem. Boy, did I feel silly. I'd say you should have.
But since the OP quite clearly sub-classed JApplet, I fail to see the relevance of your post to this thread. Except that both are related to L'n'F (a relatively tenuous connection).
> For those interested in themes, I have a drop-in theme manager at > http://steelme.sf.net that I'd like some feedback on. So it might be better to start a different thread asking for your testing. (..in fact, I will)
[ And ..isn't it time SourceFourge programmers got there act together, dragged themselves (kicking and screaming) into the third millenium and offered either.. a) a screenshot ..or, b) web-launchable app./JWS jar? ]
 Signature Andrew Thompson http://www.PhySci.org/ Open-source software suite http://www.PhySci.org/codes/ Web & IT Help http://www.1point1C.org/ Science & Technology
T.J. Willis - 21 May 2004 16:27 GMT > But since the OP quite clearly sub-classed > JApplet, I fail to see the relevance of your > post to this thread. The word Applet was used at least 13 times. JApplet was used 0 times. A certain amount of precision in language can help avoid this sort of miscommunication.
> [ And ..isn't it time SourceFourge programmers > got there act together, dragged themselves > (kicking and screaming) into the third > millenium and offered either.. > a) a screenshot ..or, > b) web-launchable app./JWS jar? ] Thanks for the constructive criticism. I'd point out that it is a 0.1.2 release. If I had everything I wanted done, don't you think it would have a grander, more awe-inspiring release number?
Cheers, TJ
Andrew Thompson - 21 May 2004 16:33 GMT > If I had everything I wanted done, > don't you think it would have a grander, more awe-inspiring > release number? ..release numbers can be 'awe-inspiring'?
If so, you need to get out more..
 Signature Andrew Thompson http://www.PhySci.org/ Open-source software suite http://www.PhySci.org/codes/ Web & IT Help http://www.1point1C.org/ Science & Technology
Andrew Thompson - 21 May 2004 07:47 GMT > For those interested in themes, .. See my response on new thread.. 'Some LnF theme changer'
 Signature Andrew Thompson http://www.PhySci.org/ Open-source software suite http://www.PhySci.org/codes/ Web & IT Help http://www.1point1C.org/ Science & Technology
T.J. Willis - 21 May 2004 22:13 GMT > <blah, blah, blah deleted> Yes, you can set a metal theme in a JApplet. This works in appletviewer:
import javax.swing.*; import javax.swing.plaf.*; import javax.swing.plaf.metal.*; import java.awt.*;
public class app extends JApplet { public void init(){ NewTheme t = new NewTheme(); JLabel lab = new JLabel("Hi there"); add(lab); JRootPane rt = SwingUtilities.getRootPane(this);
MetalLookAndFeel.setCurrentTheme(t); try { UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); } catch (Exception e) { System.out.println("Can't set lnf" +e); } SwingUtilities.updateComponentTreeUI(rt); }
class NewTheme extends DefaultMetalTheme { public ColorUIResource getPrimary1(){ return new ColorUIResource(Color.red); } public ColorUIResource getPrimary2(){ return new ColorUIResource(Color.red); } public ColorUIResource getPrimary3(){ return new ColorUIResource(Color.red); } public ColorUIResource getSecondary1(){ return new ColorUIResource(Color.red); } public ColorUIResource getSecondary2(){ return new ColorUIResource(Color.red); } public ColorUIResource getSecondary3(){ return new ColorUIResource(Color.red); }
} }
Obvious that you were using a JApplet. I counted 13 uses of the word Applet and 0 of JApplet in the thread. Doing that once in the code will toast it.
Andrew Thompson - 22 May 2004 00:36 GMT > Andrew Thompson <SeeMySites@www.invalid> wrote in message news:.. ...
> Yes, you can set a metal theme in a JApplet. This works in > appletviewer: ...
When I run* your applet** I see red.
No, .. don't mean I get angry, I mean that all I see is a big ..red ...square.
Is it May-Day? Did I miss it? Is it like a 'Glug Glug Box' perhaps? :)
* After correcting the compilation error. ** OK, OK! _J_Applet, JApplet, ..sheesh!
OTOH I decided to take a more rigourous approach to my own PLAF changer and concocted this little demo. here.. <http://www.physci.org/test/lnf/>
You don't need to compile it or anything, just click the link and click a button in the dialog that appears.
You sohuld be able to see a variety of Swing components in various states of editable/enabled, as well as a draggable toolbar. The source is linked.
> Obvious that you were using a JApplet. I counted 13 uses of the word > Applet and 0 of JApplet in the thread. Doing that once in the code > will toast it. OK ..yeah, you make a good point.
Communication in such a precise environment as software development can hinge on a single letter (as you point out), and those involved should take care to be precise, so as to avoid a tumultuous confusion.
(chuckles) Now come on, admit it, you're pissed at me ...not the OP! ;-)
 Signature Andrew Thompson http://www.PhySci.org/ Open-source software suite http://www.PhySci.org/codes/ Web & IT Help http://www.1point1C.org/ Science & Technology
T.J. Willis - 22 May 2004 07:06 GMT Andrew Thompson <SeeMySites@www.invalid> wrote in message
> * After correcting the compilation error. Did you get an 'attempting to assign weaker access' error on the getPrim*, getSec* methods in the inner class? Just looking at it, I would expect that to come up, but I tried it on jdk1.4.2 and jdk1.5.0B1 and it compiled. Now I'm curious...
Thanks, TJ
> (chuckles) Now come on, admit it, > you're pissed at me ...not the OP! ;-) Just tired of getting static when for attempting to be helpful. It's almost like being married again *shudder*.
Andrew Thompson - 22 May 2004 23:41 GMT > Andrew Thompson <SeeMySites@www.invalid> wrote in message >> * After correcting the compilation error. > > Did you get an 'attempting to assign weaker access' error on > the getPrim*, getSec* methods in the inner class? No, no. simple typo. add(lab); -> getContentPane().add();
Mind you, if you meant something else, it would explain why I had little luck with the code.
>> (chuckles) Now come on, admit it, >> you're pissed at me ...not the OP! ;-) > > Just tired of getting static when for attempting to be helpful. It's > almost like being married again *shudder*. I'm *always* getting in trouble for "taking the p**s" (an Australian translation of "giving static").
OTOH (and both being aware that the original post might have been more clear on Swing), do you recognise that your own first post in this thread had a strong element of T.T.P.?
If your comment had been only directed at myself * I probably would have laughed and moved on (..your comment *was* very funny - made me laugh!) but I jumped on it because I was worried it might divert a thread on a subject that is important to me, and the OP.
* Of course, your comment *was* only quoting *my* rather _dweebish_ comment, but the entire thread was about Gaute's L'n'F problem.
I hope .. a) Your open source project goes well b) You can forgive any initial T.T.P. c) You continue to help people d) ..the OP is still listening(??) and got his answer (my example completely ignores the original source, but instead is based on some old source of my own)
 Signature Andrew Thompson http://www.PhySci.org/ Open-source software suite http://www.PhySci.org/codes/ Web & IT Help http://www.1point1C.org/ Science & Technology
Andrew Thompson - 29 May 2004 13:25 GMT > I have a class that derives from DefaultMetalTheme (see below). Are you still about? Hope so!
There is a much better solution for you now. The original source I pointed you to, had a problem when I took the component out of the dialog and put it in the applet itself.
Since I have now solved that, the component is now on the (floatable!) tool bar, I thought I should follow this up..
Further details here.. <http://www.physci.org/test/lnf/>
HTH
 Signature Andrew Thompson http://www.PhySci.org/ Open-source software suite http://www.PhySci.org/codes/ Web & IT Help http://www.1point1C.org/ Science & Technology
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|