Java Forum / GUI / January 2004
Changing Look & Feel: layouting and component sizes
Karsten Wutzke - 01 Jan 2004 19:30 GMT Hi all!
I have a non modal JDialog in which information about the current demo is displayed. When changing the look and feel, the buttons and other components' sizes are not correctly adjusted to the new look and feel.
I call
SwingUtilities.updateComponentTreeUI(wnd); wnd.pack();
on every window owned by the frame, which the dialog is (I checked).
Inside every dialog's overridden pack() method I simply try to call
//pnSuper is the content pane! SwingUtilities.updateComponentTreeUI(pnSuper); pnSuper.revalidate(); pnSuper.repaint();
to update the dialog's content pane, in hope, so that every component on that panel will get laid out again and the component colors will be adjusted. But this obviously isn't the case. I also tried calling pnSuper.doLayout(), without success.
Can anyone give me a suggestion as to why the components don't get laid out and don't get their new preferred sizes? I might be totally wrong with my solution... please correct me if so!
What do I have to call to make it work? I don't want to recreate the whole dialog.
TIA! Karsten
PS: The look and feel change does work for the main frame's content pane, so everything else works.
Dave Glasser - 01 Jan 2004 20:29 GMT Karsten Wutzke <kwutzke-blahblah@emporis.com> wrote on Thu, 01 Jan 2004 20:30:34 +0100 in comp.lang.java.gui:
>Hi all! > [quoted text clipped - 27 lines] >What do I have to call to make it work? I don't want to recreate the >whole dialog. If you explicitly called setPreferredSize() on any of the components in your dialog, changing the L&F on the fly probably wouldn't have an effect.
Also, from my own experience, I've found that changing the L&F of a Swing app on the fly can produce all sorts of bizarre results if the L&F you're switching to or from was not written properly. It seems a lot of L&F designers wrote their L&Fs assuming that a program would set the L&F on start up and never switch to a different one.
 Signature Check out QueryForm, a free, open source, Java/Swing-based front end for relational databases.
http://qform.sourceforge.net
Andrew Thompson - 02 Jan 2004 07:01 GMT ....
> ...When changing the look and feel, the buttons and other > components' sizes are not correctly adjusted to the new look and feel. ....
> Inside every dialog's overridden pack() method I simply try to call > > //pnSuper is the content pane! > SwingUtilities.updateComponentTreeUI(pnSuper); > pnSuper.revalidate(); > pnSuper.repaint(); Why do you call 'updateComponentTreeUI' from inside 'pack()' Karsten? Why not put in in a method and invoke the method yourself?
If you cannot sort the problem yourself, I suggest an SSCCE* - updating the L&F has a few gotcha's.. * http://www.physci.org/codes/sscce.jsp
-- Andrew Thompson * http://www.PhySci.org/ PhySci software suite * http://www.1point1C.org/ 1.1C - Superluminal! * http://www.AThompson.info/andrew/ personal site
Karsten Wutzke - 02 Jan 2004 17:27 GMT > .... > [quoted text clipped - 14 lines] > in in a method and invoke the method > yourself? Well, the reason is, that when changing the look and feel, the components contained in a window most likely change, and so do their sizes (buttons, combo boxes, etc.). I call pack() on every window to adjust their size to their newly sized contents.
Since Window, what I call pack on doesn't have an updateUI method, I placed the call into pack, which I call on *every* window anyway. It was some kind of desparate try to update the contents of every window, that didn't update from the previously called
SwingUtilities.updateComponentTreeUI(wnd);
which should have updated the content pane. I my case, it sometimes doesn't.
Err, I think, this has something to do with the LnF implementations I use, which, as the other replier hinted, are not implemented to work well with on the fly LnF changes.
So finally, I'm beginning to think of it as someone else's fault and ditch work on any solution...
Karsten
Dave Glasser - 03 Jan 2004 03:55 GMT Karsten Wutzke <kwutzke-blahblah@emporis.com> wrote on Fri, 02 Jan 2004 18:27:00 +0100 in comp.lang.java.gui:
>Err, I think, this has something to do with the LnF implementations I >use, which, as the other replier hinted, are not implemented to work >well with on the fly LnF changes. > >So finally, I'm beginning to think of it as someone else's fault and >ditch work on any solution... One way to test that theory would be to try changing between the L&Fs that come bundled with the JRE *before* setting any third-party L&F. Those would be the Metal, CDE/Motif and the native L&F (probably Windows). Those tend to be very well-behaved, even when swapped on the fly, from my experience. If you notice odd behavior when using only the JRE-bundled L&Fs, it's more likely to be something wrong with your code.
Later JREs also include a GTK L&F, but I've noticed some quirks in that.
 Signature Check out QueryForm, a free, open source, Java/Swing-based front end for relational databases.
http://qform.sourceforge.net
Karsten Wutzke - 03 Jan 2004 17:18 GMT > Karsten Wutzke <kwutzke-blahblah@emporis.com> wrote on Fri, 02 Jan > 2004 18:27:00 +0100 in comp.lang.java.gui: [quoted text clipped - 16 lines] > Later JREs also include a GTK L&F, but I've noticed some quirks in > that. I will try that. I wrote code that sets the most recent look and feel on application startup.
Karsten
Andrew Thompson - 03 Jan 2004 06:38 GMT > So finally, I'm beginning to think of it as someone else's fault and > ditch work on any solution... It's not 'someone else's fault' the code does not work. Others _have_ got it to work. It's time for an SSCCE.. http://www.physci.org/codes/sscce.jsp\
-- Andrew Thompson * http://www.PhySci.org/ PhySci software suite * http://www.1point1C.org/ 1.1C - Superluminal! * http://www.AThompson.info/andrew/ personal site
Dave Glasser - 03 Jan 2004 15:53 GMT "Andrew Thompson" <andrew64@bigNOSPAMpond.com> wrote on Sat, 03 Jan 2004 06:38:09 GMT in comp.lang.java.gui:
>> So finally, I'm beginning to think of it as someone else's fault and >> ditch work on any solution... > >It's not 'someone else's fault' the code does not work. If the cause of the problem is a buggy third-party component, then it is in fact "someone else's fault."
>Others _have_ got it to work. How do you know that others, using the same third-party L&F as this guy, have gotten the correct behavior that has eluded him?
>It's time for an SSCCE.. >http://www.physci.org/codes/sscce.jsp\ Careful, you're starting to sound like Paul Lutus.
 Signature Check out QueryForm, a free, open source, Java/Swing-based front end for relational databases.
http://qform.sourceforge.net
Andrew Thompson - 03 Jan 2004 23:21 GMT > "Andrew Thompson" <andrew64@bigNOSPAMpond.com> wrote on Sat, 03 Jan > 2004 06:38:09 GMT in comp.lang.java.gui: .....
> >It's not 'someone else's fault' the code does not work. > > If the cause of the problem is a buggy third-party component, then it > is in fact "someone else's fault." If, if, if.. I'm in this thread in an attempt to get the OP's code working, why are you in this thread?
> >Others _have_ got it to work. > > How do you know that others, using the same third-party L&F as this > guy, have gotten the correct behavior that has eluded him? Others _may_ have experienced trouble with the hypothetical third party LnF that the OP _might_ be using.
..Obviously he should give up immediately, it's an unsolvable problem.
> >It's time for an SSCCE.. > >http://www.physci.org/codes/sscce.jsp\ > > Careful, you're starting to sound like Paul Lutus. Careful, you're starting to sound pointless
-- Andrew Thompson * http://www.PhySci.org/ PhySci software suite * http://www.1point1C.org/ 1.1C - Superluminal! * http://www.AThompson.info/andrew/ personal site
Dave Glasser - 04 Jan 2004 00:44 GMT "Andrew Thompson" <andrew64@bigNOSPAMpond.com> wrote on Sat, 03 Jan 2004 23:21:18 GMT in comp.lang.java.gui:
>> "Andrew Thompson" <andrew64@bigNOSPAMpond.com> wrote on Sat, 03 Jan >> 2004 06:38:09 GMT in comp.lang.java.gui: [quoted text clipped - 5 lines] > >If, if, if.. I'm just going by what the guy said. He didn't explicitly say he's using a third-party L&F, but his response to me strongly suggests that is the case.
>I'm in this thread in an attempt to >get the OP's code working, why are you in >this thread? I shared with the OP some of the things I've learned about changing L&Fs on the fly.
>> >Others _have_ got it to work. >> [quoted text clipped - 4 lines] >hypothetical third party LnF that the OP _might_ >be using. They may, and they may not have. I have no way of knowing. And in spite of your claim that "others have got it to work," neither do you.
>..Obviously he should give up immediately, it's >an unsolvable problem. It may or may not be solvable with the tools at his disposal. Or the OP may decide that the problem isn't worth solving, and seek a workaround instead. Forgoing the ability to change PLAFs on the fly is not likely to be a showstopper.
>> >It's time for an SSCCE.. >> >http://www.physci.org/codes/sscce.jsp\ >> >> Careful, you're starting to sound like Paul Lutus. > >Careful, you're starting to sound pointless Oh, I think I've made my point.
 Signature Check out QueryForm, a free, open source, Java/Swing-based front end for relational databases.
http://qform.sourceforge.net
Andrew Thompson - 04 Jan 2004 11:00 GMT > "Andrew Thompson" <andrew64@bigNOSPAMpond.com> wrote on Sat, 03 Jan > 2004 23:21:18 GMT in comp.lang.java.gui: > >> "Andrew Thompson" <andrew64@bigNOSPAMpond.com> wrote on Sat, 03 Jan > >> 2004 06:38:09 GMT in comp.lang.java.gui: I'll rewind a bit (..'back down' if you like)
> >..... > >> >It's not 'someone else's fault' the code does not work. [quoted text clipped - 7 lines] > using a third-party L&F, but his response to me strongly suggests that > is the case. OK. I deliberately couched my words to the last reponse not to be explicit, but I see that was counter-productive. So I'll admit.
'I missed the part about the custom LnF' [until you mentioned it]
> >I'm in this thread in an attempt to > >get the OP's code working, why are you in [quoted text clipped - 14 lines] > They may, and they may not have. I have no way of knowing. And in > spite of your claim that "others have got it to work," neither do you. Yes, you are right in this circumstance, especially considering.. a) the custom layout b) not having seen the full code, I am not even certain what it is the OP is trying to achieve (OK - the stated aim is 'change LnF', but there may be extra quirks on top, not indicated in the snippets)
> >..Obviously he should give up immediately, it's > >an unsolvable problem. [quoted text clipped - 3 lines] > workaround instead. Forgoing the ability to change PLAFs on the fly is > not likely to be a showstopper. Again, good point. There are times I've given up on some quirky aspect of UI's on the simple basis that 'the app. _works_'
So, all jibes aside. I'll put it to the OP.
Do you want to solve this enough that you would prepare the example I suggested?
I feel we are at the stage where this is the best way to progress toward a solution (or at the very least, have others confirm that _they_ cannot get that particular L&F working)
-- Andrew Thompson * http://www.PhySci.org/ PhySci software suite * http://www.1point1C.org/ 1.1C - Superluminal! * http://www.AThompson.info/andrew/ personal site
Jon A. Cruz - 05 Jan 2004 19:10 GMT > I call > > SwingUtilities.updateComponentTreeUI(wnd); > wnd.pack(); > > on every window owned by the frame, which the dialog is (I checked). That's ok... but there's a better way to find all of them.
> Inside every dialog's overridden pack() method I simply try to call > > //pnSuper is the content pane! > SwingUtilities.updateComponentTreeUI(pnSuper); > pnSuper.revalidate(); > pnSuper.repaint(); You should never need to do that.
Clean that up and leave pack() as-is.
For updating...
First and foremost, be sure you change the look and feel.
Next, call java.awt.Frame.getFrames() to get an array of all frames floating around.
Then walk the array and call SwingUtilities.updateComponentTreeUI() and then pack() on each member of the array.
That's it. Nothing more should be needed.
If things don't work properly, then the problems lie elsewhere, and kludging up pack() or such won't fix things.
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 ...
|
|
|