Article View: borland.public.jbuilder.java.swing
Article #654Re: null pointer while changing L & F
From: GEORGE
Date: Tue, 19 Apr 2005 01:34
Date: Tue, 19 Apr 2005 01:34
238 lines
9343 bytes
9343 bytes
I am posting a runnable version of my "app". There are 3 buttons (for
Motif, Metal and Window) in the JDialog subclass. If I change the L&F
"directly" in the action handler for the buttons there is no problem.
However if I just set a Preferences object in the action handler with the
chosen look and feel and make the JDialog subclass a
PreferenceChangeListener (as shown below) then I get an NPE: and not always
on the first button click.
I would like to get it to work the second way because in my real app, I'd
like to be able to inform all the other containers of the L&F change by
making all of them implement PreferenceChangeListener. If there is a better
way to accomplish this, please let me know.
TIA
George
------------------------------------------------------------------------------------------------------------
package configuration;
import javax.swing.*;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.util.prefs.*;
public class OptionsDialog1 extends JDialog
implements PreferenceChangeListener
{
public OptionsDialog1() throws Exception {
init();
}
public static void main(String[] args) {
try {
OptionsDialog1 optionsdialog1 = new OptionsDialog1();
optionsdialog1.setVisible(true);
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void init() throws Exception {
userPreferences = Preferences.userRoot();
this.setTitle("Configuration Options");
this.setModal(true);
this.setSize(500,500);
scrollPane1.setViewportView(tree);
scrollPane2.setViewportView(panel);
splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setDividerLocation(150);
splitPane.setLeftComponent(scrollPane1);
splitPane.setRightComponent(scrollPane2);
splitPane.setOneTouchExpandable(true);
userPreferences.addPreferenceChangeListener(this);
this.getContentPane().setLayout(new GridBagLayout());
this.getContentPane().add(splitPane, new
GridBagConstraints(0,0,1,1,1,1,GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(20,10,0,10),0,0));
this.getContentPane().add(metalButton, new
GridBagConstraints(0,1,1,1,1,0,GridBagConstraints.EAST,
GridBagConstraints.NONE, new Insets(20,0,20,50),0,0));
this.getContentPane().add(motifButton, new
GridBagConstraints(1,1,1,1,0,0,GridBagConstraints.EAST,
GridBagConstraints.NONE, new Insets(20,10,20,0),0,0));
this.getContentPane().add(windowsButton, new
GridBagConstraints(2,1,1,1,0,0,GridBagConstraints.EAST,
GridBagConstraints.NONE, new Insets(20,10,20,0),0,0));
metalButton.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
try {
// UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
// setLAF();
userPreferences.putInt("LOOK_AND_FEEL", METAL);
} catch (Exception ex) {}
}
});
motifButton.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
try {
// UIManager.setLookAndFeel(
// "com.sun.java.swing.plaf.motif.MotifLookAndFeel");
userPreferences.putInt("LOOK_AND_FEEL", MOTIF);
setLAF();
}catch (Exception ex) {}
}
});
windowsButton.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
try {
//
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
// setLAF();
userPreferences.putInt("LOOK_AND_FEEL", WINDOWS);
} catch (Exception ex) {}
}
});
}
public void preferenceChange(PreferenceChangeEvent e) {
System.out.println(e.getKey());
try {
int laf = userPreferences.getInt("LOOK_AND_FEEL",
LookAndFeelHandler.METAL);
switch (laf) {
case LookAndFeelHandler.MOTIF:
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
return;
case LookAndFeelHandler.WINDOWS:
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
return;
default:
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
return;
}
} catch (Exception ex) {}
}
private void setLAF() {
SwingUtilities.updateComponentTreeUI(this);
}
JSplitPane splitPane = new JSplitPane();
JScrollPane scrollPane1 = new JScrollPane();
JScrollPane scrollPane2 = new JScrollPane();
JTree tree = new JTree();
JPanel panel = new JPanel();
JButton motifButton = new JButton("Motif");
JButton metalButton = new JButton("Metal");
JButton windowsButton = new JButton("Windows");
Preferences userPreferences;
public static final int METAL = 0;
public static final int MOTIF = 1;
public static final int WINDOWS = 2;
}
----- Original Message -----
From: "Lori M Olson [TeamB]" <javadragon@techie.com>
Newsgroups: borland.public.jbuilder.java.swing
Sent: Saturday, April 16, 2005 11:39 AM
Subject: Re: null pointer while changing L & F
> GEORGE wrote:
>
>> I am using the code below to change the look and feel of my app using 3
>> radio buttons for Metal, Motif and Windows.
>>
>> The L & F changes alright. But I get a null pointer exception along the
>> way. The stack trace is also shown below but I am unable to make much of
>> it. Any help would be appreciated.
>>
>> TIA
>> George
>>
>
> What leads you to believe that piece of code is responsible? It's not
> part of the stack trace? Is it that the problem goes away if you comment
> out that code or something like that?
>
> I suspect that your code is simply exposing an different, underlying
> problem. Reading stack traces of Swing bugs is usually a thankless task.
> I really hate finding NPE's in the JDK. If you run in the debugger, you
> can click on those line numbers and go directly to the line on which the
> NPE happened. Looks like something needed a preferred size to be set, and
> it was not. But that's just a wild guess.
>
> --
>
> Regards,
>
> Lori Olson [TeamB]
>
> ------------
>
> Save yourself, and everyone else, some time and search the
> newsgroups and the FAQ-O-Matic before posting your next
> question.
>
> Google Advanced Newsgroup Search
> http://www.google.ca/advanced_group_search
> Other Newsgroup Searches:
> http://www.borland.com/newsgroups/ngsearch.html
> Joi Ellis's FAQ-O-Matic:
> http://www.visi.com/~gyles19/fom-serve/cache
"Lori M Olson [TeamB]" <javadragon@techie.com> wrote in message
news:426131bc$1@newsgroups.borland.com...
> GEORGE wrote:
>
>> I am using the code below to change the look and feel of my app using 3
>> radio buttons for Metal, Motif and Windows.
>>
>> The L & F changes alright. But I get a null pointer exception along the
>> way. The stack trace is also shown below but I am unable to make much of
>> it. Any help would be appreciated.
>>
>> TIA
>> George
>>
>
> What leads you to believe that piece of code is responsible? It's not
> part of the stack trace? Is it that the problem goes away if you comment
> out that code or something like that?
>
> I suspect that your code is simply exposing an different, underlying
> problem. Reading stack traces of Swing bugs is usually a thankless task.
> I really hate finding NPE's in the JDK. If you run in the debugger, you
> can click on those line numbers and go directly to the line on which the
> NPE happened. Looks like something needed a preferred size to be set, and
> it was not. But that's just a wild guess.
>
> --
>
> Regards,
>
> Lori Olson [TeamB]
>
> ------------
>
> Save yourself, and everyone else, some time and search the
> newsgroups and the FAQ-O-Matic before posting your next
> question.
>
> Google Advanced Newsgroup Search
> http://www.google.ca/advanced_group_search
> Other Newsgroup Searches:
> http://www.borland.com/newsgroups/ngsearch.html
> Joi Ellis's FAQ-O-Matic:
> http://www.visi.com/~gyles19/fom-serve/cache/1.html
Technical Details
Message-ID:
<42646021$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<4260659e@newsgroups.borland.com> <426131bc$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 111d45,d97b42cc813c1e29 X-Google-Attributes: gid111d45,public X-Google-Language: ENGLISH,ASCII-7-bit From: "GEORGE" <geo@nowhere.net> Newsgroups: borland.public.jbuilder.java.swing References: <4260659e@newsgroups.borland.com> <426131bc$1@newsgroups.borland.com> Subject: Re: null pointer while changing L & F Date: Mon, 18 Apr 2005 21:34:14 -0400 Lines: 240 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2180 X-RFC2646: Format=Flowed; Response X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 NNTP-Posting-Host: 70.21.199.188 Message-ID: <42646021$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1113874465 70.21.199.188 (18 Apr 2005 18:34:25 -0800) Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail