aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes cant i change LAF at runtime? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "cant i change LAF at runtime?" Watch "cant i change LAF at runtime?" New topic
Author

cant i change LAF at runtime?

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

i have given lot of themes or lookandfeel of jtatoo in my application but the problem is when i click or select any laf the laf should change
it is changing also but partially , i mean after changing laf at runtime i m not getting the entire look of that theme
how to do that any idea ?


The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

SwingUtilities.updateComponentTreeUI. Your existing components already have a UI set, and you need to tell them to update it to that belonging to the new LAF. Call that method with your top-level window(s) as arguments and it will update the UI of all components.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4527
    
    5

But be aware that all LAFs don't play fair. Haven't tried with the latest release which is part of core Java 7, but the preview Nimbus included with Java 6 used to set some LAF features that weren't restored when switching beck to (say) Metal. I think I remember JTable gridlines disappearing after the changes.


luck, db
There are no new questions, but there may be new answers.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Rob Spoor wrote:SwingUtilities.updateComponentTreeUI. Your existing components already have a UI set, and you need to tell them to update it to that belonging to the new LAF. Call that method with your top-level window(s) as arguments and it will update the UI of all components.

when ever i use this method and supply jlist as argument because its my top component then the JVM throws an null pointer exception i dont know what is null here


running this method from a list selection valueChanged() event
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

In Java 6, line 2613 of BasicListUI.java (check src.zip inside your JDK folder) is That means that either the list is null, or it has no model. My guess is the latter; perhaps the old ListUI removes the old model and the new ListUI doesn't set a new one.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Rob Spoor wrote:In Java 6, line 2613 of BasicListUI.java (check src.zip inside your JDK folder) is That means that either the list is null, or it has no model. My guess is the latter; perhaps the old ListUI removes the old model and the new ListUI doesn't set a new one.

yes my jlist has a default model but i dont understand the "new" and "old" list
i m not making any new list here so where does the new list arises from ?
and how to solve my problem ?

int size = list.getModel().getSize();

this gives 12 , yes i have 12 items in jlist

list has no model

i guess list always has a "default model" when we didn't explicitly give one , isn't it ?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

naved momin wrote:
Rob Spoor wrote:In Java 6, line 2613 of BasicListUI.java (check src.zip inside your JDK folder) is That means that either the list is null, or it has no model. My guess is the latter; perhaps the old ListUI removes the old model and the new ListUI doesn't set a new one.

yes my jlist has a default model but i dont understand the "new" and "old" list
i m not making any new list here so where does the new list arises from ?
and how to solve my problem ?

I never mentioned "new" and "old" lists, I mentioned new and old ListUIs. ListUI is the base class of the object responsible for providing the look and feel of a JList. The LAF sets this ListUI. If you change the LAF, this will remove the old ListUI and set a new one for the JList. You don't do this explicitly, the API does it when you update the JList's UI which you in turn do by updating the UI of the entire component tree.

int size = list.getModel().getSize();

this gives 12 , yes i have 12 items in jlist
Where does this give 12? Directly before the call to SwingUtilities.updateComponentTreeUI(list);? What does it give directly after that call?

list has no model

i guess list always has a "default model" when we didn't explicitly give one , isn't it ?

You're actually right on that one. If a JList is created without an explicit model a new one is created for it. Calling setModel with null as its arguments will cause an IllegalArgumentException. In other words, list.getModel() could never be null. That means that in that line list must be null.

BasicListUI has a private field of type JList. This is the one from the line I posted before. Initially it's null. It's given a value when its installUI method is called. This is done when the ListUI object is set for the JList. It's set to null again when its uninstallUI method is called. This is done when the ListUI is removed from the JList.
What this tells me is that the BasicListUI's internal event listener is still being used after the BasicListUI is removed from the JList. This should usually never occur; removing the BasicListUI should also unregister all of its internal event listeners. The only reason I can think of is that some code (written by you perhaps?) is getting all of the JList's listeners (in this case ListSelectionListener) and doing something that keeps them alive (perhaps adding them again to the same JList or another one). As a result, this listener is called when it shouldn't be.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Rob Spoor wrote:
naved momin wrote:
Rob Spoor wrote:In Java 6, line 2613 of BasicListUI.java (check src.zip inside your JDK folder) is That means that either the list is null, or it has no model. My guess is the latter; perhaps the old ListUI removes the old model and the new ListUI doesn't set a new one.

yes my jlist has a default model but i dont understand the "new" and "old" list
i m not making any new list here so where does the new list arises from ?
and how to solve my problem ?

I never mentioned "new" and "old" lists, I mentioned new and old ListUIs. ListUI is the base class of the object responsible for providing the look and feel of a JList. The LAF sets this ListUI. If you change the LAF, this will remove the old ListUI and set a new one for the JList. You don't do this explicitly, the API does it when you update the JList's UI which you in turn do by updating the UI of the entire component tree.

int size = list.getModel().getSize();

this gives 12 , yes i have 12 items in jlist
Where does this give 12? Directly before the call to SwingUtilities.updateComponentTreeUI(list);? What does it give directly after that call?

list has no model

i guess list always has a "default model" when we didn't explicitly give one , isn't it ?

You're actually right on that one. If a JList is created without an explicit model a new one is created for it. Calling setModel with null as its arguments will cause an IllegalArgumentException. In other words, list.getModel() could never be null. That means that in that line list must be null.

BasicListUI has a private field of type JList. This is the one from the line I posted before. Initially it's null. It's given a value when its installUI method is called. This is done when the ListUI object is set for the JList. It's set to null again when its uninstallUI method is called. This is done when the ListUI is removed from the JList.
What this tells me is that the BasicListUI's internal event listener is still being used after the BasicListUI is removed from the JList. This should usually never occur; removing the BasicListUI should also unregister all of its internal event listeners. The only reason I can think of is that some code (written by you perhaps?) is getting all of the JList's listeners (in this case ListSelectionListener) and doing something that keeps them alive (perhaps adding them again to the same JList or another one). As a result, this listener is called when it shouldn't be.

thanks for the explanation rob i have solved that problem
but now the problem is when ever i set the LAF for 1st time it gets update so than i travel through the jlist and set another LAf it also set well
but when i try to set the LAF which i have used earlier then the entire frame is not updated
for eg half part like frame is update but buttons and jlist is not updated so i have done this

this also doesn't do what i want it to do
can you sense the problem please ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: cant i change LAF at runtime?