简体   繁体   中英

how to call active frame that already exist from another class that extends JDialog then remove all of its component?

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

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 ~,~" )

I'm not clear on what your goal is, but if you want to change the components that are displayed in a container, such as a JFrame or JDialog's contentPane, then I recommend that you use a CardLayout to do this since it allows you to easily swap "views".

There could be two ways to do this:

  • Your JDialog class could use a reference to the JFrame that is passed in via its constructor (and you should then pass it immediately into the dialog's super constructor so that your modality will work correctly). You could then call any public methods in the JFrame's class.
  • Or since the JDialog is modal, the JFrame's code will halt while the dialog is visible. You could swap "views" immediately after the dialog has been disposed of and is no longer visible. this would keep the JFrame manipulating code in the JFrame class.
  • Edit: note that if you don't use CardLayout, then you're responsible for calling revalidate() and repaint() on any container who gets its components changed.

As an aside: since English is not your first tongue and nor is it the native language of many folks on this forum, please avoid using non-standard abbreviations. The clearer your communication with us, the easier it will be for us to understand you and help you.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM