Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    surot's Avatar
    surot is offline Member
    Join Date
    Mar 2008
    Posts
    10
    Rep Power
    0

    Default How to make a modal JFrame?

    hi friends,
    I have 2 jFrame one is the main jFrame and the other one is the sub jFrame. What im trying to do is when i show the sub jFrame, the user cannot do anything in the main jFrame(e.g. the user cannot click any button).I think it can be done in jDialog and by using the setModal method however i want to know if this is possible in jFrame.Awaiting for your prompt reply.thanks.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    It should be possible as far as I know. I have done so many such things you are talking about. What properties you have set on the main frame?

  3. #3
    surot's Avatar
    surot is offline Member
    Join Date
    Mar 2008
    Posts
    10
    Rep Power
    0

    Default

    code :

    /*
    * mainJFrame.java
    *
    * Created on April 10, 2008, 3:27 PM
    */

    package com.modalForm;

    public class mainJFrame extends javax.swing.JFrame {

    /** Creates new form mainJFrame */
    public mainJFrame() {
    initComponents();
    }

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

    jButton1.setText("show sub JFrame");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton1ActionPerformed(evt);
    }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(138, 138, 138)
    .addComponent(jButton1)
    .addContainerGap(182, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(66, 66, 66)
    .addComponent(jButton1)
    .addContainerGap(273, Short.MAX_VALUE))
    );

    java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize ();
    setBounds((screenSize.width-443)/2, (screenSize.height-396)/2, 443, 396);
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    subJFrame sub = new subJFrame();
    sub.setVisible(true);
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new mainJFrame().setVisible(true);
    }
    });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    // End of variables declaration

    }

    ========
    /*
    * subJFrame.java
    *
    * Created on April 10, 2008, 3:29 PM
    */

    package com.modalForm;

    /**
    *
    * @author IT Department
    */
    public class subJFrame extends javax.swing.JFrame {

    /** Creates new form subJFrame */
    public subJFrame() {
    initComponents();
    }

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

    jButton1.setText("Close");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton1ActionPerformed(evt);
    }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(154, 154, 154)
    .addComponent(jButton1)
    .addContainerGap(187, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(77, 77, 77)
    .addComponent(jButton1)
    .addContainerGap(200, Short.MAX_VALUE))
    );

    pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    this.dispose();
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new subJFrame().setVisible(true);
    }
    });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    // End of variables declaration

    }

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    surot's Avatar
    surot is offline Member
    Join Date
    Mar 2008
    Posts
    10
    Rep Power
    0

    Default

    Upon clicking the "show sub Jframe" button the sub JFrame shows also in the screen right? Now, i dont want the user to interact to the main JFrame(it should be locked for interaction) until the sub JFrame has been closed...i hope i explain it clearly.thanks...

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Ah, I got the point. There is one tricky way to do this using JFrame. Set the location, I mean the initial display location, in to same values in both frames. And then on the sub frame, set the always on top property to true. Then try.

    But in that case you have to hide the title bar, if not user can move anywhere and do the work.

    Still it is not ok, because on the system tray you can both frames. You can move frames there also.

    In this type of situations, used JDialog box.

  7. #7
    surot's Avatar
    surot is offline Member
    Join Date
    Mar 2008
    Posts
    10
    Rep Power
    0

    Default

    ok..I will try what you've said..Once again..thank you...

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    :) Anytime, try it and see what happened. As far as I remember, there is no other way to do this, using properties of JFrames.

    I'm saying, not remember ;)

  9. #9
    Joe2003 is offline Member
    Join Date
    Jan 2008
    Posts
    21
    Rep Power
    0

    Default

    I dont think you can set the modal value on a JFrame. From past experince I think you have to use a JDialog?

    cant rememeber, might be wrong

  10. #10
    surot's Avatar
    surot is offline Member
    Join Date
    Mar 2008
    Posts
    10
    Rep Power
    0

    Default

    I have solved the modal mode of JFrame by using the setEnabled method. Below is my code.

    /*
    * mainJFrame.java
    *
    * Created on April 10, 2008, 3:27 PM
    */

    package com.modalForm;

    public class mainJFrame extends javax.swing.JFrame {

    /** Creates new form mainJFrame */
    public mainJFrame() {
    initComponents();
    }

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

    jButton1.setText("show sub JFrame");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton1ActionPerformed(evt);
    }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(138, 138, 138)
    .addComponent(jButton1)
    .addContainerGap(182, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(66, 66, 66)
    .addComponent(jButton1)
    .addContainerGap(273, Short.MAX_VALUE))
    );

    java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize ();
    setBounds((screenSize.width-443)/2, (screenSize.height-396)/2, 443, 396);
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    subJFrame sub = new subJFrame();
    sub.setVisible(true);
    this.setEnabled(false);
    }

    private void formWindowActivated(java.awt.event.WindowEvent evt) {
    this.setEnabled(true);
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new mainJFrame().setVisible(true);
    }
    });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    // End of variables declaration

    }

    ========
    /*
    * subJFrame.java
    *
    * Created on April 10, 2008, 3:29 PM
    */

    package com.modalForm;


    public class subJFrame extends javax.swing.JFrame {

    /** Creates new form subJFrame */
    public subJFrame() {
    initComponents();
    }

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

    jButton1.setText("Close");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton1ActionPerformed(evt);
    }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(154, 154, 154)
    .addComponent(jButton1)
    .addContainerGap(187, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(77, 77, 77)
    .addComponent(jButton1)
    .addContainerGap(200, Short.MAX_VALUE))
    );

    pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    this.dispose();
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new subJFrame().setVisible(true);
    }
    });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    // End of variables declaration

    }


    ===
    Now my problem is how will i set the mainJFrame as the active form after closing the subJFrame?

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Make back to setEnable() true, after disposing the sub frame.

    By the time pal, when you put code, please use tags. It's really helpful for us. :)

  12. #12
    saul1905 is offline Member
    Join Date
    Jan 2009
    Posts
    1
    Rep Power
    0

    Default

    thank. the code is very good

  13. #13
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by saul1905 View Post
    thank. the code is very good
    Completely unnecessary, though. You can do everything in a JDialog that you can in a JFrame (and in the same manner), and can make it modal simply by passing a boolean to the constructor.

    Why do people insist on fitting square pegs into the round holes?

  14. #14
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Masijade is right. JDialog is available for this purpose. JFrame is a container; by creating two of them, you essentially have two independent windows. Changing the second JFrame to JDialog should involve little effort, and your program will make much more sense.

    Spend a little extra time to learn the "right" way now, and you will know it for the rest of your career.

  15. #15
    olkin is offline Member
    Join Date
    Jan 2009
    Posts
    3
    Rep Power
    0

    Default

    But if you are using JDialog, it's very difficult to "switch off" the close button. JFrame option in this case is more helpful, I think.

  16. #16
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by olkin View Post
    But if you are using JDialog, it's very difficult to "switch off" the close button. JFrame option in this case is more helpful, I think.
    Once again, you can do everything with a JDialog that you can with a JFrame. There is no reason why you can't assign your own WindowListener to the JDialog. I can't think of any reason why you would really want to though. I see no reason why you would need to "switch off" the close button. If the app is written well enough to realise that the user didn't "interact properly" with the JDialog (if that's even really necessary), and put it back up, then you probably shouldn't be doing the stuff in the first place.

  17. #17
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    > it's very difficult to "switch off" the close button.
    Difficult? setDefaultCloseOperation(DO_NOTHING_ON_CLOSE)

    db

  18. #18
    olkin is offline Member
    Join Date
    Jan 2009
    Posts
    3
    Rep Power
    0

    Default

    With "switch off" the close button I mean disabling it, making it not visible. Because when it is visible - it means that user can press it and he expects avoiding of the step.
    My case is as following: user HAS to choose between some value. In case he just closes the dialog - it means that he is not making any choice. That's why I want to "switch" close button off.
    My workaround for this now - after pressing close button, the user is getting message about his fault and the same dialog appears again.
    But... you see, in case he doesn't see this button, he has obligation to make choice without any further notification.

  19. #19
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Try setting

    myJDialog.setUndecorated(true);

    This will remove the entire title bar, with the window controls.

    You might also look into JOptionPane.showOptionDialog(). It is intended to simplify prompting the user to select from a list of values.

    Also, the user should be allowed to cancel, even if that causes the application to close. Forcing them to make a selection and go on is a bit brutal...

  20. #20
    olkin is offline Member
    Join Date
    Jan 2009
    Posts
    3
    Rep Power
    0

    Default

    Hi Steve11235,

    I've tried to make it undecorated (btw, it should be done before the dialog is displayable), but it looks very ugly:(
    And I've decided not to use brutal force and leave my application in peace with close button:)
    Thank you for your advices.

Page 1 of 2 12 LastLast

Similar Threads

  1. Can't edit JTextField after modal dialog
    By JavaNerd in forum Java Applets
    Replies: 0
    Last Post: 02-07-2008, 09:11 PM
  2. JFrame problem
    By saytri in forum New To Java
    Replies: 6
    Last Post: 01-11-2008, 05:12 PM
  3. JFrame (Hello World)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-14-2007, 06:27 PM
  4. help me with JFrame and JLabel
    By michcio in forum New To Java
    Replies: 5
    Last Post: 11-20-2007, 07:44 AM
  5. Help with JFrame
    By Albert in forum AWT / Swing
    Replies: 2
    Last Post: 07-04-2007, 04:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •