Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8581479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:03:38+00:00 2026-06-11T21:03:38+00:00

I have class main extends jframe , it has a button that calls /shows

  • 0

I have class main extends jframe, it has a button that calls /shows another class that extends jdialog.

If the button from jdialog is triggered, it will dispose that dialog and will remove all component of jframe, then add it to a new jpanel.

What should I do?

Here’s my new broken code:

public class mainz extends JFrame{
mainz(){
    setVisible(true);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);

    JToolBar r = new JToolBar();
    r.add(Box.createHorizontalGlue());
    add(r, BorderLayout.NORTH);

    JButton n = new JButton();
    r.add(n, BorderLayout.EAST);

    n.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae){
            show();
        }
    });
}

public void  show(){
    dialogz d = new dialogz(this);
    d.setVisible(true);
}

public void lastHope(){
    getContentPane().removeAll();
    getContentPane().validate();
    getContentPane().repaint();
}

public static void main (String[]args){
    new mainz().setExtendedState(MAXIMIZED_BOTH);
}

}

public class dialogz extends JDialog{
public dialogz(final mainz owner) {
    setSize(300, 300);
    JButton n = new JButton("execute");
    add(n);

    final JFrame ew = (JFrame)super.getOwner();// <<

    n.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae){
            dispose();
            //owner.lastHope;
            ew.removeAll();// <<
            ew.validate();// <<
            ew.repaint();// <<
        }
    });
}

void yes(){
    getOwner().removeAll();

    getOwner().validate();
    getOwner().repaint();
}

}

I know I can prevent my main class from extending jframe, and call it from main instead, but I want to do it like that…

Please help me … T-T

Sorry for my English, I from a far away country ~,~”

update:
the error is

java.lang.ClassCastException: javax.swing.SwingUtilities$SharedOwnerFrame cannot be cast to javax.swing.JFrame

it will be done with delete the line that contain
// <<
then call lastHope();

but i think there’s a another way to get that existing jframe to removeall
(by casting it first or something ~,~” )

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:03:40+00:00Added an answer on June 11, 2026 at 9:03 pm

    You are calling getParent() but you never set the parent (or owner). That should happen in the constructor as already pointed out. Also, be mindful that getParent() returns a Container object and getOwner() returns a Window object. Both of these refer to the JFrame which is the parent and owner. If you want to use it as a JFrame, you’ll have to cast the output as (JFrame). But removeAll() is in Container class so if that’s all you want, there’ll be no need for casting.

    Update:

    JFrame frame = new JFrame();
    JDialog dialog = new JDialog(frame);//frame is owner
    
    JFrame parentOfDialog = (JFrame)(dialog.getParent());
    //OR
    //JFrame parentOfDialog = (JFrame)(dialog.getOwner());
    parentOfDialog.removeAll();
    

    If you are using your custom class, pass JFrame in the constructor and call super.

    Please read the javadoc on JDialog before you try to use it. Also, read more about inheritance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a main class and another class that has a variable I
Basically I have a GUI that inherits from the JFrame class and has its
I have a main class in a program that launches another class that handles
I have a Backbone Router: class X.Routers.Main extends Backbone.Router routes: '/': 'home' 'pageb': 'actionb'
I have a class class with main thread public class MainThread extends Thread {
I have a Class(call it main class) and the method in main class calls
I have a main class(which is basically a netbeans form;drag and drop) from where
I have a Main class and a HelloWorld class. I have created a button
I have a class which extends JFrame and forms the GUI of my program.
I have this class, let's say, Foo . It extends JFrame and is a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.