Results 1 to 20 of 21
Thread: How to make a modal JFrame?
- 04-10-2008, 08:34 AM #1
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.
- 04-10-2008, 08:48 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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?
- 04-10-2008, 09:35 AM #3
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
}
- 04-10-2008, 10:05 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I'm not clear what you say. I can interact with any frame at any time on your code.
- 04-10-2008, 10:27 AM #5
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...
- 04-10-2008, 11:00 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 04-10-2008, 11:07 AM #7
ok..I will try what you've said..Once again..thank you...
- 04-10-2008, 11:34 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
:) 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 ;)
- 04-10-2008, 04:13 PM #9
Member
- Join Date
- Jan 2008
- Posts
- 21
- Rep Power
- 0
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
- 04-11-2008, 03:32 AM #10
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?
- 04-11-2008, 06:40 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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. :)
- 01-22-2009, 02:53 PM #12
Member
- Join Date
- Jan 2009
- Posts
- 1
- Rep Power
- 0
thank. the code is very good
- 01-22-2009, 02:59 PM #13
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
- 01-22-2009, 03:31 PM #14
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.
- 01-25-2009, 06:18 PM #15
Member
- Join Date
- Jan 2009
- Posts
- 3
- Rep Power
- 0
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.
- 01-25-2009, 08:04 PM #16
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 01-25-2009, 11:15 PM #17
> it's very difficult to "switch off" the close button.
Difficult? setDefaultCloseOperation(DO_NOTHING_ON_CLOSE)
db
- 01-25-2009, 11:38 PM #18
Member
- Join Date
- Jan 2009
- Posts
- 3
- Rep Power
- 0
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.
- 01-26-2009, 12:47 AM #19
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...
- 01-26-2009, 09:57 AM #20
Member
- Join Date
- Jan 2009
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Can't edit JTextField after modal dialog
By JavaNerd in forum Java AppletsReplies: 0Last Post: 02-07-2008, 09:11 PM -
JFrame problem
By saytri in forum New To JavaReplies: 6Last Post: 01-11-2008, 05:12 PM -
JFrame (Hello World)
By Java Tip in forum Java TipReplies: 0Last Post: 12-14-2007, 06:27 PM -
help me with JFrame and JLabel
By michcio in forum New To JavaReplies: 5Last Post: 11-20-2007, 07:44 AM -
Help with JFrame
By Albert in forum AWT / SwingReplies: 2Last Post: 07-04-2007, 04:44 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks