As @MadProgrammer said (+1 to him), this is definitely not a good idea you'd rather want to
use a JDialog
and call setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
to make sure it cannot be closed.
You could also use a JWindow
(+1 to @M. M.) or call setUndecorated(true);
on your JFrame
instance.
Alternatively you may want to add your own WindowAdapater
to make the JFrame
un-minimizable etc by overriding windowIconified(..)
and calling setState(JFrame.NORMAL);
from within the method:
//necessary imports
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Test {
/**
* Default constructor for Test.class
*/
public Test() {
initComponents();
}
public static void main(String[] args) {
/**
* Create GUI and components on Event-Dispatch-Thread
*/
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Test test = new Test();
}
});
}
private final JFrame frame = new JFrame();
/**
* Initialize GUI and components (including ActionListeners etc)
*/
private void initComponents() {
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setResizable(false);
frame.addWindowListener(getWindowAdapter());
//pack frame (size JFrame to match preferred sizes of added components and set visible
frame.pack();
frame.setVisible(true);
}
private WindowAdapter getWindowAdapter() {
return new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we) {//overrode to show message
super.windowClosing(we);
JOptionPane.showMessageDialog(frame, "Cant Exit");
}
@Override
public void windowIconified(WindowEvent we) {
frame.setState(JFrame.NORMAL);
JOptionPane.showMessageDialog(frame, "Cant Minimize");
}
};
}
}