Article View: borland.public.jbuilder.java.swing
Article #656Re: null pointer while changing L & F
From: George
Date: Tue, 19 Apr 2005 13:30
Date: Tue, 19 Apr 2005 13:30
232 lines
8586 bytes
8586 bytes
apologies for posting this thrice...I thought the deletes worked when I
posted this at home. But now I'm viewing this from work and I still see the
original two posts...Pl ignore.
"GEORGE" <geo@nowhere.net> wrote in message
news:4264611d$1@newsgroups.borland.com...
> 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.*;
> import com.georgex.util.LookAndFeelHandler;
>
> /**
> * <p>Title: </p>
> *
> * <p>Description: </p>
> *
> * <p>Copyright: Copyright (c) 2005</p>
> *
> * <p>Company: </p>
> *
> * @author
> * @version 1.0
> */
> 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", METAL);
> switch (laf) {
> case MOTIF:
>
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
> SwingUtilities.updateComponentTreeUI(this);
> return;
> case 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;
> }
>
>
>
>
>
>
> --------------------------------------------------------------------------
---------------------------------------
> "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:
<426507d9$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!news-out.nntp.be!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<4260659e@newsgroups.borland.com> <426131bc$1@newsgroups.borland.com> <4264611d$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" <gxavier@hotmail.com> Newsgroups: borland.public.jbuilder.java.swing References: <4260659e@newsgroups.borland.com> <426131bc$1@newsgroups.borland.com> <4264611d$1@newsgroups.borland.com> Subject: Re: null pointer while changing L & F Date: Tue, 19 Apr 2005 09:30:03 -0400 Lines: 234 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 NNTP-Posting-Host: 216.163.255.1 Message-ID: <426507d9$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1113917401 216.163.255.1 (19 Apr 2005 06:30:01 -0800) Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!news-out.nntp.be!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail