NullPointerException when asking for InternalFrameUI

07-30-07, 12:42 PM
|
 |
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:
setRootPaneCheckingEnabled(false);
My JInternalModalFrame class: (i removed the comments as they were in dutch
Java Code:
package gui; import java.awt.AWTEvent; import java.awt.ActiveEvent; import java.awt.Component; import java.awt.EventQueue; import java.awt.MenuComponent; import java.beans.PropertyVetoException; import javax.swing.JFrame; import javax.swing.JInternalFrame; import javax.swing.JLayeredPane; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.event.MouseInputAdapter; /** * @author UnrealEd * @version 25-apr-07 14:03:25 * @see JInternalFrame */ private static final long serialVersionUID = 1L; public JInternalModalFrame (Component parent ) { this(parent, ""); } this(parent, title, false); } boolean resizeable) { this(parent, title, resizeable, true); } boolean resizeable, boolean closeable) { this(parent, title, resizeable, closeable, false); } boolean resizeable, boolean closeable, boolean maximizeable) { this(parent, title, resizeable, closeable, maximizeable, false); } boolean resizeable, boolean closeable, boolean maximizeable, boolean iconifiable) { super(title, resizeable, closeable, maximizeable, iconifiable); if (parent instanceof JFrame) { setModalLayeredPane (((JFrame) parent ). getLayeredPane()); } else { setModalLayeredPane (JLayeredPane. getLayeredPaneAbove(parent )); } createGlass(); // putClientProperty("JInternalFrame.frameType", "optionDialog"); try { setSelected(true); } updateUI(); } private void createGlass() { modalGlass.setOpaque(false); }; modalGlass.addMouseListener(adap); modalGlass.addMouseMotionListener(adap); modalGlass.add(this); modalGlass.setBounds(0, 0, getModalLayeredPane().getWidth(), getModalLayeredPane().getHeight()); modalGlass.setVisible(true); setModalGlass(modalGlass); getModalLayeredPane().setLayer(getModalGlass(), getModalLayeredPane().add(getModalGlass()); toFront(); } public void dispose() { super.dispose(); modalGlass.remove(this); getLayeredPane().remove(modalGlass); getLayeredPane().repaint(); } public void show() { super.show(); startModal(); } public void setVisible(boolean isVisible) { super.setVisible(isVisible); if (isVisible) { startModal(); } else { stopModal(); } } private synchronized void startModal() { try { EventQueue theQueue = getToolkit (). getSystemEventQueue(); while (isVisible()) { modalGlass.setBounds(0, 0, modalLayeredPane.getWidth(), modalLayeredPane.getHeight()); AWTEvent event = theQueue. getNextEvent(); Object source = event. getSource(); } else { System. err. println("Unable to dispatch: " + event ); } } } else { } } finally { dispose(); } } private synchronized void stopModal() { notifyAll(); } public JPanel getModalGlass () { return modalGlass; } private void setModalGlass (JPanel modalGlass ) { this.modalGlass = modalGlass; } return modalLayeredPane; } private void setModalLayeredPane (JLayeredPane modalLayeredPane ) { this.modalLayeredPane = modalLayeredPane; } }
And eventually, the JInternalTitlelessFrame:
Java Code:
package gui; import java.awt.BorderLayout; import java.awt.Component; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JInternalFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.plaf.basic.BasicInternalFrameUI; public class JInternalTitlelessFrame extends JInternalModalFrame { private static final long serialVersionUID = 1L; private static final String emptyTitle = ""; public JInternalTitlelessFrame (Component parent ) { this(parent, false); } public JInternalTitlelessFrame (Component parent, boolean resizeable ) { super(parent, emptyTitle, resizeable); } public void dispose() { super.dispose(); } public void show() { super.show(); setRootPaneCheckingEnabled(false); } public void setVisible(boolean isVisible) { super.setVisible(isVisible); if (isVisible) { setRootPaneCheckingEnabled(false); } else { } } return northPane; } this.northPane = northPane; } public static void main(String[] args) { frame. setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); final JInternalTitlelessFrame modal = new JInternalTitlelessFrame( frame, true); modal. setContentPane(new JPanel()); modal.add("message", lb); System. out. println("Goopa! Goopa!"); modal.dispose(); } }; b.addActionListener(s); modal.add("button", b); b.addActionListener(s); modal.add("button", b); modal.add("body", lb); modal.add("body", b); modal.add("body", lb); modal.add("body", b); modal.setSize(300, 300); modal.setVisible(true); desktop.add(modal); System. out. println("output__"); } }; desktop.add(internal); button.addActionListener(showModal); Container iContent = internal. getContentPane(); internal.setBounds(25, 25, 200, 100); internal.setVisible(true); frame.setSize(500, 300); frame.setVisible(true); } }
Now the problem  : Whenever i try to run the above code (the main method), it throws a NullPointerException at this line:
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
|

07-30-07, 06:33 PM
|
|
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.
|

08-04-07, 12:35 PM
|
 |
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:
package gui.components; import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import gui.components.JInternalTitlelessFrame; import javax.swing.JButton; import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JInternalFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class TitlelessFrameTest { public static void main(String[] args) { frame. setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); frame.setVisible(true); final JInternalTitlelessFrame modal = new JInternalTitlelessFrame(frame, JInternalTitlelessFrame.CENTER_BOTH); modal. setContentPane(new JPanel()); modal.add("message", lb); System. out. println("Goopa! Goopa!"); modal.dispose(); } }; b.addActionListener(s); modal.add("button", b); b.addActionListener(s); modal.add("button", b); modal.add("body", lb); modal.add("body", b); modal.add("body", lb); modal.add("body", b); modal.setSize(200, 100); modal.setVisible(true); } }; desktop.add(internal); button.addActionListener(showModal); Container iContent = internal. getContentPane(); internal.setBounds(25, 25, 200, 100); internal.setVisible(true); frame.setSize(500, 300); frame.setVisible(true); } }
and here's a code snippet that doesn't work:
Java Code:
package gui.components; import gui.components.JInternalTitlelessFrame; import javax.swing.JPanel; import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JPasswordField; class TitlelessFrameTest2 { public static void main (String[] args) { jf.setSize(400, 300); jf. setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); JInternalTitlelessFrame loginWindow = new JInternalTitlelessFrame(jf, JInternalTitlelessFrame.CENTER_BOTH); loginWindow.setBounds(10, 10, 200, 100); loginWindow. setContentPane(new JPanel()); // place Components on window loginAttempt.setText("Login attempt at: " + date); loginWindow.add(loginAttempt); loginWindow. add(new JLabel("username:")); loginWindow. add(new JLabel("password:")); loginWindow. add(new JButton("Login")); loginWindow.setVisible(true); jf.setVisible(true); } }
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:
package gui.components; import java.awt.Component; import javax.swing.plaf.basic.BasicInternalFrameUI; /** * @author UnrealEd * @version 01-aug-07 15:33:56 */ public class JInternalTitlelessFrame extends JInternalModalFrame { /** COMMENT ME! */ private static final long serialVersionUID = 1L; private static final String emptyTitle = ""; public static final int CENTER_NOTHING = 0; public static final int CENTER_VERTICAL = 1; public static final int CENTER_HORIZONTAL = 2; public static final int CENTER_BOTH = 3; private int center = CENTER_NOTHING; public JInternalTitlelessFrame (Component parent ) { this(parent, CENTER_NOTHING); } public JInternalTitlelessFrame (Component parent, int center ) { this(parent, center, false); } public JInternalTitlelessFrame (Component parent, int center, boolean resizeable) { super(parent, emptyTitle, resizeable); setCenter(center); setAbsoluteLocation(); } public void dispose() { super.dispose(); } private int getCenter() { return center; } private void setAbsoluteLocation() { if ((getCenter() & CENTER_HORIZONTAL) == CENTER_HORIZONTAL) { int init_x = Math. round((parent. getWidth() - getWidth ()) / 2); setLocation(init_x, getY()); } if ((getCenter() & CENTER_VERTICAL) == CENTER_VERTICAL) { int init_y = Math. round((parent. getHeight() - getHeight ()) / 2); setLocation(getX(), init_y); } } private void setCenter(int center) { this.center = center; } public void setVisible(boolean isVisible) { setRootPaneCheckingEnabled(false); setAbsoluteLocation(); try { } } super.setVisible(isVisible); } public void show() { super.show(); setRootPaneCheckingEnabled(false); try { } } }
Java Code:
package gui.components; import java.awt.AWTEvent; import java.awt.ActiveEvent; import java.awt.Component; import java.awt.EventQueue; import java.awt.MenuComponent; import java.beans.PropertyVetoException; import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JInternalFrame; import javax.swing.JLayeredPane; import javax.swing.SwingUtilities; import javax.swing.event.MouseInputAdapter; /** * @author UnrealEd * @version 31-jul-07 18:13:57 * @see JInternalFrame */ /** COMMENT ME! */ private static final long serialVersionUID = 1L; public JInternalModalFrame (Component parent ) { this(parent, ""); } this(parent, title, false); } boolean resizeable) { this(parent, title, resizeable, true); } boolean resizeable, boolean closeable) { this(parent, title, resizeable, closeable, false); } boolean resizeable, boolean closeable, boolean maximizeable) { this(parent, title, resizeable, closeable, maximizeable, false); } boolean resizeable, boolean closeable, boolean maximizeable, boolean iconifiable) { super(title, resizeable, closeable, maximizeable, iconifiable); if (parent instanceof JFrame) { setModalLayeredPane (((JFrame) parent ). getLayeredPane()); } else { setModalLayeredPane (JLayeredPane. getLayeredPaneAbove(parent )); } createGlass(); //putClientProperty("JInternalFrame.frameType", "optionDialog"); try { setSelected(true); } } private void createGlass() { modalGlass.setOpaque(false); }; modalGlass.addMouseListener(adap); modalGlass.addMouseMotionListener(adap); modalGlass.add(this); modalGlass.setBounds(0, 0, getModalLayeredPane().getWidth(), getModalLayeredPane().getHeight()); modalGlass.setVisible(true); setModalGlass(modalGlass); getModalLayeredPane (). setLayer(modalGlass, JLayeredPane. MODAL_LAYER); getModalLayeredPane().add(modalGlass); toFront(); } public void dispose() { super.dispose(); getModalGlass().remove(this); getModalLayeredPane().remove(getModalGlass()); getModalLayeredPane().repaint(); } public void show() { super.show(); startModal(); } public void setVisible(boolean isVisible) { super.setVisible(isVisible); if (isVisible) { startModal(); } else { stopModal(); } } private synchronized void startModal() { try { EventQueue theQueue = getToolkit (). getSystemEventQueue(); while (isVisible()) { getModalGlass().setBounds(0, 0, getModalLayeredPane().getWidth(), getModalLayeredPane().getHeight()); AWTEvent event = theQueue. getNextEvent(); Object source = event. getSource(); } else { System. err. println("Unable to dispatch: " + event ); } } } else { } } finally { dispose(); } } private synchronized void stopModal() { notifyAll(); } return modalLayeredPane; } private void setModalLayeredPane (JLayeredPane modalLayeredPane ) { this.modalLayeredPane = modalLayeredPane; } return modalGlass; } this.modalGlass = modalGlass; } }
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
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|