I'm writing a simple painting program using Java, and I want some method to be called whenever the JFrame component is resized. However I can't find any method like windowResizedListener or an event like windowResizedEvent. What can I do?!
The resize event fires when the document view (window) has been resized. This event is not cancelable and does not bubble. In some earlier browsers it was possible to register resize event handlers on any HTML element.
Answer: Use the addEventListener() Method You can simply use the addEventListener() method to register an event handler to listen for the browser window resize event, such as window. addEventListener('resize', ...) . The following example will display the current width and height of the browser window on resize.
The onresize event occurs when the browser window has been resized.
Implement a ComponentAdapter
with componentResized()
:
frame.addComponentListener(new ComponentAdapter() { public void componentResized(ComponentEvent componentEvent) { // do stuff } });
Overriding particular methods of ComponentAdapter
is a convenient alternative to implementing all the methods of ComponentListener
. Several examples seen here illustrate the "convenience for creating listener objects" mentioned in the API.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With