[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7589
  • Last Modified:

How to make Modal JFrame

I'm creating a JFrame(parent) which in turn creates another JFrame(child). Until the child frame is active the parent frame should be disabled i.e it should be modal.
0
bhagwand
Asked:
bhagwand
  • 3
1 Solution
 
expertmbCommented:
hi,

  when u r calling the child frame in that method use


  parentFrame.disable();


 mb...
0
 
expertmbCommented:
hi,
  u can use
 
  parentFrame.setEnabled(false)
 
  while closing the childframe
 
   parentFrame.setEnabled(true)
 
 this will solve ur problem

 good luck
 
 mb...
0
 
expertmbCommented:
hi bhagwand,

will this help u

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

public class frameTest extends JFrame{

      public frameTest(){
      super("Frame Test") ;

      Container container = this.getContentPane();
      

      JMenuBar menuBar = new JMenuBar();
      JMenu frame = new JMenu("Frame");
      JMenuItem Frame = new JMenuItem("Child Frame");
      
      frame.add(Frame);
      menuBar.add(frame);

      setJMenuBar(menuBar);

      Frame.addActionListener(new FrameListener());


      addWindowListener(new WindowEventHandler())      ;
      setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
      setSize(500,500);
      show();

      }

      class FrameListener implements ActionListener{
            public void actionPerformed(ActionEvent ae){
                  setEnabled(false);                  
                   final JFrame frame1 = new JFrame("Child Frame");
                  frame1.setSize(300,300);
                  frame1.setEnabled(true);
                  frame1.show();
                  
               frame1.addWindowListener(new WindowAdapter(){
                    public void windowClosing(WindowEvent evt){
                    frame1.dispose();
                    setEnabled(true);
                    toFront();
                         }
                });
            }
      }
      

      public static void main(String [] args){
            new frameTest();
      }
      
      class WindowEventHandler extends WindowAdapter{
            public void windowClosing(WindowEvent evt){
                  System.exit(0);
       }
     }
}


good luck

mb...
 
0
 
heyhey_Commented:
>> parent frame should be disabled i.e it should be modal.


you can use MODAL JDialog, but not modal JFrame.
0
 
mbormannCommented:
Join the club yaar ,Merry christams man.
Click on my profile for answer.
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now