Override minimize of JFrame - java

I'm making a program with a logger. The logger has its own JFrame.
I'm trying to override the reaction from clicking on the minimize-button of that frame.
I would like the frame to either setVisible(false) or do the defaultCloseOperation (as i set that to hide earlier).
How should I do this?
Thanks in advance

Use a JDialog instead of a JFrame. JDialogs don't have a minimize button.

You can add a WindowListener and add a iconified handler that will react when the window is minimized.
Maybe:
frame.addWindowListener(new WindowAdapter(){
public void windowIconified(WindowEvent e){
frame.setVisible(false);
}
});

You can use the WindowStateListener like this
f.addWindowStateListener(new WindowStateListener() {
#Override
public void windowStateChanged(WindowEvent arg0) {
if (arg0.getNewState() == Frame.ICONIFIED) {
// do stuff
}
}
});

Try this:
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowIconified(WindowEvent event)
{
//do your stuff
}
});

Related

Close Java Frame by clicking X button

Here is a piece of code that I wrote but the close button on the top of the application doesn't work please help
Code:
import java.awt.*;
import java.awt.event.*;
public class App extends Frame implements MouseMotionListener {
App() {
addMouseMotionListener(this);
setSize(200, 200);
setVisible(true);
}
public void mouseDragged(MouseEvent e) {
Graphics g = getGraphics();
g.setColor(Color.RED);
g.fillRect(e.getX(), e.getY(),10, 10);
}
public void mouseMoved(MouseEvent e) {
}
public static void main(String[] args)throws Exception {
App a = new App();
}
}
Image:
You need to add Listener and call dispose while initializing your applet.
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
You need to add above line of code inside the constructor.
Try it with that little code:
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
You have to insert it in "App()". It will close the program "System.exit(0);"
when you press the close button.
im not using AWT often but heres my Solution :)
addWindowListener(new WindowAdapter(){
#Override
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
Add a new WindowAdapter to your App Constructor and call system.exit(0) on the Window Close Event
You can use WindowContants on JFrame#setDefaultCloseOperation to achieve the desired action. This allows you to dispose the frame, entirely terminate the application and a few more with a single line in the constructor:
this.setDefaultCloseOperation(WindowContants.DISPOSE_ON_CLOSE);
will dispose the frame containing the app. This is sufficient to terminate the program you presented.
This approach doesn't allow handling any events though, but simply closes the frame.

How to do action when closing the frame?

How do I do action when I'm going to terminates the programme.
For example :
When I'm going to terminates the programme, I want to set
if( the x button is clicked or something )
deleteRow from database;
else
just end.
Is it should be done inside the ui frame ?
Use window listeners for that. windowClosing method is used to execute code when window is closing, windowClosed method can be used if you want to do something after the window has closed.
JFrame window=new JFrame("Window");
window.addWindowListener(new WindowAdapter(){
#Override
public void windowClosed(WindowEvent evt)
{
//Do something after window has closed.
}
public void windowClosing(WindowEvent evt)
{
//Do something when window is closing.
//Useful when you have to access data in window(buttons, textfields etc)
}
});
There are also other window event listeners.Documentation of WindowListener
addWindowListener(new WindowAdapter()
{
#Override
public void windowClosing(WindowEvent e)
{
.....
}
});

Java. Swing. Disable JFrame close button

Is it possible to disable JFrame close operation?
EDIT.
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(
new WindowAdapter() {
#Override
public void windowClosing(final WindowEvent e) {
}
});
And this code doesn't solve the problem.
EDIT
Is it possible to consume WindowClosing event?

Detecting resizing of JDialog

How can you detect when a JDialog has been resized? (WindowListener would have been my guess, but that doesn't seem to have a resizing event handler.)
Try using a ComponentListener:
YourDialog.addComponentListener(new ComponentAdapter()
{
public void componentResized(ComponentEvent e)
{
// do stuff
}
});

Notification if a modal dialog is shown on top of a JFrame

What listener should I register in a JFrame instance to be notified if a modal JDialog is shown on top of the frame (the frame is the owner of the dialog)? Thanks in advance.
I think JFrame.addWindowListener(...) would work and then pay attention to WindowListener.windowDeactivated(...)
ETA:
jFrame.addWindowListener(new WindowAdapter() {
#Override
public void windowDeactivated(WindowEvent e) {
if(e.getOppositeWindow() instanceof JDialog) {
JDialog dialog = (JDialog) e.getOppositeWindow();
if(dialog.isModal()) {
// do stuff
}
}
}
});

Categories