To close JFrame over a JDialog

Hi,
I am facing a slight problem. Say I have a code like under
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class abc extends JDialog implements ActionListener
     JPanel jp1 = new JPanel();
     JButton ok1 = new JButton("OK");
     public abc()
          this.getContentPane().setLayout(new BorderLayout());
          this.getContentPane().add(jp1,BorderLayout.CENTER);
          jp1.add(ok1);
          ok1.addActionListener(this);
          ok1.setActionCommand("ok1");
     public void actionPerformed(ActionEvent e)
          if (e.getActionCommand().equals("ok"))
               System.out.println("Inside action performed method");
          else if (e.getActionCommand().equals("ok1"))
          JFrame j1 = new JFrame();
          JPanel jp2 = new JPanel();
          JButton btn1 = new JButton("OK");
          j1.getContentPane().add(jp2);
          jp2.add(btn1);     
          btn1.addActionListener(this);
          btn1.setActionCommand("ok");
          j1.show();
          j1.pack();
          j1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
public static void main(String args[])
          abc ABC = new abc();
          ABC.show();
          ABC.pack();
Now here in this code the line
j1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
closes the application itself. What I want is only the JFrame closes while the main Dialog stills stays in context.
Can someone help with me how I can achieve the same.
Regards.
Anand

There is a dialog it has a OK button. When I click the OK button a frame opens with another OK1 button in it.
Now if I click the "x" sign of the frame it should be disposed without doing anything. frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)But if I click the OK1 button then also the frame should be disposed but
after doing something other processing corresponding to the OK1 button.
OK1.addActionListener( new ActionListener() {
    public void actionPerformed(ActionEvent e) {
          // do processing
         frame.setVisible(false);
         // add this if you want to remove the frame from memory and recreate it when it needs to be opened
         frame.dispose();
})>
And within all this the parent(JDialog) and the child(JFrame) should be modal.A JFrame unfortunately cannot be modal, so you shd use a JDialog instead.
ICE

Similar Messages

  • How to Close Planned Over Heads in product costing

    HI,
    We are maintaining planned over heads through KP26 and Costing sheet.Kindly tell me how to close planned over head wether it is periodicall closing or other?
    Kindly provide me the guide lines  how to do this Planned overheads closing in the system?How many ways are there to close planned overheads in the system?
    Kindly adviice me?
    Thanks
    Sunitha

    Dear Sunitha,
    To run the Cost estimate or close the planned overhead KP26 and Costing Sheet is not enough you have to move forward and you need to define Cost component Structure (OKTZ) Path: SPRO > IMG > Controlling > Product Cost Controlling > Product Cost Planning > Basic Setting for Material Costing > Define Cost Component Structure. After maintaining cost component structure you need to define Costing variant T.code OKKN Path: SPRO > IMG > Controlling > Product Cost Controlling > Product Cost Planning > Material Cost Estimate with Quantity Structure . Here you copy PPC1 standard and create your own costing Variant.
    After Maintaining the Configuration go to T.code CK11n and run your costing and if it is correct then save it. To update Price in MM run T.code CK24 and release Material and update it.
    thx.
    Ganpat

  • Close JFrame by press JButton

    i wonder how to close JFrame...
    i know that DeafultCloseOperation... to this for me by pressing X...
    but how can i close the JFrame by pressing JButton...
    what do i have to do?
    to create new Event to do this or what?
    thx
    cya

    You can just call dispose();
                 btnClose.addActionListener(
                            new ActionListener()
                                   // pass query to tthe frame
                                 public void actionPerformed( ActionEvent event )
                                     dispose();
                                 } // end actionPerformed
                             }  // end ActionListener inner class         
                          ); // end call to addActionListener 

  • Close JFrame

    hi all,
    i have a JFrame. on that frame i put a JTabbedPane. on this pane i add a JPanel. and on this panel there is a JButton "exit". when this button is clicked, a JDialog opens that asks the user if he really wants to quit. when the user clicks "yes", i would like the frame to close, without using the System.exit() command (since the frame is opened from another JFrame, and that one isn't supposed to shut down).
    does any of you know how to do that? thanks a lot people!

    thanks woodi,
    i know that i could use these methods, but the problem is i cannot tell the program which frame to close; since i'm using:
    JFrame --> JTabbedPane --> JPanel --> JButton --> JDialog
    how to tell the JDialog that i want to distroy the JFrame?

  • JFrame references in JDialog constructors.

    If you have a single JFrame that houses your GUI and have Dialogs scattered through out your application, what is the best way to make create each dialog with a reference to that one master JFrame.
    Currently, I am passing the JFrame reference all over the place. Practically every constructor now has a JFrame as part of its argument list. The object graph is fairly deep, because I have screens that open sub screens, I have panels that are resused among screens; I have table editor that popup messages; etc; etc. It would be so nice if I didn't have to pass this JFrame reference to ever thing I created.
    I could use the Singleton pattern, but I heard this was a bad idea.
    Can someone give me some guidance, and perhaps an example?
    Thanks.

    When I say "Dialogs scattered throughout the application", I'm trying to say that I have one JFrame with a main method. When launched through the main method, the primary view is loaded. From there, the user can click in different areas to launch "detail" screens. Within these "detail" screens, I have fields that need validation. These validations require a lot of messages to be generated for the user. To do this, I use JOptionPane. JOptionPane requires a JFrame. (Technically, the JFrame is optional, but without it you run into other issues.)
    For example, here's some I have plugged into one of my JTable Editors:
      public boolean stopCellEditing(){
        String startTime = (String)_table.getModel().getValueAt(_row, _col - 1);
        if(startTime == null || "".equals(startTime)){
          return super.stopCellEditing();
        Float start = getTimeAsDecimal(startTime);
        Float end = getTimeAsDecimal(_editor.getText());
        if(end < start){
          Object response = Message.showMessageDialog(_frame, "MESSAGE", JOptionPane.INFORMATION_MESSAGE, "End must be after start");
          _editor.setBackground(Color.RED);
          _editor.setForeground(Color.WHITE);
         _editor.requestFocus();
          return false;
        return super.stopCellEditing();
      }You will notice that I have "_frame" as a variable in my showMessageDialog. I have to pass this "_frame" object from the very top, pass it into the JDialog that houses the JTable, then pass it into the editor that has this message. Now, this is only three layers. There are occasions where I have to pass it even more layers.
    Is this bad architecture? Can it be done better or more efficiently?
    Why is it bad to have a singleton JFrame object? How is everyone else doing it?
    Thanks.
    Edited by: Kazan on Jan 21, 2009 2:11 PM

  • Hiding/showing JFrame with active JDialog

    Hi,
    I have a problem with hiding and re-showing a JFrame that contains an active JDialog.
    I have a JFrame (named operatorFrame) that holds the main application and a JFrame that I use for showing a screensaver. When the screensaver has to be shown I perform a hide() of the operatorFrame and a show() of the screensaver frame.
    These actions are done in a Runnable that is called through SwingUtilities.invokeAndWait(). Now when the operatorFrame contained an opened JDialog and the screensaver started, when closing the screensaver the operatorFrame.show() method is called and there it stops. The instruction that follows the show() is never reached.
    Does anybody have any ideas?
    Thanks,
    Taaje

    I understand that but then why does it work when I run the same code in Windows (the normal environment is Red Hat Linux)?
    I just found out that it works when running it under Windows.

  • Change jFrame to be jDialog

    I wrote a big application using mostly jFrames as pop up screens to collect data. I only realized now I should have used jDialogs instead. Is there a way to convert the jFrames to jDialog instead of writing all new??

    question is - is there a way to convert a jFrame to be a jDialog?They are the same. You use either:
    JFrame frame = new JFrame();
    or
    JDialog dialog = new JDialog();
    They both use a content pane and you add components to the content pane the same way whether its a frame or a dialog.
    So your question doesn't make sense. You go through the code and change JFrame to JDialog. That is the simplest way.
    Of course it is not the best design as was stated in the first reply.

  • Question on not to close JFrame when close button is clicked

    Hi All,
    Good Morning
    I have frame created like:
    JFrame frame=new JFrame();When we give
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);The above statement closes the frame but i want the same frame to be hidden in the statusbar.So can any one help me in providing any built in method if there or else how to do it??
    Thanks in advance.
    regards,
    Viswanadh

    but i want the same frame to be hidden in the statusbar.Thats what the "minimize" button is for. Don't mix the functionality of the two buttons.

  • Btn in JFrame to Open JDialog

    Hi Guys
    In NetBeans GUI Builder I have created and designed a MainGUI (JFrame Form) that has some buttons. I also went ahead and created and designed a SubGUI (JDialog Form).
    What do I need to do to link MainGUI to SubGUI through the press of a button ????
    I have added the ActionPerformed / ActionEvent method to the button, and inside that I have tried:
    SubGUI.setVisible(true);but it doesn't work !!
    I would greatly appreciate any help.
    Thank You.

    Here is the other bit that I think might be relevant:
    private void starterBtnActionPerformed(java.awt.event.ActionEvent evt) {                                          
            StarterDialog.setVisible(true);
        private void maincourseBtnActionPerformed(java.awt.event.ActionEvent evt) {                                             
            MainCourseDialog.setVisible(true);
        private void dessertBtnActionPerformed(java.awt.event.ActionEvent evt) {                                          
            DessertDialog.setVisible(true);
        private void adminBtnActionPerformed(java.awt.event.ActionEvent evt) {                                        
            AdminDialog.setVisible(true);
        private void starterListMouseClicked(java.awt.event.MouseEvent evt) {                                        
            Starter selectedStarter = (Starter)starterList.getSelectedValue();
            if (selectedStarter != null)
                populateSDialog(selectedStarter);
        private void sOKBtnActionPerformed(java.awt.event.ActionEvent evt) {                                      
            // TODO add your handling code here:
        private void sCancelBtnActionPerformed(java.awt.event.ActionEvent evt) {                                          
            this.dispose();
        private void maincourseListMouseClicked(java.awt.event.MouseEvent evt) {                                           
            MainCourse selectedMC = (MainCourse)maincourseList.getSelectedValue();
            if (selectedMC != null)
                populateMCDialog(selectedMC);
        private void mcOKBtnActionPerformed(java.awt.event.ActionEvent evt) {                                       
            // TODO add your handling code here:
        private void mcCancelBtnActionPerformed(java.awt.event.ActionEvent evt) {                                           
            // TODO add your handling code here:
        private void dessertListMouseClicked(java.awt.event.MouseEvent evt) {                                        
            Dessert selectedDessert = (Dessert)dessertList.getSelectedValue();
            if (selectedDessert != null)
                populateDDialog(selectedDessert);
        private void dOKBtnActionPerformed(java.awt.event.ActionEvent evt) {                                      
            // TODO add your handling code here:
        private void dCancelBtnActionPerformed(java.awt.event.ActionEvent evt) {                                          
            // TODO add your handling code here:
        private void populateSDialog(Starter s1)
            sNameJLi.setText(s1.getName());
            sCalsJLi.setText(String.valueOf(s1.getCalories()));
            sDescTA.setText(s1.getDescription());
            sPriceJLi.setText(String.valueOf(s1.getPrice()));
        private void populateMCDialog(MainCourse mc1)
            mcNameJLi.setText(mc1.getName());
            mcCalsJLi.setText(String.valueOf(mc1.getCalories()));
            mcDescTA.setText(mc1.getDescription());
            mcPriceJLi.setText(String.valueOf(mc1.getPrice()));
        private void populateDDialog(Dessert d1)
            dNameJLi.setText(d1.getName());
            dCalsJLi.setText(String.valueOf(d1.getCalories()));
            dDescTA.setText(d1.getDescription());
            dPriceJLi.setText(String.valueOf(d1.getPrice()));
        * @param args the command line arguments
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new MainGUI().setVisible(true);
        }

  • Problem with JFrame, JInternalFrame and JDialog

    problem solved..sorry

    I have just found this post:
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=302473
    And the first way seems to be the only way to solve the problem... :(
    Now the question is, does it exist a way to refresh a JFrame after that I have uset setVisible(true)?
    If it does exist I could have something like this:
    setVisible(true)
    new panel(getInsets(););
    contpan.add(panel);
    REFRESH();

  • Make jFrame wait for jDialog to finish

    I am making a project in Netbeans i have a main Frame and some Dialog windows that open up if you press some buttons.
    when one window opens i want the main Frame script to pause and wait for it to complete.
    i have tried 3 different ways:
    questionDialog.setVisible(true);
    while (questionDialog.isVisible()) {
        try {
            Thread.sleep(200);
        } catch (Exception e) {
    } but that shows the frame of the dialog box but never loads the inside and the whole application freezes.
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            questionDialog.setVisible(true);
    while (questionDialog.isVisible()) {
        try {
            Thread.sleep(200);
        } catch (Exception e) {
    }that loaded my Dialog box but the script never waits for it to close i assume it is because it waits until "later" to show the dialog box and in that time it skips the while loop.
    Thread t = new Thread(new Runnable() {
        public void run() {
            questionDialog.setVisible(true);
            while (questionDialog.isVisible()) {
                try {
                    Thread.sleep(200);
                } catch (Exception e) {
    t.start();
    System.out.println("Dialog started");
    System.out.println(questionDialog.isVisible());
    try {
        t.join();
    } catch (Exception e) {
    }but that had the same result as the first code it waited but never showed the dialog.
    where am i going wrong how should i go about this?
    Scott.
    EDIT:
    by finish i mean become not Visible. so isVisible() will return false
    Edited by: ratcateme on Nov 20, 2008 3:02 PM

    You don't have to do anything to do that
    questionDialog.setVisible(true), this method will block the thread, it would't keep going until the questionDialog is unvisible, this is the mistake of the way 1 and way 3.
    and the way 2: the while block will be execute before the questionDialog.setVisible(true) is execute, so questionDialog.isVisible() will return false
    my en is very poor, good luck!

  • Close JFrame in Netbeans

    Hello,
    I have a java application in NetBeans. I have been using Netbean absolut layout. I didn´t create JFrame class variable at all, instead using:Ok is a button
    ok.setFont(new java.awt.Font("Arial", 0, 10));
    ok.setToolTipText("ok");
    ok.setLabel("ok");
    ok.setMargin(new java.awt.Insets(2, 2, 2, 2));
    getContentPane().add(ok, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 150, 60, 20));
    ok.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    okPerformed(evt);
    I don´t want to use System.exit(0); because it will shut down whole system. In order to use dispose method, I created a JFrame class variable in beginning of the class, but it doesn´t work.
    JFrame frame = new JFrame();
    public void okPerformed (java.awt.event.ActionEvent evt){
    this.frame.dispose();
    Can anyone help me?
    Thanks

    hello,
    Thanks for replying my message so fast.
    I tried, but it still doesn´t work. I am using netbeans absolute Layout. It doesn´t really have frame. Here is my code
    public class tt extends JFrame{
    JFrame frame = new JFrame();
    JTextField text1= new JTextField(20);
    JButton ok = new JButton();
    public tt throws Exception
    setSize(400,300);
    setTitle("tt");
    getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
    text1.setFont(new java.awt.Font("Arial",0,12));
    text1.setBounds(5, 8, 5, 5);
    text1.setEditable(true);
    getContentPane().add(text1, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 30, 100, 20));
    ok.setFont(new java.awt.Font("Arial", 0, 10));
    ok.setToolTipText("ok");
    ok.setLabel("ok");
    ok.setMargin(new java.awt.Insets(2, 2, 2, 2));
    getContentPane().add(ok, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 150, 60, 20));
    ok.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    okPerformed(evt);
    setVisible(true);
    public void okPerformed (java.awt.event.ActionEvent evt){
    setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    Thanks

  • Move a JFrame over the screen by specifying coordinates

    Hi,
    I have a JPanel within a JFrame.
    All I wanted to do is this. I have an image loaded in that JPanel
    using MediaTracker. I can move that image anywhere inside the JPanel.
    Now ,I want to "simulate" what is happening within this JPanel in the
    entire Screen. That is, whenvever I move the image inside the JPanel,
    there should be a (different) JFrame application on the screen and it should move according to the "relative" screen coordinates.
    I thought I could use SwingUtilities.convertPointToScreen()
    function , but really dont know how to proceed exactly.ie., how to
    move a JFrame application anywhere in the screen not manually but
    moving it based on the relative coordinates I retrieve from the JPanel.
    The "simulation" here is that a JFrame in the screen
    is equated to an image in a JPanel.
    Any help would be REALLY appretiated.
    Thanks,
    Pullu

    Don't know what you're really looking for, but try:
    Rectangle parentRect=SwingUtilities.getRoot(myJPanel).getBounds();
    and see if it gives you the info you need.
    ;o)
    V.V.

  • Algo and close JFrame question

    Just a question. Basically, this is just my main method:
        public static void main(String[] args) {
            //Launch window ==============================================
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    game.launchFrame();
            //============================================================
        }It launches the frame of my game, which is a domino game. There's a bunch of code there (done), and at the end of a game, it asks whether the user wants to play another game, or quit the program. Quitting the program means just using System.exit(0).
    But what about if the user wants to play another game? I was thinking to just close the frame and launch another one. So I put "isEnd = true" if the user wants another game, and modified the main method:
            while (isEnd == false){
            //Launch window ==============================================
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    game.launchFrame();
            //============================================================
            }But it's totally flawed. It repeatedly tries to launch the same frame, so it's messy as hell.
    What do you guys suggest so the program can start a new game?

    so...
    we have the frame -> "DominoFrame.java"
    we have the GUI builder -> "MyDominoGUIBuilder.java"
    we have the class for storing domino -> "DominoStorage.java"
    and the Domino classes -> "DominoSet.java" , "Domino.java", etc...
    when you launch the app in DominoFrame, MyDominoGUIBuilder uses a new DominoStorage
    to initialize and manage the Domino game...
    when you wanna start a new game, you can "destroy" the DominoStorage and creating
    a new one and call the MyDominoGUIBuilder to create a new game, etc...

  • Non-modal JDialog hides its JFrame "owner"

    I'm writing my first significant swing application. It has a JFrame that launches a JDialog. When constructing the JDialog, I have the JFrame as the owner and set it to be non-modal. The dialog is indeed non-modal, since it allows me to click on the JFrame and run it. However, if the two windows overlap, the JDialog is always on top, covering the JFrame, even if the JFrame has focus. This can make it difficult to effectively work with the JFrame while the JDialog is on the screen.
    There must be a simple solution to this, but I haven't discovered it yet. Any ideas?

    Thank you so much for your reply. I just confirmed that passing null to the JDialog does indeed allow the JFrame to overlay it. However, now if I minimize the JFrame, the JDialog stays up. I'd prefer that it go disappear with the JFrame. Is there a way to "have it both ways"? That is, Is there a way to make the JDialog go away when the JFrame is minimized, but still make it possible for the JFrame to overlay the JDialog?
    Thanks again for the original reply.
    TIA on this one, too!
    Tim

Maybe you are looking for

  • How to pass the table name dynamically in xml parsing

    Hi All, I have created one procedure for parsing xml file which is working perfectly. FORALL i IN t_tab.first .. t_tab.last          INSERT INTO Test_AP VALUES t_tab(i);Now I want to put the table name dynamically+. For that I have added one query an

  • Is Office 2008 for Mac more compatible with PC's ?

    Hi, I currently have iWork 09. Sometimes, I save my documents to a .doc format for instance, but when I re-open it, I always have error messages saying that this thing or that thing has been modified to fit the current format etc... If I use office 2

  • Printing 16bit tiff with ID and Leopard

    Does anyone know what ID does to 16bit tiffs when print in Leopard with a 16bit capable driver? Since I can place 16bit tiffs in ID does ID pass it along when printing or does ID convert it to 8bit?

  • Proper way to Module Code for Problem

    Hi I am looking solution for this problem If any one can help me out ..... At ACME a "job" is a group of print items. JOB For example, a job can be a run of business cards, envelopes, and letterhead together. Some items qualify as being sales tax fre

  • Embedding youtube channel into muse

    hello, I know how to embed a YouTube video into muse. Is there a way I can embed the channel so it can automatically show the most recent video that has been posted. Is this possible to do? Thanks Ben.