Hello, I want to know that is there any solution in java from which we are able to get to Know whether a component resized or not. If you are having any type of idea for achieving in java then please help me to achieve it. Thank you in advance.
Hello, I want to know that is there any solution in java from which we are able to get to Know whether a component resized or not. If you are having any type of idea for achieving in java then please help me to achieve it. Thank you in advance.
For getting the solution on your problem you must need to make use of the component listener in java. The Component listener is a listener interface for receiving component events. A component is an object having a graphical representation that can be displayed on the screen and that can interact with the user. The class that is interested in processing a component event either implements this interface and all the methods it contains, or extends the abstract ComponentAdapter class overriding only the methods of interest. The listener object created from that class is then registered with a component using the component's addComponentListener method. When the component's size, location, or visibility changes, the relevant method in the listener object is invoked, and the ComponentEvent is passed to it. One or more component events are fired by a Component object just after the component is hidden, made visible, moved, or resized.
Hello, I think you must need to make use of the component listener for this purpose. If you simply use the methods below then you will able to get the solution for your problem.:
- componentHidden(ComponentEvent)
- componentMoved(ComponentEvent)
- resizedCheck(ComponentEvent)
- componentShown(ComponentEvent)
You can able to get the solution for your problem, by simply using the program below:
Code:boolean resized = false; paintComponent(): { if ( resized == false ) create and paint Mandelbrot image; else super.paint(); } resized event start: resized = true; resized event end: resized = false;
Hello, you can simply add the code below in your program and check whether your component is resized or not:
Code:private Timer t1 = new Timer(1000, this); public void resizedCheck(ComponentEvent e) { p1.setShouldRecalculateImage(false); t1.start(); } public void actionPerformed(ActionEvent e) { p1.setShouldRecalculateImage(true); t1.stop(); p1.repaint(); }
Hey, if you simply use the code below then you will able to get the solution for your resizing is done or not:
Code:import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import java.awt.event.ComponentListener; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.awt.Component; public class compresizing { JFrame frame=new JFrame("Resizing"); JButton b1=new JButton("MONSTER"); public compresizing() { ComponentListener complistener=new ComponentAdapter() { public void componentResized(ComponentEvent evt) { Component comp=(Component)evt.getSource(); int currentWidth=comp.getSize().width; int currentHeight=comp.getSize().height; JOptionPane.showMessageDialog(null,"Size changed to : "+currentWidth+"\nMONSTER HEIGHT NOW : "+currentHeight,"Size",JOptionPane.INFORMATION_MESSAGE); } }; b1.addComponentListener(complistener); frame.add(b1); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400,100); frame.setVisible(true); } public static void main(String[]args) { compresizing obj=new compresizing(); } }
Bookmarks