Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cant i change LAF at runtime?

 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic