Close JFrame using mouse hover

Source code below will show you, how to create JFrame that can close, when you hover your cursor on it.

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


import javax.swing.JFrame;

import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class CloseJFrameUsingMouseHover
{
public static void main(String[]args)
{
//Create mouse listener that will listen when someone hover on JFrame
MouseListener ml=new MouseAdapter()
{
public void mouseEntered(MouseEvent event)
{
//Put JFrame close code here
System.exit(0);
}
};

//Create JFrame with title ( HOVER ON THE BOX BELOW !!!!!!!!! )
JFrame frame=new JFrame("HOVER ON THE BOX BELOW !!!!!!!!!");

//Add mouse listener to JFrame
frame.addMouseListener(ml);

//Set default close operation to JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size to :
//Width : 400 pixels
//Height : 400 pixels
frame.setSize(400,400);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


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

Close JFrame using mouse click

Source code below will show you, how to close JFrame when click on it.

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


import javax.swing.JFrame;

import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class CloseJFrameUsingMouseClick
{
public static void main(String[]args)
{
//Create mouse listener that will listen when someone click on JFrame
MouseListener ml=new MouseAdapter()
{
public void mouseClicked(MouseEvent event)
{
//Put JFrame close code here
System.exit(0);
}
};

//Create JFrame with title ( CLICK IN THE BOX BELOW !!!!!!!!! )
JFrame frame=new JFrame("CLICK IN THE BOX BELOW !!!!!!!!!");

//Add mouse listener to JFrame
frame.addMouseListener(ml);

//Set default close operation to JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size to :
//Width : 400 pixels
//Height : 400 pixels
frame.setSize(400,400);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


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

Close JFrame using keyboard

Source code below will show you how to close JFrame using keyboard press. In this source code we will use "Esc" key. So when someone press "Esc" key, and at the same time JFrame focused, the JFrame will close and this program will exit.

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


import javax.swing.JFrame;

import java.awt.event.KeyListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class CloseJFrameUsingKeyboard
{
public static void main(String[]args)
{
//Create a KeyListener that can listen when someone press Esc key on keyboard
//You can change for what key that you want, by change value at:
//VK_ESCAPE
KeyListener kl=new KeyAdapter()
{
public void keyPressed(KeyEvent evt)
{
//If someone click Esc key, this program will exit
if(evt.getKeyCode()==KeyEvent.VK_ESCAPE)
{
System.exit(0);
}
}
};

//Create a JFrame with title ( CLOSE JFRAME USING KEYBOARD )
JFrame frame=new JFrame("CLOSE JFRAME USING KEYBOARD");

//add Key Listener to JFrame
frame.addKeyListener(kl);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size to :
//Width : 400 pixels
//Height : 400 pixels
frame.setSize(400,400);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


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

Get all JFrame in program

Source code below will show you how to get all JFrame that contains in a program

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


import javax.swing.JFrame;

import java.awt.Frame;

public class GetAllJFrameInAnApplication
{
public static void main(String[]args)
{
//Create JFrame with title ( HAHAHAHA )
JFrame frame1=new JFrame("HAHAHAHA");

//Create JFrame with title ( HEHEHEHE )
JFrame frame2=new JFrame("HEHEHEHE");

//Create JFrame with title ( HUHUHUHU )
JFrame frame3=new JFrame("HUHUHUHU");

//Create JFrame with title ( HIHIHIHI )
JFrame frame4=new JFrame("HIHIHIHI");

//Create JFrame with title ( ZZZZZZZZ )
JFrame frame5=new JFrame("ZZZZZZZZ");

//Create array from type Frame.
//Store all JFrame into this array.
Frame[]allJFrame=Frame.getFrames();

//Print each JFrame title in this application
for(int i=0; i<allJFrame.length; i++)
{
System.out.println("JFrame "+(i+1)+" :"+allJFrame[i].getTitle());
}
}
}


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

Remove JFrame title bar

Source code below will show you, how to remove title bar from a JFrame.

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


import javax.swing.JFrame;

public class RemoveJFrameTitleBar
{
public static void main(String[]args)
{
//Create a JFrame
JFrame frame=new JFrame();

//Method setUndecorated(true) will eliminate JFrame title bar
frame.setUndecorated(true);

//Set JFrame size to :
//Width : 400 pixels
//Height : 400 pixels
frame.setSize(400,400);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


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

Set JFrame layout using Grid Layout

Source code below will show you, how to set JFrame layout using GridLayout. GridLayout that we use in this source code contains 3 rows and 2 columns. When you use GridLayout to set layout, you just need to play with number of row and number of column. After that GridLayout will add your component from left to right. If it reached at last column in a row, it will go to the next row, and it will start again add component from left to right.

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


import javax.swing.JFrame;
import javax.swing.JButton;

import java.awt.GridLayout;

public class GridLayoutForJFrame
{
public static void main(String[]args)
{
//Create a JFrame with title ( Set JFrame layout using Grid Layout )
JFrame frame=new JFrame("Set JFrame layout using Grid Layout");

//Create grid layout that contains :
//3 rows
//2 columns
GridLayout layout=new GridLayout(3,2);

//Set JFrame layout to grid layout with 3 rows and 2 columns
frame.setLayout(layout);

//Create 6 buttons that we want to add into JFrame
JButton button1=new JButton("BUTTON1");
JButton button2=new JButton("BUTTON2");
JButton button3=new JButton("BUTTON3");
JButton button4=new JButton("BUTTON4");
JButton button5=new JButton("BUTTON5");
JButton button6=new JButton("BUTTON6");


//Add all buttons into JFrame

//Add first button at row number 1 and column number 1
frame.add(button1);

//Add second button at row number 1 and column number 2
frame.add(button2);

//Add third button at row number 2 and column number 1
frame.add(button3);

//Add fourth button at row number 2 and column number 2
frame.add(button4);

//Add fifth button at row number 3 and column number 1
frame.add(button5);

//Add sixth button at row number 3 and column number 2
frame.add(button6);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size :
//Width : 400 pixels
//Height : 400 pixels
frame.setSize(400,400);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


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

Set JFrame layout using Border Layout

Source code below will show you how to add component into JFrame that use Border Layout as it's Layout Manager. If you use Border Layout, you must know 5 region that will be use by Border Layout to arrange component in a JFrame:

NORTH

WEST

CENTER

EAST

SOUTH



You cannot change this setting. If you want to add a component, you also must set location of your component. For example, if you want your component locate at SOUTH, you must use BorderLayout.SOUTH. You can see it in complete source code below. Don't forget to set your location.

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


import javax.swing.JFrame;
import javax.swing.JButton;

import java.awt.BorderLayout;

public class BorderLayoutForJFrame
{
public static void main(String[]args)
{
//Create a JFrame with title ( Border Layout for JFrame layout manager )
JFrame frame=new JFrame("Border Layout for JFrame layout manager");

//Set JFrame layout to Border Layout
frame.setLayout(new BorderLayout());

//Create 5 buttons that we want to add into JFrame
JButton button1=new JButton("NORTH");
JButton button2=new JButton("WEST");
JButton button3=new JButton("CENTER");
JButton button4=new JButton("EAST");
JButton button5=new JButton("SOUTH");

//Add all buttons into JFrame
frame.add(button1,BorderLayout.NORTH);
frame.add(button2,BorderLayout.WEST);
frame.add(button3,BorderLayout.CENTER);
frame.add(button4,BorderLayout.EAST);
frame.add(button5,BorderLayout.SOUTH);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(600,400);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


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

Set JFrame layout using Flow Layout

Source code below will show you how to set JFrame layout using Flow Layout. For your information, when you use Flow Layout to set your JFrame layout, component will be add from left to right. If there are no space, next component will be add at next line from left to right.

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


import javax.swing.JFrame;
import javax.swing.JButton;

import java.awt.FlowLayout;

public class FlowLayoutForJFrame
{
public static void main(String[]args)
{
//Create a JFrame with title ( Set JFrame layout manager using Flow Layout )
JFrame frame=new JFrame("Set JFrame layout manager using Flow Layout");

//Set JFrame layout to Flow Layout
frame.setLayout(new FlowLayout());

//Create 6 buttons that want to add into JFrame
JButton button1=new JButton("BUTTON 1");
JButton button2=new JButton("BUTTON 2");
JButton button3=new JButton("BUTTON 3");
JButton button4=new JButton("BUTTON 4");
JButton button5=new JButton("BUTTON 5");
JButton button6=new JButton("BUTTON 6");

//Add all button into JFrame
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size with
//Width = 600 pixels
//Height = 400 pixels
frame.setSize(600,400);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


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

Add JSlider into JFrame

Source code below will show you, how to create horizontal slider in java using JSlider class, and after that add the slider into a JFrame.

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


import javax.swing.JSlider;
import javax.swing.JFrame;

import java.awt.FlowLayout;

public class AddJSliderIntoJFrame
{
public static void main(String[]args)
{
//Create a slider from JSlider class
JSlider slider=new JSlider();

//spacing between major tick
slider.setMajorTickSpacing(10);

//spacing between minor tick
slider.setMinorTickSpacing(1);

//make slider integer value visible
slider.setPaintLabels(true);

//make slider tick visible
slider.setPaintTicks(true);

//Create a JFrame with title ( Put slider into JFrame )
JFrame frame=new JFrame("Put slider into JFrame");

//set layout manager for JFrame
frame.setLayout(new FlowLayout());

//Add JSlider into JFrame
frame.add(slider);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(400,100);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


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

Create splash screen in java using JWindow

Source code below will show you, how to create splash screen in java using JWindow




splashscreenimage.jpg


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


import javax.swing.JWindow;
import javax.swing.JFrame;
import javax.swing.ImageIcon;

import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.Image;
import java.awt.Dimension;

public class CreateSplashScreenUsingJWindow extends JWindow
{
Image imageForSplashScreen;
ImageIcon ii;

public CreateSplashScreenUsingJWindow()
{
//Get image for splash screen image.
//Put full address for your image location
//In my case, my image location is : C:\Documents and Settings\evergreen\Desktop\splashscreenimage.jpg
imageForSplashScreen=Toolkit.getDefaultToolkit().getImage("C:\\Documents and Settings\\evergreen\\Desktop\\splashscreenimage.jpg");

//Create ImageIcon from Image
ii=new ImageIcon(imageForSplashScreen);

//Set JWindow size from image size
setSize(ii.getIconWidth(),ii.getIconHeight());

//Get current screen size
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();

//Get x coordinate on screen for make JWindow locate at center
int x=(screenSize.width-getSize().width)/2;

//Get y coordinate on screen for make JWindow locate at center
int y=(screenSize.height-getSize().height)/2;

//Set new location for JWindow
setLocation(x,y);

//Make JWindow visible.
setVisible(true);
}

//Paint image onto JWindow
public void paint(Graphics g)
{
super.paint(g);
g.drawImage(imageForSplashScreen,0,0,this);
}

public static void main(String[]args)
{
CreateSplashScreenUsingJWindow cssujw=new CreateSplashScreenUsingJWindow();

try
{
//Make JWindow appear for 5 seconds before disappear
Thread.sleep(5000);
cssujw.dispose();
}
catch(Exception exception)
{
exception.printStackTrace();
}

JFrame frame=new JFrame("Main Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
}
}


*****************************************************************************
JUST COMPILE AND EXECUTE IT
Note : Download splashscreenimage.jpg into your computer.
Change location for your splashscreenimage.jpg in the source
code. If you don't do it, the image will not appear.
*****************************************************************************

CLICK HERE FOR >> Java transparent splash screen

RELAXING NATURE VIDEO