Current location: Hot Scripts Forums » Programming Languages » Everything Java » NullPointerException when asking for InternalFrameUI


NullPointerException when asking for InternalFrameUI

Reply
  #1 (permalink)  
Old 07-30-07, 12:42 PM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
NullPointerException when asking for InternalFrameUI

Hi,

As you might remember I was writing a modal JInternalFrame, about 2-3 months ago (check out this thread). Today I wanted to extend this class by a class who removes the titlebar from the JInternalFrame. I found a code snippet (see below) which does the trick, but now i'm facing some problems when I implement it into my extending class.

The code snippet to remove the titlebar:
Java Code:
  1.  
  2. setRootPaneCheckingEnabled(false);
  3. ((javax.swing.plaf.basic.BasicInternalFrameUI)frame.getUI()).setNorthPane(null);
My JInternalModalFrame class: (i removed the comments as they were in dutch
Java Code:
  1. package gui;
  2.  
  3. import java.awt.AWTEvent;
  4. import java.awt.ActiveEvent;
  5. import java.awt.Component;
  6. import java.awt.EventQueue;
  7. import java.awt.MenuComponent;
  8. import java.beans.PropertyVetoException;
  9.  
  10. import javax.swing.JFrame;
  11. import javax.swing.JInternalFrame;
  12. import javax.swing.JLayeredPane;
  13. import javax.swing.JPanel;
  14. import javax.swing.SwingUtilities;
  15. import javax.swing.event.MouseInputAdapter;
  16.  
  17. /**
  18. * @author UnrealEd
  19. * @version 25-apr-07 14:03:25
  20. * @see JInternalFrame
  21. */
  22. public class JInternalModalFrame extends JInternalFrame {
  23.  
  24.     private static final long serialVersionUID = 1L;
  25.  
  26.     private JLayeredPane modalLayeredPane;
  27.  
  28.     private JPanel modalGlass;
  29.  
  30.     public JInternalModalFrame(Component parent) {
  31.         this(parent, "");
  32.     }
  33.  
  34.     public JInternalModalFrame(Component parent, String title) {
  35.         this(parent, title, false);
  36.     }
  37.  
  38.     public JInternalModalFrame(Component parent, String title,
  39.             boolean resizeable) {
  40.         this(parent, title, resizeable, true);
  41.     }
  42.  
  43.     public JInternalModalFrame(Component parent, String title,
  44.             boolean resizeable, boolean closeable) {
  45.         this(parent, title, resizeable, closeable, false);
  46.     }
  47.  
  48.     public JInternalModalFrame(Component parent, String title,
  49.             boolean resizeable, boolean closeable, boolean maximizeable) {
  50.         this(parent, title, resizeable, closeable, maximizeable, false);
  51.     }
  52.  
  53.     public JInternalModalFrame(Component parent, String title,
  54.             boolean resizeable, boolean closeable, boolean maximizeable,
  55.             boolean iconifiable) {
  56.         super(title, resizeable, closeable, maximizeable, iconifiable);
  57.  
  58.         if (parent instanceof JFrame) {
  59.             setModalLayeredPane(((JFrame) parent).getLayeredPane());
  60.         } else {
  61.             setModalLayeredPane(JLayeredPane.getLayeredPaneAbove(parent));
  62.         }
  63.         createGlass();
  64.  
  65.         // putClientProperty("JInternalFrame.frameType", "optionDialog");
  66.         try {
  67.             setSelected(true);
  68.         } catch (PropertyVetoException ignored) {
  69.         }
  70.  
  71.         updateUI();
  72.     }
  73.  
  74.     private void createGlass() {
  75.         JPanel modalGlass = new JPanel();
  76.         modalGlass.setOpaque(false);
  77.         MouseInputAdapter adap = new MouseInputAdapter() {
  78.         };
  79.         modalGlass.addMouseListener(adap);
  80.         modalGlass.addMouseMotionListener(adap);
  81.         modalGlass.add(this);
  82.         modalGlass.setBounds(0, 0, getModalLayeredPane().getWidth(),
  83.                 getModalLayeredPane().getHeight());
  84.         modalGlass.setVisible(true);
  85.         setModalGlass(modalGlass);
  86.         getModalLayeredPane().setLayer(getModalGlass(),
  87.                 JLayeredPane.MODAL_LAYER);
  88.         getModalLayeredPane().add(getModalGlass());
  89.         toFront();
  90.     }
  91.  
  92.     public void dispose() {
  93.         super.dispose();
  94.         modalGlass.remove(this);
  95.         getLayeredPane().remove(modalGlass);
  96.         getLayeredPane().repaint();
  97.     }
  98.  
  99.     public void show() {
  100.         super.show();
  101.         startModal();
  102.     }
  103.  
  104.     public void setVisible(boolean isVisible) {
  105.         super.setVisible(isVisible);
  106.         if (isVisible) {
  107.             startModal();
  108.         } else {
  109.             stopModal();
  110.         }
  111.     }
  112.  
  113.     private synchronized void startModal() {
  114.         try {
  115.             if (SwingUtilities.isEventDispatchThread()) {
  116.                 EventQueue theQueue = getToolkit().getSystemEventQueue();
  117.                 while (isVisible()) {
  118.                     modalGlass.setBounds(0, 0, modalLayeredPane.getWidth(),
  119.                             modalLayeredPane.getHeight());
  120.                     AWTEvent event = theQueue.getNextEvent();
  121.                     Object source = event.getSource();
  122.                     if (event instanceof ActiveEvent) {
  123.                         ((ActiveEvent) event).dispatch();
  124.                     } else if (source instanceof Component) {
  125.                         ((Component) source).dispatchEvent(event);
  126.                     } else if (source instanceof MenuComponent) {
  127.                         ((MenuComponent) source).dispatchEvent(event);
  128.                     } else {
  129.                         System.err.println("Unable to dispatch: " + event);
  130.  
  131.                     }
  132.                 }
  133.             } else {
  134.                 Thread.yield();
  135.             }
  136.         } catch (InterruptedException ignored) {
  137.         } finally {
  138.             dispose();
  139.         }
  140.     }
  141.  
  142.     private synchronized void stopModal() {
  143.         notifyAll();
  144.     }
  145.  
  146.     public JPanel getModalGlass() {
  147.         return modalGlass;
  148.     }
  149.  
  150.     private void setModalGlass(JPanel modalGlass) {
  151.         this.modalGlass = modalGlass;
  152.     }
  153.  
  154.     public JLayeredPane getModalLayeredPane() {
  155.         return modalLayeredPane;
  156.     }
  157.  
  158.     private void setModalLayeredPane(JLayeredPane modalLayeredPane) {
  159.         this.modalLayeredPane = modalLayeredPane;
  160.     }
  161. }
And eventually, the JInternalTitlelessFrame:
Java Code:
  1. package gui;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Component;
  5. import java.awt.Container;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8.  
  9. import javax.swing.JButton;
  10. import javax.swing.JComponent;
  11. import javax.swing.JDesktopPane;
  12. import javax.swing.JFrame;
  13. import javax.swing.JInternalFrame;
  14. import javax.swing.JLabel;
  15. import javax.swing.JPanel;
  16. import javax.swing.plaf.basic.BasicInternalFrameUI;
  17.  
  18. public class JInternalTitlelessFrame extends JInternalModalFrame {
  19.  
  20.     private static final long serialVersionUID = 1L;
  21.  
  22.     private static final String emptyTitle = "";
  23.  
  24.     private JComponent northPane;
  25.  
  26.     public JInternalTitlelessFrame(Component parent) {
  27.         this(parent, false);
  28.     }
  29.  
  30.     public JInternalTitlelessFrame(Component parent, boolean resizeable) {
  31.         super(parent, emptyTitle, resizeable);
  32.     }
  33.  
  34.     public void dispose() {
  35.         super.dispose();
  36.     }
  37.  
  38.     public void show() {
  39.         super.show();
  40.         setNorthPane(((BasicInternalFrameUI) getUI()).getNorthPane());
  41.         setRootPaneCheckingEnabled(false);
  42.         ((BasicInternalFrameUI) getUI()).setNorthPane(null);
  43.     }
  44.  
  45.     public void setVisible(boolean isVisible) {
  46.         super.setVisible(isVisible);
  47.         setNorthPane(((BasicInternalFrameUI) getUI()).getNorthPane());
  48.         if (isVisible) {
  49.             setRootPaneCheckingEnabled(false);
  50.             ((BasicInternalFrameUI) getUI()).setNorthPane(null);
  51.         } else {
  52.             ((BasicInternalFrameUI) getUI()).setNorthPane(getNorthPane());
  53.         }
  54.     }
  55.  
  56.     private JComponent getNorthPane() {
  57.         return northPane;
  58.     }
  59.  
  60.     private void setNorthPane(JComponent northPane) {
  61.         this.northPane = northPane;
  62.     }
  63.  
  64.     public static void main(String[] args) {
  65.         final JFrame frame = new JFrame("Modal Internal Frame");
  66.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  67.  
  68.         final JDesktopPane desktop = new JDesktopPane();
  69.  
  70.         final JInternalFrame internal = new JInternalFrame("Opener");
  71.  
  72.         ActionListener showModal = new ActionListener() {
  73.             public void actionPerformed(ActionEvent e) {
  74.                 final JInternalTitlelessFrame modal = new JInternalTitlelessFrame(
  75.                         frame, true);
  76.  
  77.                 modal.setContentPane(new JPanel());
  78.                 JLabel lb = new JLabel("This is just a test");
  79.                 modal.add("message", lb);
  80.  
  81.                 JButton b = new JButton("click me ;)");
  82.                 ActionListener s = new ActionListener() {
  83.                     public void actionPerformed(ActionEvent arg0) {
  84.                         System.out.println("Goopa! Goopa!");
  85.                         modal.dispose();
  86.                     }
  87.  
  88.                 };
  89.                 b.addActionListener(s);
  90.                 modal.add("button", b);
  91.                 b = new JButton("Close");
  92.                 b.addActionListener(s);
  93.                 modal.add("button", b);
  94.                 lb = new JLabel("body test");
  95.                 b = new JButton("body btn");
  96.                 modal.add("body", lb);
  97.                 modal.add("body", b);
  98.                 modal.add("body", lb);
  99.                 modal.add("body", b);
  100.                 modal.setSize(300, 300);
  101.                 modal.setVisible(true);
  102.                 desktop.add(modal);
  103.                 System.out.println("output__");
  104.             }
  105.         };
  106.         desktop.add(internal);
  107.         JButton button = new JButton("Open");
  108.         button.addActionListener(showModal);
  109.  
  110.         Container iContent = internal.getContentPane();
  111.         iContent.add(button, BorderLayout.CENTER);
  112.         internal.setBounds(25, 25, 200, 100);
  113.         internal.setVisible(true);
  114.  
  115.         Container content = frame.getContentPane();
  116.         content.add(desktop, BorderLayout.CENTER);
  117.         frame.setSize(500, 300);
  118.         frame.setVisible(true);
  119.     }
  120. }
Now the problem : Whenever i try to run the above code (the main method), it throws a NullPointerException at this line:
Java Code:
  1. setNorthPane(((BasicInternalFrameUI) getUI()).getNorthPane());
in the JInternalTitlessFrame::setVisible method. For some reason no UI is set when I try to create a modal frame. Can anyone see why this happens? I've been staring at it for hours, but I can't find it (I have very little knowledge when it comes to UIs, and how Swing works).

A second problem i'm facing is the fact that whenever i resize the JInternalTitlelessFrame or the JInternalModalFrame, the frame automatically jumps back to the default entered size. As far as i can see i didn't set anything specific to get this efect, but can anyone see what i'm doing wrong?

Many thanx in advance
cheers
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #2 (permalink)  
Old 07-30-07, 06:33 PM
King Coder King Coder is offline
Community VIP
 
Join Date: Jan 2006
Posts: 703
Thanks: 0
Thanked 0 Times in 0 Posts
It looks like there is no InternalFrameUI returned by getUI(), which is why you are getting the null pointer exception. Maybe it's not initialized in the InternalFrames or it's possibly a platform issue... I don't know exactly. You can fix this by calling updateUI() or setUI(InternalFrameUI). The first method is the easiest, but both work.
__________________
my site
Reply With Quote
  #3 (permalink)  
Old 08-04-07, 12:35 PM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
sorry for the delay, but i was kinda busy lately

I tried what you suggested, and it fixed the NullPointerException. However, the northpane wasn't removed when the JInternalFrame was displayed. After some experimenting i finally found a solution: i now make a call for the setVisible method of the parent after i removed the northpane. I don't know why this solves the problem, i don't even know why if it's the proper way of coding, but it works

I also fixed the second problem i had (the one where i can't move/resize the JInternalFrame). I simply had to change the modalGlass to a JDesktopPane instead of a JPanel (JPanel always uses the minimumLayoutSize ? ).

While these problems are all solved, i'm facing a new one . This time i can't create a JInternalTitlelessFrame on a JFrame immediately. The reason i specify the immediately is because it does work when i create the JInternalTitlelessFrame in an ActionEvent (actionPerformed) of a JButton which i place in the JFrame. Here's a sample of the code that does work:
Java Code:
  1. package gui.components;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Container;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7.  
  8. import gui.components.JInternalTitlelessFrame;
  9.  
  10. import javax.swing.JButton;
  11. import javax.swing.JDesktopPane;
  12. import javax.swing.JFrame;
  13. import javax.swing.JInternalFrame;
  14. import javax.swing.JLabel;
  15. import javax.swing.JPanel;
  16.  
  17. public class TitlelessFrameTest {
  18.  
  19.     public static void main(String[] args) {
  20.         final JFrame frame = new JFrame("Modal Internal Frame");
  21.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22.         frame.setVisible(true);
  23.  
  24.         final JDesktopPane desktop = new JDesktopPane();
  25.  
  26.         final JInternalFrame internal = new JInternalFrame("Opener");
  27.  
  28.         ActionListener showModal = new ActionListener() {
  29.             public void actionPerformed(ActionEvent e) {
  30.                 final JInternalTitlelessFrame modal = new JInternalTitlelessFrame(frame, JInternalTitlelessFrame.CENTER_BOTH);
  31.  
  32.                 modal.setContentPane(new JPanel());
  33.                 JLabel lb = new JLabel("This is just a test");
  34.                 modal.add("message", lb);
  35.  
  36.                 JButton b = new JButton("click me ;)");
  37.                 ActionListener s = new ActionListener() {
  38.                     public void actionPerformed(ActionEvent arg0) {
  39.                         System.out.println("Goopa! Goopa!");
  40.                         modal.dispose();
  41.                     }
  42.  
  43.                 };
  44.                 b.addActionListener(s);
  45.                 modal.add("button", b);
  46.                 b = new JButton("Close");
  47.                 b.addActionListener(s);
  48.                 modal.add("button", b);
  49.                 lb = new JLabel("body test");
  50.                 b = new JButton("body btn");
  51.                 modal.add("body", lb);
  52.                 modal.add("body", b);
  53.                 modal.add("body", lb);
  54.                 modal.add("body", b);
  55.                 modal.setSize(200, 100);
  56.                 modal.setVisible(true);
  57.                 System.out.println("hello");
  58.             }
  59.         };
  60.         desktop.add(internal);
  61.         JButton button = new JButton("Open");
  62.         button.addActionListener(showModal);
  63.  
  64.         Container iContent = internal.getContentPane();
  65.         iContent.add(button, BorderLayout.CENTER);
  66.         internal.setBounds(25, 25, 200, 100);
  67.         internal.setVisible(true);
  68.  
  69.         Container content = frame.getContentPane();
  70.         content.add(desktop, BorderLayout.CENTER);
  71.         frame.setSize(500, 300);
  72.         frame.setVisible(true);
  73.     }
  74. }
and here's a code snippet that doesn't work:
Java Code:
  1. package gui.components;
  2.  
  3. import gui.components.JInternalTitlelessFrame;
  4.  
  5. import javax.swing.JPanel;
  6. import javax.swing.JDesktopPane;
  7. import javax.swing.JFrame;
  8. import javax.swing.JLabel;
  9. import javax.swing.JTextField;
  10. import javax.swing.JPasswordField;
  11.  
  12. class TitlelessFrameTest2 {
  13.  
  14.     public static void main (String[] args) {
  15.         JFrame jf = new JFrame();
  16.         jf.setSize(400, 300);
  17.         jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  18.         jf.setContentPane(new JDesktopPane());
  19.  
  20.         JInternalTitlelessFrame loginWindow = new JInternalTitlelessFrame(jf,
  21.                 JInternalTitlelessFrame.CENTER_BOTH);
  22.         loginWindow.setBounds(10, 10, 200, 100);
  23.         loginWindow.setContentPane(new JPanel());
  24.  
  25.         // place Components on window
  26.         JLabel loginAttempt = new JLabel();
  27.         GregorianCalendar gc = new GregorianCalendar();
  28.         String date = gc.get(GregorianCalendar.DAY_OF_MONTH) + "/";
  29.         date += gc.get(GregorianCalendar.MONTH) + "/";
  30.         date += gc.get(GregorianCalendar.YEAR);
  31.         loginAttempt.setText("Login attempt at: " + date);
  32.         loginWindow.add(loginAttempt);
  33.  
  34.         loginWindow.add(new JLabel("username:"));
  35.         loginWindow.add(new JTextField(30));
  36.  
  37.         loginWindow.add(new JLabel("password:"));
  38.         loginWindow.add(new JPasswordField(30));
  39.  
  40.         loginWindow.add(new JButton("Login"));
  41.         loginWindow.setVisible(true);
  42.        
  43.         jf.setVisible(true);
  44.     }
  45. }
When i run the second code, the JFrame is not accessible anymore (as expected as the JInternalTitlelessFrame is a modal JInternalFrame), but the JInternalFrame doesn't appear.

Here are the update JInternalModalFrame and JInternalTitlelessFrame in case you want to test it yourself:
Java Code:
  1. package gui.components;
  2.  
  3. import java.awt.Component;
  4. import javax.swing.plaf.basic.BasicInternalFrameUI;
  5.  
  6. /**
  7. * @author UnrealEd
  8. * @version 01-aug-07 15:33:56
  9. */
  10. public class JInternalTitlelessFrame extends JInternalModalFrame {
  11.  
  12.     /** COMMENT ME! */
  13.     private static final long serialVersionUID = 1L;
  14.  
  15.     private static final String emptyTitle = "";
  16.  
  17.     public static final int CENTER_NOTHING = 0;
  18.  
  19.     public static final int CENTER_VERTICAL = 1;
  20.  
  21.     public static final int CENTER_HORIZONTAL = 2;
  22.  
  23.     public static final int CENTER_BOTH = 3;
  24.  
  25.     private int center = CENTER_NOTHING;
  26.  
  27.     public JInternalTitlelessFrame(Component parent) {
  28.         this(parent, CENTER_NOTHING);
  29.     }
  30.  
  31.     public JInternalTitlelessFrame(Component parent, int center) {
  32.         this(parent, center, false);
  33.     }
  34.  
  35.     public JInternalTitlelessFrame(Component parent, int center,
  36.             boolean resizeable) {
  37.         super(parent, emptyTitle, resizeable);
  38.         setCenter(center);
  39.         setAbsoluteLocation();
  40.     }
  41.  
  42.     public void dispose() {
  43.         super.dispose();
  44.     }
  45.  
  46.     private int getCenter() {
  47.         return center;
  48.     }
  49.  
  50.     private void setAbsoluteLocation() {
  51.         Component parent = getModalGlass();
  52.         if ((getCenter() & CENTER_HORIZONTAL) == CENTER_HORIZONTAL) {
  53.             int init_x = Math.round((parent.getWidth() - getWidth()) / 2);
  54.             setLocation(init_x, getY());
  55.         }
  56.         if ((getCenter() & CENTER_VERTICAL) == CENTER_VERTICAL) {
  57.             int init_y = Math.round((parent.getHeight() - getHeight()) / 2);
  58.             setLocation(getX(), init_y);
  59.         }
  60.  
  61.     }
  62.  
  63.     private void setCenter(int center) {
  64.         this.center = center;
  65.     }
  66.  
  67.     public void setVisible(boolean isVisible) {
  68.         setRootPaneCheckingEnabled(false);
  69.         setAbsoluteLocation();
  70.         try {
  71.             if (((BasicInternalFrameUI) getUI()).getNorthPane() != null) {
  72.                 ((BasicInternalFrameUI) getUI()).setNorthPane(null);
  73.             }
  74.         } catch (NullPointerException e) {
  75.         }
  76.         super.setVisible(isVisible);
  77.     }
  78.  
  79.     public void show() {
  80.         super.show();
  81.         setRootPaneCheckingEnabled(false);
  82.         try {
  83.             ((BasicInternalFrameUI) getUI()).setNorthPane(null);
  84.         } catch (NullPointerException e) {
  85.         }
  86.     }
  87. }
Java Code:
  1. package gui.components;
  2.  
  3. import java.awt.AWTEvent;
  4. import java.awt.ActiveEvent;
  5. import java.awt.Component;
  6. import java.awt.EventQueue;
  7. import java.awt.MenuComponent;
  8. import java.beans.PropertyVetoException;
  9.  
  10. import javax.swing.JDesktopPane;
  11. import javax.swing.JFrame;
  12. import javax.swing.JInternalFrame;
  13. import javax.swing.JLayeredPane;
  14. import javax.swing.SwingUtilities;
  15. import javax.swing.event.MouseInputAdapter;
  16.  
  17. /**
  18. * @author UnrealEd
  19. * @version 31-jul-07 18:13:57
  20. * @see JInternalFrame
  21. */
  22. public class JInternalModalFrame extends JInternalFrame {
  23.  
  24.     /** COMMENT ME! */
  25.     private static final long serialVersionUID = 1L;
  26.  
  27.     private JLayeredPane modalLayeredPane;
  28.  
  29.     private JDesktopPane modalGlass;
  30.  
  31.     public JInternalModalFrame(Component parent) {
  32.         this(parent, "");
  33.     }
  34.  
  35.     public JInternalModalFrame(Component parent, String title) {
  36.         this(parent, title, false);
  37.     }
  38.  
  39.     public JInternalModalFrame(Component parent, String title,
  40.             boolean resizeable) {
  41.         this(parent, title, resizeable, true);
  42.     }
  43.  
  44.     public JInternalModalFrame(Component parent, String title,
  45.             boolean resizeable, boolean closeable) {
  46.         this(parent, title, resizeable, closeable, false);
  47.     }
  48.  
  49.     public JInternalModalFrame(Component parent, String title,
  50.             boolean resizeable, boolean closeable, boolean maximizeable) {
  51.         this(parent, title, resizeable, closeable, maximizeable, false);
  52.     }
  53.  
  54.     public JInternalModalFrame(Component parent, String title,
  55.             boolean resizeable, boolean closeable, boolean maximizeable,
  56.             boolean iconifiable) {
  57.         super(title, resizeable, closeable, maximizeable, iconifiable);
  58.  
  59.         if (parent instanceof JFrame) {
  60.             setModalLayeredPane(((JFrame) parent).getLayeredPane());
  61.         } else {
  62.             setModalLayeredPane(JLayeredPane.getLayeredPaneAbove(parent));
  63.         }
  64.         createGlass();
  65.  
  66.         //putClientProperty("JInternalFrame.frameType", "optionDialog");
  67.         try {
  68.             setSelected(true);
  69.         } catch (PropertyVetoException ignored) {
  70.         }
  71.     }
  72.  
  73.     private void createGlass() {
  74.         JDesktopPane modalGlass = new JDesktopPane();
  75.         modalGlass.setOpaque(false);
  76.         MouseInputAdapter adap = new MouseInputAdapter() {
  77.         };
  78.         modalGlass.addMouseListener(adap);
  79.         modalGlass.addMouseMotionListener(adap);
  80.         modalGlass.add(this);
  81.         modalGlass.setBounds(0, 0, getModalLayeredPane().getWidth(),
  82.                 getModalLayeredPane().getHeight());
  83.         modalGlass.setVisible(true);
  84.         setModalGlass(modalGlass);
  85.         getModalLayeredPane().setLayer(modalGlass, JLayeredPane.MODAL_LAYER);
  86.         getModalLayeredPane().add(modalGlass);
  87.         toFront();
  88.     }
  89.  
  90.     public void dispose() {
  91.         super.dispose();
  92.         getModalGlass().remove(this);
  93.         getModalLayeredPane().remove(getModalGlass());
  94.         getModalLayeredPane().repaint();
  95.     }
  96.  
  97.     public void show() {
  98.         super.show();
  99.         startModal();
  100.     }
  101.  
  102.     public void setVisible(boolean isVisible) {
  103.         super.setVisible(isVisible);
  104.         if (isVisible) {
  105.             startModal();
  106.         } else {
  107.             stopModal();
  108.         }
  109.     }
  110.  
  111.     private synchronized void startModal() {
  112.         try {
  113.             if (SwingUtilities.isEventDispatchThread()) {
  114.                 EventQueue theQueue = getToolkit().getSystemEventQueue();
  115.                 while (isVisible()) {
  116.                     getModalGlass().setBounds(0, 0,
  117.                             getModalLayeredPane().getWidth(),
  118.                             getModalLayeredPane().getHeight());
  119.                     AWTEvent event = theQueue.getNextEvent();
  120.                     Object source = event.getSource();
  121.                     if (event instanceof ActiveEvent) {
  122.                         ((ActiveEvent) event).dispatch();
  123.                     } else if (source instanceof Component) {
  124.                         ((Component) source).dispatchEvent(event);
  125.                     } else if (source instanceof MenuComponent) {
  126.                         ((MenuComponent) source).dispatchEvent(event);
  127.                     } else {
  128.                         System.err.println("Unable to dispatch: " + event);
  129.                     }
  130.                 }
  131.             } else {
  132.                 Thread.yield();
  133.             }
  134.         } catch (InterruptedException ignored) {
  135.         } finally {
  136.             dispose();
  137.         }
  138.     }
  139.  
  140.     private synchronized void stopModal() {
  141.         notifyAll();
  142.     }
  143.  
  144.     protected JLayeredPane getModalLayeredPane() {
  145.         return modalLayeredPane;
  146.     }
  147.  
  148.     private void setModalLayeredPane(JLayeredPane modalLayeredPane) {
  149.         this.modalLayeredPane = modalLayeredPane;
  150.     }
  151.  
  152.     protected JDesktopPane getModalGlass() {
  153.         return modalGlass;
  154.     }
  155.  
  156.     private void setModalGlass(JDesktopPane modalGlass) {
  157.         this.modalGlass = modalGlass;
  158.     }
  159. }

I know this thread is quite long, so thanks again for your time
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 05:43 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.