Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Issue with accessing application's JFrame component from a Dialog.

  1. #1
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default 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);
         }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default 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?
    Java Code:
    JPanel panel = new OtherPanel();
    MazePanel mp = (MazePanel) panel;
    Clearly not.

    I suggest you rethink your design.

  3. #3
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default 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.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default 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.

    db
    shall likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Issue with accessing application's JFrame component from a Dialog.

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Issue with accessing application's JFrame component from a Dialog.

    Quote Originally Posted by DarrylBurke View Post
    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.

    db
    Thanks.

    System.out.println(dialogParent.getContentPane().g etClass().getName()) returns javax.swing.JPanel.

    I found out that I needed to mazeFrame.setContentPane(mazePanel); instead of mazeFrame.add(mazePanel);.

    Once I did that, it returned MazeGame.MazePanel.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default 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 like
    Java Code:
    public GridSizeDialog(MazePanel mazePanel) {
        super(SwingUtilities.xxxx(mazePanel), true);
        mazePanel.CreateMaze(gridSize);
    }
    Look up the appropriate method of SwingUtilities in the API.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Issue with accessing application's JFrame component from a Dialog.

    Sounds like a good idea.

    I think the SwingUtilities method is windowForComponent. I'll try it out.

    Thanks

Similar Threads

  1. non-modal dialog issue
    By newbie123 in forum AWT / Swing
    Replies: 8
    Last Post: 03-21-2012, 12:05 AM
  2. a Component on top of all jframe components
    By shomid in forum AWT / Swing
    Replies: 2
    Last Post: 09-10-2011, 03:46 PM
  3. Dialog boxes closing on EVERY application.
    By Rtme in forum AWT / Swing
    Replies: 1
    Last Post: 05-17-2011, 07:54 PM
  4. Accessing JFrame from seperat file?
    By soulmed in forum AWT / Swing
    Replies: 6
    Last Post: 04-22-2011, 03:36 PM
  5. Replies: 1
    Last Post: 04-10-2011, 02:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •