Pages

How to know when a component resized


We use button as component in this example.

****************************************************************
COMPLETE SOURCE CODE FOR : HowToKnowWhenComponentResize.java
****************************************************************


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 HowToKnowWhenComponentResize
{
JFrame b=new JFrame("TRY RESIZE ME");

JButton d=new JButton("MONSTER");

public HowToKnowWhenComponentResize()
{
ComponentListener cl=new ComponentAdapter()
{
public void componentResized(ComponentEvent evt)
{
Component temp=(Component)evt.getSource();
int currentWidth=temp.getSize().width;
int currentHeight=temp.getSize().height;
JOptionPane.showMessageDialog(null,"SIZE OF COMPONENT IS CHANGE\nMONSTER WIDTH NOW : "+currentWidth+"\nMONSTER HEIGHT NOW : "+currentHeight,"MONSTER'S SIZE",JOptionPane.INFORMATION_MESSAGE);
}
};

d.addComponentListener(cl);
b.add(d);

b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.setSize(400,100);
b.setVisible(true);
}

public static void main(String[]args)
{
HowToKnowWhenComponentResize myObject=new HowToKnowWhenComponentResize();
}
}


****************************************************************
JUST COMPILE AND EXECUTE IT
****************************************************************
Related Posts Plugin for WordPress, Blogger...