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
****************************************************************

How to know current location of jframe after it moved to other location on screen


Result of the current location for a jframe in this program is current coordinate of the top left corner for the jframe.

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


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.Point;
import java.awt.Component;

public class HowToKnowCurrentLocationOfJframe extends ComponentAdapter
{
JFrame b=new JFrame("TRY MOVE ME");

public HowToKnowCurrentLocationOfJframe()
{
b.addComponentListener(this);

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

public void componentMoved(ComponentEvent evt)
{
Component temp=(Component)evt.getSource();
JOptionPane.showMessageDialog(null,"CURRENT LOCATION : "+" X = "+(temp.getLocationOnScreen()).getX()+" Y = "+(temp.getLocationOnScreen()).getY(),"CURRENT LOCATION",JOptionPane.INFORMATION_MESSAGE);
}

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


********************************************************************
JUST COMPILE AND EXECUTE IT
********************************************************************

How to know when a component has been made invisible or visible ???


In this example, we use button as component.

*************************************************************************
COMPLETE SOURCE CODE FOR : HowToKnowWhenComponentVisible.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.event.ActionListener;
import java.awt.event.ActionEvent;

import java.awt.FlowLayout;

public class HowToKnowWhenComponentVisible
{
JFrame a=new JFrame("CLICK HERE");
JFrame b=new JFrame("FRAME TO STORE JBUTTON NAME'S MONSTER");

JButton c=new JButton("VISIBLE");
JButton e=new JButton("INVISIBLE");
JButton d=new JButton("MONSTER");

public HowToKnowWhenComponentVisible()
{
ComponentListener cl=new ComponentAdapter()
{
public void componentHidden(ComponentEvent evt)
{
JOptionPane.showMessageDialog(null,"MONSTER IS INVISIBLE");
}

public void componentShown(ComponentEvent evt)
{
JOptionPane.showMessageDialog(null,"MONSTER IS VISIBLE");
}
};

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

ActionListener al=new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
if(evt.getSource()==e)
{
d.show(false);
b.repaint();
}

if(evt.getSource()==c)
{
d.show(true);
b.repaint();
}
}
};

c.addActionListener(al);
e.addActionListener(al);
a.setLayout(new FlowLayout());
a.add(c);
a.add(e);

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

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

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


*************************************************************************
JUST COMPILE AND EXECUTE IT
*************************************************************************

RELAXING NATURE VIDEO