How could I create a window which is modal and has a maximize button?
So is it possible to create a modal JFrame or create a JDialog with maximize button?
On most look and feels, modal windows (such as JDialog) do not have a maximise button simply because they're not supposed to be maximised (or minimised) at all.
It's possible with some tricks to add a maximise button, but it would be completly against the way JDialog is supposed to work.
If you need a maximise button, the best solution would be using a JWindow or a JFrame instead of a JDialog. Those windows support maximisation and minimisation.
WARNING: You shouldn't do that, no matter what.
A trick to do this in JDialog:
setUndecorated(true);
getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
Solution 1: Tested on Windows
I used a JFrame for the modal window
JFrame mainWindow = new JFrame;
mainWindow.setVisible(true);
JFrame modalWindow = new JFrame();
// The next two sentences gives modalWindow modal beahaviour
mainWindow.setEnabled(false);
mainWindow.setFocusable(false);
modalWindow.setVisible(true);
Solution 2: Tested on Ubuntu
I added a WindowFocusListener
addWindowFocusListener(new java.awt.event.WindowFocusListener() {
public void windowGainedFocus(java.awt.event.WindowEvent evt) {}
public void windowLostFocus(java.awt.event.WindowEvent evt) {
formWindowLostFocus(evt);}
private void formWindowLostFocus(java.awt.event.WindowEvent evt) {
this.requestFocus();
this.toFront();}
Here is an alternate / slightly more detailed answer.
Try Are You Missing Maximize Button? (formerly here). This is a github archive of blog articles and code by Santhosh Kumar Tekturi from the now defunct JRoller site.
It is a complete utility class that makes a Frame mimic a Dialog, similar to the other answers. It involves adding a WindowListener to the Frame to keep the frame on top of its owner and keep its owner frame disabled (warning: in the windowClosed method it should probably be frame.removeWindowListener(this);, and a WindowListener to the owner to keep the frame on top of it and to remove the listener. It also uses its own EventQueue to process events. Note this is an old post, so as mentioned in the code there may be newer APIs to deal with this code better.
Here is the core function. See the link for the rest. Note: the code in the repository differs from the article; I believe the repository is more developed.
// show the given frame as modal to the specified owner.
// NOTE: this method returns only after the modal frame is closed.
public static void showAsModal(final Frame frame, final Frame owner){
frame.addWindowListener(new WindowAdapter(){
public void windowOpened(WindowEvent e){
owner.setEnabled(false);
}
public void windowClosing(WindowEvent e) {
owner.setEnabled(true);
}
public void windowClosed(WindowEvent e){
frame.removeWindowListener(this); // originally called on owner
}
});
owner.addWindowListener(new WindowAdapter(){
public void windowActivated(WindowEvent e){
if(frame.isShowing()){
frame.setExtendedState(JFrame.NORMAL);
frame.toFront();
}else
owner.removeWindowListener(this);
}
});
owner.toFront();
frame.setVisible(true);
try{
new EventPump(frame).start();
} catch(Throwable throwable){
throw new RuntimeException(throwable);
}
}
Related
I have a program with a GUI that needs to open a separate window and wait for the user to select and option, then continue. I figure I should be doing this with the wait() and notify() methods, but I'm still trying to figure out exactly how to use those. A complicating factor is that things seem to work differently when the second window is created in an actionPerformed() method, which it needs to be.
Here's how I think it should be done here, apparently it is not quite right...
This should create a window with a button, when the button is pressed, another window with a button should be created, and when that button is pressed, the program should print "End".
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class WtfExample {
public static void main(String[] args) {
JFrame jf = new JFrame();
JButton butt = new JButton("Button");
butt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
WtfExample we = new WtfExample();
we.display();
}
});
jf.getContentPane().add(butt);
jf.setSize(new Dimension(1000, 500));
jf.setVisible(true);
System.out.println("End");
}
public synchronized void display() {
JFrame jf = new JFrame();
JButton butt = new JButton("Button");
butt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
synchronized(WtfExample.this) {
WtfExample.this.notifyAll();
}
}
});
jf.getContentPane().add(butt);
jf.setSize(new Dimension(1000, 500));
jf.setVisible(true);
while(true) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
}
}
edit- I wasn't clear enough in one thing- the second window that's opened is blank, like its components were never added to it. That's the case whether it's a frame or dialog, but that only happens if the window is created from the actionPerformed method.
No, you should just be using a JDialog.
You need a modal dialog window. Here's a tutorial on dialogs. It is easier to use JOptionPane for the simple cases.
A Dialog can be modal. When a modal Dialog is visible, it blocks user input to all other windows in the program.
As the other two answers suggest you need a modal JDialog. You do not need to deal with any Thread classes. The JDialog window will deal with the giving you control back once the user input is handled. There are a few ways you can set the dialog box modal. Here are two examples.
new JDialog(Dialog owner, boolean modal)
or
new JDialog(Dialog owner, String title, boolean modal)
You could also do something like this:
JDialog dialog = new JDialog(owner);
dialog.setModal(true);
I think this is a pretty good article about modality in JAVA.
I am building an application which I need to open a JFrame in another JFrame while the first JFrame is disabled.
The problem is when I want to close the second JFrame I need to enable the first JFrame.
I have tried in several ways, but it is not working properly and I was not able to reach one goal in each of them. I need to reach both goals:
Disabling the first Frame when the second Frame is opened
Enabling it when the second Frame is closed.
Here is part of my code:
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
PaymentJFrame pjf = new PaymentJFrame();
//setEnableValue(false);
//enableFrame();
this.setEnabled(false);
pjf.setVisible(true);
if (!pjf.isActive()){
this.setEnabled(true);
}
}
This code dose not enable the First Frame at all.
I have tried to use it in another way by adding an enable when the second Frame is closed but it is not working:
//Class in first Frame
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
PaymentJFrame pjf = new PaymentJFrame();
//setEnableValue(false);
//enableFrame();
this.setEnabled(false);
pjf.setVisible(true);
}
//Class in second Frame
private void formWindowClosed(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
FinancialDocumentsJFrame.setEnableValue(true);
}
Dose any one know how can I reach these goals?
The first Frame is the main frame and the second frame is a class that I make the frame object from it in order to show and get more information from users.
I ma using netBeans IDE designer.
First of all, a Swing application should only have one JFrame, other windows can be JDialog or somethign else. As for your question, use this code as an example. It uses a listener to detect the closing event of the second window. The following code should be in the (first) JFrame (it looks like you have a button there)
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
JDialog paymentDialog = new JDialog();
MyFirstFrame.this.setEnabled(false);
paymentDialog.setVisible(true);
paymentDialog.addWindowListener(new WindowAdapter() {
#Override
public void windowClosed(WindowEvent e) {
MyFirstFrame.this.setEnabled(true);
}
});
}
You can create your own dialog by extending JDialog as you did with the frames, and use the custom dialog in this code. Also instead of setting the JFrame enabled or disabled, you could consider using a modal JDialog that blocks actions to the JFrame while the dialog is active.
And further still, there is the setAlwaysOnTop(boolean) for Swing windows.
use this.dispose()method JFrame has dispose() method for JFrame Close
I decided to use jDialog as it was recommended by many people on the net.
I used a jDialog and copied all the objects I had used in the second jFrame.
It worked the way I wanted it to do.
My only problem is that why java hasn't prepared a different way to answer this need.
I'm using an action listener in the first jFrame to open the second Frame which I have used jDialog for it.
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
ActivityJDialog ajdf = new ActivityJDialog(this,true);
ajdf.setVisible(true);
}
And I have copied everything I wanted to have into this jDialog.
public class ActivityJDialog extends java.awt.Dialog {
//Document Attributes
int documentStatus = -1;
int documentType = -1;
/**
* Creates new form AcrivityJDialog
* #param parent
* #param modal
*/
public ActivityJDialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
/*if(false) // Full screen mode
{
// Disables decorations for this frame.
//this.setUndecorated(true);
// Puts the frame to full screen.
this.setExtendedState(this.MAXIMIZED_BOTH);
}
else // Window mode
{*/
// Size of the frame.
this.setSize(1000, 700);
// Puts frame to center of the screen.
this.setLocationRelativeTo(null);
// So that frame cannot be resizable by the user.
this.setResizable(false);
//}
}
Thank you all for helping.
I need to set to open a different JFrame as the defaultCloseOperation of the JFrame that I'm currently working.
Can I do that in Netbeans ?
http://docs.oracle.com/javase/7/docs/api/java/awt/event/WindowListener.html
Add a WindowListener to the first frame and when it closes, make the seconds frame and close this first frame. Then set defaultCloseOperation to DO_NOTHING_ON_CLOSE.
Non-working code just so you get the idea:
public class FirstFrame extends JFrame implements WindowListener {
(other stuff)
#Override
public void windowClosed(WindowEvent e) {
//make second frame
dispose();
}
}
I create a JWindow in my applet to display an update process, and I set the owner to the applet parent. My problem is that the JWindow is visible in all the tabs in my browser and not just the one containing my applet. Is it possible to add a Listener to know when my applet is visible? (And then hide the JWindow when it's not)
Applets appear to be added to a system Frame when they are displayed so you can use a WindowListener. I added the following code in the init() method of the JApplet:
Window window = SwingUtilities.windowForComponent(this);
window.addWindowListener( new WindowAdapter()
{
#Override
public void windowActivated(WindowEvent e)
{
displayWindow.setVisible( true );
}
#Override
public void windowDeactivated(WindowEvent e)
{
displayWindow.setVisible( false );
}
});
I don't play much with applets but I think thats what the start() and stop() methods are for. You would make the window visible in start() and hide the window in stop().
I have a program with a GUI that needs to open a separate window and wait for the user to select and option, then continue. I figure I should be doing this with the wait() and notify() methods, but I'm still trying to figure out exactly how to use those. A complicating factor is that things seem to work differently when the second window is created in an actionPerformed() method, which it needs to be.
Here's how I think it should be done here, apparently it is not quite right...
This should create a window with a button, when the button is pressed, another window with a button should be created, and when that button is pressed, the program should print "End".
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class WtfExample {
public static void main(String[] args) {
JFrame jf = new JFrame();
JButton butt = new JButton("Button");
butt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
WtfExample we = new WtfExample();
we.display();
}
});
jf.getContentPane().add(butt);
jf.setSize(new Dimension(1000, 500));
jf.setVisible(true);
System.out.println("End");
}
public synchronized void display() {
JFrame jf = new JFrame();
JButton butt = new JButton("Button");
butt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
synchronized(WtfExample.this) {
WtfExample.this.notifyAll();
}
}
});
jf.getContentPane().add(butt);
jf.setSize(new Dimension(1000, 500));
jf.setVisible(true);
while(true) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
}
}
edit- I wasn't clear enough in one thing- the second window that's opened is blank, like its components were never added to it. That's the case whether it's a frame or dialog, but that only happens if the window is created from the actionPerformed method.
No, you should just be using a JDialog.
You need a modal dialog window. Here's a tutorial on dialogs. It is easier to use JOptionPane for the simple cases.
A Dialog can be modal. When a modal Dialog is visible, it blocks user input to all other windows in the program.
As the other two answers suggest you need a modal JDialog. You do not need to deal with any Thread classes. The JDialog window will deal with the giving you control back once the user input is handled. There are a few ways you can set the dialog box modal. Here are two examples.
new JDialog(Dialog owner, boolean modal)
or
new JDialog(Dialog owner, String title, boolean modal)
You could also do something like this:
JDialog dialog = new JDialog(owner);
dialog.setModal(true);
I think this is a pretty good article about modality in JAVA.