🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.jbuilder.java.swing
6 messages
6 total messages Started by GEORGE Sat, 16 Apr 2005 01:08
null pointer while changing L & F
#649
Author: GEORGE
Date: Sat, 16 Apr 2005 01:08
114 lines
4560 bytes
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

------------------------------------------------------------------------------------------------
MY CODE

    int laf = userPreferences.getInt("LOOK_AND_FEEL", 
LookAndFeelPanel.METAL);
    switch (laf) {
     case LookAndFeelPanel.MOTIF:
       UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
       SwingUtilities.updateComponentTreeUI(this);
//       this.pack();
       return;
     case LookAndFeelPanel.WINDOWS:
       UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
       SwingUtilities.updateComponentTreeUI(this);
//       this.pack();

       return;
     default:
       UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
      SwingUtilities.updateComponentTreeUI(this);
//      this.pack();

----------------------------------------------------------------------------------------------
STACK TRACE

Exception occurred during event dispatching:

java.lang.NullPointerException

 at 
javax.swing.plaf.basic.BasicSplitPaneUI.getPreferredSize(BasicSplitPaneUI.java:1065)

 at javax.swing.JComponent.getPreferredSize(JComponent.java:1582)

 at java.awt.GridBagLayout.GetLayoutInfo(GridBagLayout.java:911)

 at java.awt.GridBagLayout.getLayoutInfo(GridBagLayout.java:816)

 at java.awt.GridBagLayout.preferredLayoutSize(GridBagLayout.java:636)

 at java.awt.Container.preferredSize(Container.java:1558)

 at java.awt.Container.getPreferredSize(Container.java:1543)

 at javax.swing.JComponent.getPreferredSize(JComponent.java:1584)

 at java.awt.GridBagLayout.GetLayoutInfo(GridBagLayout.java:911)

 at java.awt.GridBagLayout.getLayoutInfo(GridBagLayout.java:816)

 at java.awt.GridBagLayout.ArrangeGrid(GridBagLayout.java:1412)

 at java.awt.GridBagLayout.arrangeGrid(GridBagLayout.java:1372)

 at java.awt.GridBagLayout.layoutContainer(GridBagLayout.java:712)

 at java.awt.Container.layout(Container.java:1401)

 at java.awt.Container.doLayout(Container.java:1390)

 at java.awt.Container.validateTree(Container.java:1473)

 at java.awt.Container.validateTree(Container.java:1480)

 at java.awt.Container.validateTree(Container.java:1480)

 at java.awt.Container.validate(Container.java:1448)

 at 
javax.swing.RepaintManager.validateInvalidComponents(RepaintManager.java:379)

 at 
javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:113)

 at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)

 at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)

 at 
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:234)

 at 
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)

 at 
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:153)

 at java.awt.Dialog$1.run(Dialog.java:515)

 at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)

 at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)

 at 
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:234)

 at 
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)

 at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)

 at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)

 at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Re: null pointer while changing L & F
#650
Author: Lori M Olson [TeamB]
Date: Sat, 16 Apr 2005 15:39
42 lines
2495 bytes
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
Re: null pointer while changing L & F
#653
Author: GEORGE
Date: Tue, 19 Apr 2005 01:23
188 lines
7878 bytes
I am posting below the code for my "app":  just a single runnable JDialog 
subclass.  There are three buttons Metal,  Motif, and Window .  I find that 
if I change  the L&F directly in the action class for these buttons (see the 
commented out lines in the action handlers) then there is no problem. 
However if I use the action handlers to set a Preferences object and make 
the JDialog class a PreferenceChangeListener then I get a null pointer 
exception.  The trouble is that each time I run the program , the NPE 
doesn't happen on the first button click.  Sometimes its the second, 
sometimes the third click, etc.


The Code
---------------------------------------------------------------------------------------------------------------------------------------------------------
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;

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");
//     SwingUtilities.updateComponentTreeUI(this);
          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");
//    SwingUtilities.updateComponentTreeUI(this);

          userPreferences.putInt("LOOK_AND_FEEL", MOTIF);
         }catch (Exception ex) {}
      }
    });

    windowsButton.addActionListener(new AbstractAction() {
      public void actionPerformed(ActionEvent e) {
        try {
// 
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
//        SwingUtilities.updateComponentTreeUI(this);
          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;
}


"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 
Re: null pointer while changing L & F
#654
Author: GEORGE
Date: Tue, 19 Apr 2005 01:34
238 lines
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 
Re: null pointer while changing L & F
#655
Author: GEORGE
Date: Tue, 19 Apr 2005 01:38
206 lines
8047 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.*;
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
Re: null pointer while changing L & F
#656
Author: George
Date: Tue, 19 Apr 2005 13:30
232 lines
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
>
>
>
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads