Outer class jframe is not hiding when calling the inner class jframe

Posted by user2909960 on Stack Overflow See other posts from Stack Overflow or by user2909960
Published on 2013-10-23T08:22:15Z Indexed on 2013/10/23 15:54 UTC
Read the original article Hit count: 378

When i am calling the inner jframe, it is called, but outer jframe is not hiding. instead it gets overlapped. so what will be the solution for this. Is there any way to get out of this. As i tried when i am calling the inner class frame, the outer class frame is also called, and it is not hidden.

package com.exp.example;

import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;


@SuppressWarnings("serial")
public class A extends JFrame implements ActionListener {
    JFrame rframe = new JFrame();
    JLabel CFirstName;
    JTextField Cfname;
    JButton jbsubmit;
    Container cp;

    public A() {

        rframe.setSize(500, 200);
        rframe.setLocationRelativeTo(null);
        cp = getContentPane();
        cp.setLayout(null);
        setSize(550, 300);
        rframe.setTitle("Outer Frame");
        cp.setBackground(new Color(140, 180, 180));

        CFirstName = new JLabel("First Name");
        Cfname = new JTextField(10);
        jbsubmit = new JButton("PREVIEW");

        CFirstName.setBounds(10, 20, 100, 35);
        Cfname.setBounds(150, 20, 150, 25);
        jbsubmit.setBounds(190, 110, 92, 25);
        cp.add(CFirstName);
        cp.add(Cfname);
        cp.add(jbsubmit);

        jbsubmit.addActionListener(this);

        rframe.add(cp);
        rframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        rframe.setVisible(true);
    }

    public void actionPerformed(ActionEvent ae) {
        String action = ae.getActionCommand();


        if (action == "PREVIEW") {
            /* Write the code here
             * When we click on preview button the frame of outer class(class A) gets
             * deactivated(closed) and inner frame, frame of inner class(class B) gets visible.
             * it should not be overlapped.  
             */
            /* My Code */
            new B();
            rframe.setVisible(false);

        }
    }

    public class B {
        JFrame frm = new JFrame();
        Container cp;

        public B() {
            frm.setSize(500, 200);
            frm.setLocationRelativeTo(null);
            cp = getContentPane();
            cp.setLayout(null);
            setSize(550, 300);
            frm.setTitle("Inner Frame");
            cp.setBackground(new Color(140, 180, 180));

            JLabel cpn = new JLabel("hello");
            cpn.setBounds(10, 20, 100, 35);
            cp.add(cpn);

            frm.add(cp);
            frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frm.setVisible(true);
        }
    }

    public static void main(String[] args) {
        new A();
    }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about swing