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

How to determine when a JFrame opened or closed


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


import javax.swing.JFrame;
import java.awt.Frame;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


public class DetermineWhenAJframeIsOpenOrClose
{
JFrame frame;

JFrame a=new JFrame("OPEN A JFRAME WITH TITLE'S MONSTER");
JButton b=new JButton("CLICK HERE TO OPEN");

public DetermineWhenAJframeIsOpenOrClose()
{

b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
frame=new JFrame("MONSTER");
frame.setSize(300,500);
frame.setVisible(true);

frame.addWindowListener(new WindowAdapter()
{
//windowOpened METHOD WILL BE CALLED WHEN A JFRAME IS OPENED
public void windowOpened(WindowEvent evt)
{
JOptionPane.showMessageDialog(null,"MONSTER IS OPEN");
}

//windowClosing METHOD WILL BE CALLED WHEN A JFRAME IS CLOSING
public void windowClosing(WindowEvent evt)
{
Frame temp=(Frame)evt.getSource();
temp.dispose();
JOptionPane.showMessageDialog(null,"MONSTER IS CLOSE");
}

});

}
});

a.add(b);

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

}

public static void main(String[]args)
{
DetermineWhenAJframeIsOpenOrClose mainObject=new DetermineWhenAJframeIsOpenOrClose();
}
}


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

Close application when click on JFrame close button


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


import javax.swing.JFrame;

public class ExitApplicationUsingJFrameCloseButton
{
public static void main(String[]args)
{
JFrame a=new JFrame("EXIT AN APPLICATION USING JFRAME CLOSE BUTTON");

//JFrame.EXIT_ON_CLOSE will make your application close when you hit JFrame close button
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a.setSize(200,200);
a.setVisible(true);
}
}


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

JFrame hide when click close button


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


import javax.swing.JFrame;
import javax.swing.JOptionPane;

import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class HidingJFrameWithoutDestroy
{
public static void main(String[]args)
{
JFrame a=new JFrame("HIDE A JFRAME");

a.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
try
{
JFrame temp=(JFrame)evt.getSource();
temp.setVisible(false);
String a=JOptionPane.showInputDialog(null,"CHOOSE ONE : \n 1-Make It Visible \n 2-Exit");
int b=Integer.parseInt(a);

if(b==1)
{
temp.show(true);
}

else if(b==2)
{
System.exit(0);
}

else
{
JOptionPane.showMessageDialog(null,"YOU PUT WRONG NUMBER");
}
}
catch(Exception exception)
{
JOptionPane.showMessageDialog(null,"ERROR, DON'T DO THAT\nTHIS PROGRAM WILL CLOSE");
System.exit(0);
}
}

public void windowActivated(WindowEvent event)
{
JFrame temp=(JFrame)event.getSource();
temp.setVisible(true);
}
}
);

a.setSize(200,200);
a.setVisible(true);
}
}


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

Set jframe size and it's location when click on maximize button


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


/**
*THIS TUTORIAL WILL SHOW YOU HOW TO SET THE MAXIMUM SIZE OF YOUR FRAME
*AND IT'S LOCATION WHEN YOU CLICK
*MAXIMIZE BUTTON BESIDE JFRAME CLOSE BUTTON
**/

import java.awt.Rectangle;
import javax.swing.JFrame;

public class SettingBound
{
public static void main(String[]args)
{
//first value, 400=x coordinate on your screen
//second value, 400=y coordinate on your screen
//third value, 800=JFrame width
//fourth value, 200=JFrame height
Rectangle rec=new Rectangle(400,400,800,200);

JFrame frame1=new JFrame("Setting Bound");

frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setMaximizedBounds(rec);//set maximize setting for your JFrame
frame1.setVisible(true);
}
}


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

Remove JFrame title bar


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


import javax.swing.JFrame;

public class RemovingTitleBar
{
public static void main(String[]args)
{
JFrame a=new JFrame("REMOVING TITLE BAR");
a.setUndecorated(true);
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a.setSize(200,200);
a.setVisible(true);
}
}


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

Top 10 Errors Java Programmers Make


THANK YOU TO David Reilly FOR HIS INFORMATION

10-Accessing non static member variables from static method(Such As Main Method)

9-Mistyping the name of method when overriding

8-Comparison assignment (= rather than ==)

7-Comparing two objects using == rather than using .equals(Object object) method

6-Confusion between passing by value or passing by reference

5-Writing blank exception handlers

4-Forgetting that java is zero-indexed

3-Preventing concurrent access to shared variables by threads

2-Capitalization errors

1-NULL pointers

RELAXING NATURE VIDEO