Results 1 to 8 of 8
- 05-17-2012, 01:56 AM #1
Senior Member
- Join Date
- Apr 2012
- Posts
- 199
- Rep Power
- 0
Issue with accessing application's JFrame component from a Dialog.
Hello,
My program has a dialog box that needs to access its parent's JPanel. In the code shown below, Line 11 throws a "Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JPanel cannot be cast to MazeGame.MazePanel".
My program passes the application's JFrame and modal = true to the GridSizeDialog constructor. The application's JFrame contains a mazePanel, i.e. a JPanel.
What am I doing wrong?
Java Code:public class GridSizeDialog extends javax.swing.JDialog { private int gridSize; private final JFrame dialogParent; public GridSizeDialog(java.awt.Frame parent, boolean modal) { super(parent, modal); dialogParent = (JFrame) parent; MazePanel mazePanel = (MazePanel) dialogParent.getContentPane(); mazePanel.CreateMaze(gridSize); } }
- 05-17-2012, 02:05 AM #2
Re: Issue with accessing application's JFrame component from a Dialog.
The error is telling you exactly what is wrong, you cannot cast a JPanel to a MazePanel. Surely you have heard something like: all beagles are dogs but not all dogs are beagles.
Why doesn't it allow it? Suppose a have another class that extends JPanel called OtherPanel which has no relation to MazePanel. Could I do this?
Clearly not.Java Code:JPanel panel = new OtherPanel(); MazePanel mp = (MazePanel) panel;
I suggest you rethink your design.
- 05-17-2012, 02:21 AM #3
Senior Member
- Join Date
- Apr 2012
- Posts
- 199
- Rep Power
- 0
Re: Issue with accessing application's JFrame component from a Dialog.
I can understand what you said. Essentially, you said a wolf is an animal and a cat is an animal, but a wolf is not a cat.
How can that be similar to my situation?
The JFrame (or parent) my program passed to the the JDialog class contains a mazePanel and so, I would think, getContentPane should return the mazePanel. Actually it returns a container, which should be the mazePanel.
- 05-17-2012, 03:07 AM #4
Re: Issue with accessing application's JFrame component from a Dialog.
When in a situation like this, I sysout whatever information I can get, just to check my assumptions. You could assign the value returned by getContentPane() and sysout the value and the result of its getClass().getName().
Adding a component to a frame adds it to the BorderLayout.CENTER of the frame's content pane. It doesn't set the component as the content pane.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-17-2012, 03:08 AM #5
Re: Issue with accessing application's JFrame component from a Dialog.
Moved from New to Java
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-17-2012, 04:18 AM #6
Senior Member
- Join Date
- Apr 2012
- Posts
- 199
- Rep Power
- 0
Re: Issue with accessing application's JFrame component from a Dialog.
- 05-17-2012, 06:59 AM #7
Re: Issue with accessing application's JFrame component from a Dialog.
Now that that's sorted out, I'd like to say that I think you have your design back to front. If the dialog needs a reference to the MazePanel, one should be passed as a constructor parameter.
You can use a static method of SwingUtilities to get a reference to the window that contains the MazePanel, to pass to the super implementation.
Also, if your application always uses the GridSizeDialog as only modal or only non-modal, that boolean should be hardcoded into the call to the super implementation and not passed as a parameter. Assuming here that the dialog is always modal, the constructor would look likeLook up the appropriate method of SwingUtilities in the API.Java Code:public GridSizeDialog(MazePanel mazePanel) { super(SwingUtilities.xxxx(mazePanel), true); mazePanel.CreateMaze(gridSize); }
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-17-2012, 07:25 AM #8
Senior Member
- Join Date
- Apr 2012
- Posts
- 199
- Rep Power
- 0
Similar Threads
-
non-modal dialog issue
By newbie123 in forum AWT / SwingReplies: 8Last Post: 03-21-2012, 12:05 AM -
a Component on top of all jframe components
By shomid in forum AWT / SwingReplies: 2Last Post: 09-10-2011, 03:46 PM -
Dialog boxes closing on EVERY application.
By Rtme in forum AWT / SwingReplies: 1Last Post: 05-17-2011, 07:54 PM -
Accessing JFrame from seperat file?
By soulmed in forum AWT / SwingReplies: 6Last Post: 04-22-2011, 03:36 PM -
adding component(object?) to JFrame after actionPerformed
By Shargath in forum New To JavaReplies: 1Last Post: 04-10-2011, 02:06 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks