Close JWindow using button

Source code below will show you how to exit a JWindow using a button.

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


import javax.swing.JWindow;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;

public class ClosingAJWindowUsingButton extends JWindow implements ActionListener
{
JButton button=new JButton("Exit");

public ClosingAJWindowUsingButton()
{
button.addActionListener(this);
setLayout(new FlowLayout());
add(button);
setSize(400,400);
setVisible(true);
}

public void actionPerformed(ActionEvent evt)
{
if(evt.getSource()==button)
{
System.exit(0);
}
}

public static void main(String[]args)
{
ClosingAJWindowUsingButton caj=new ClosingAJWindowUsingButton();
}
}


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

Set JWindow background color

Source code below will show you how to change JWindow background color. If we directly use setBackground method for JWindow, it seem it's color don't change. So we will use JPanel. In this case, we will set color that we want for JWindow background to JPanel background color. After that we will add the created JPanel into JWindow. So after this, if you want to create a JWindow that contain other components like JTextField or JButton with background color, you must add into a JPanel with background color that you want first. After that, add the JPanel into JWindow.

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


import java.awt.Color;

import javax.swing.JWindow;

import javax.swing.JPanel;

public class SetJWindowBackgroundColor
{
public static void main(String[]args)
{
//Color is base on RGB
//R=0 , G=219 , B=252
Color color=new Color(0,219,252);

JPanel myPanel=new JPanel();
myPanel.setBackground(color);

JWindow myWindow=new JWindow();
myWindow.add(myPanel);
myWindow.setSize(400,400);
myWindow.setVisible(true);
}
}


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

Put component into JWindow

Source code below will show you how to add a component into JWindow. In this case we will use button as component that we want to add into JWindow.

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


import javax.swing.JWindow;
import javax.swing.JButton;

import java.awt.BorderLayout;

import java.awt.Container;

public class PutComponentIntoJWindow
{
public static void main(String[]args)
{
//Create a button.
//Button will be a component that we will add into JWindow
JButton button=new JButton("MY BUTTON");

//Create a JWindow
JWindow window=new JWindow();

//When you want to add component into JWindow you must get it Container first
//After that you can add component into JWindow using it.
Container contain=window.getContentPane();

//Set container layout to BorderLayout
contain.setLayout(new BorderLayout());

//Add button into JWindow
contain.add(button,BorderLayout.CENTER);

//Set JWindow size
window.setSize(400,400);

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


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

Note : Thanks for someone that show me some light. It is wrong when we want to add component into JWindow like example(For example : window is variable for JWindow, it wrong when we want to add component into JWindow...we do like this window.add(button)).We must get it's container first, and after that we will add component into JWindow using container that we get.You can see source code above after i repaired it.

What is a JWindow ???

A JWindow is a container that can be displayed anywhere on the user's desktop. It does not have the title bar, window-management buttons, or other trimmings associated with a JFrame, but it is still a "first-class citizen" of the user's desktop, and can exist anywhere on it.
(Description From Java API)

JWindow also suitable when we want to build a splash screen for an application. What is splash screen ???
-Splash screen is a term used to describe an image that appears while computer program is loading(FROM WIKIPEDIA).
-For example :
Splash screen for Microsoft Word 2003



Create a JWindow

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


import javax.swing.JWindow;

public class CreateJWindow
{
public static void main(String[]args)
{
JWindow myWindow=new JWindow();
myWindow.setSize(400,400);
myWindow.setVisible(true);
}
}


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

Borderless window

Source code below will show you how to create borderless window in java.

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


import javax.swing.JWindow;
import javax.swing.JButton;

import java.awt.BorderLayout;

public class BorderlessWindow
{
public static void main(String[]args)
{
JButton button1=new JButton("MY BUTTON");
JWindow window=new JWindow();

window.getContentPane().add(button1,BorderLayout.CENTER);

window.setSize(400,400);

window.setVisible(true);
}
}


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

Create window without title bar and window management button

In java, we can use JWindow to create window without title bar and window management button.

Get all display mode

Source code below will display all display mode in your computer include their width, height, refresh rate and bit depth.

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


import java.awt.GraphicsEnvironment;
import java.awt.GraphicsDevice;
import java.awt.DisplayMode;

public class AllDisplayMode
{
public static void main(String[]args)
{
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd=ge.getDefaultScreenDevice();
DisplayMode[]allDisplayMode=gd.getDisplayModes();

int temp=0;

while(temp<allDisplayMode.length)
{
System.out.println("DISPLAY MODE : "+(temp+1));
System.out.println("Width : "+allDisplayMode[temp].getWidth()+" pixels");
System.out.println("Height : "+allDisplayMode[temp].getHeight()+" pixels");
System.out.println("Refresh rate : "+allDisplayMode[temp].getRefreshRate()+" Hz");
System.out.println("BitDepth : "+allDisplayMode[temp].getBitDepth()+" bit");
System.out.println("\n");
temp=temp+1;
}
}
}


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

Determine thread hold synchronized lock in java

Source code below will print true if current thread hold synchronized lock for current object and print false for otherwise.

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


//This program will print three value whether current thread hold synchronized lock
//for cuurent object or not.
public class DetermineThreadHoldSynchronizedLock
{
boolean isLock;

public DetermineThreadHoldSynchronizedLock()
{
//Check whether current thread hold synchronized lock for current object
isLock=Thread.holdsLock(this);

//Print lock value
//It should print false
System.out.println("HOLD SYNCHRONIZED : "+isLock);

//Go to method locker()
locker();
}

public void locker()
{
//Current thread get lock from current object
//It's mean other thread can't interrupt untill this thread get out from
//synchronized block,{}...like below
synchronized (this)
{
isLock=Thread.holdsLock(this);
//Print lock value
//It should print true
System.out.println("HOLD SYNCHRONIZED : "+isLock);
}
lastCheck();
}

public void lastCheck()
{
//Check whether current thread hold synchronized lock for current object
isLock=Thread.holdsLock(this);
//Print lock value
//It should print false
System.out.println("HOLD SYNCHRONIZED : "+isLock);
}

public static void main(String[]args)
{
DetermineThreadHoldSynchronizedLock dthsl=new DetermineThreadHoldSynchronizedLock();
}
}


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

Put image as jframe background

Put image as jframe background


bg.jpg



Source code below will make a jframe with bg.jpg as it's background image.

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


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

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

public class PutImageOnJFrame extends JPanel
{
public PutImageOnJFrame()
{
setOpaque(false);
setLayout(new FlowLayout());
}

public static void main(String[]args)
{
JFrame myFrame=new JFrame("Put Image");
JButton button1=new JButton("Sample 1");
JButton button2=new JButton("Sample 2");
PutImageOnJFrame c=new PutImageOnJFrame();
c.add(button1);
c.add(button2);
myFrame.add(c);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(400,400);
myFrame.setVisible(true);
}

public void paint(Graphics g)
{ //IT DEPEND ON YOUR PICTURE AND PUT IT'S LOCATION IN
Image a=Toolkit.getDefaultToolkit().getImage("E:\\_JAVA_\\---SOURCE CODE---\\bg.jpg");
g.drawImage(a,0,0,getSize().width,getSize().height,this);
super.paint(g);
}

//THANKS FOR WATCHING
}


********************************************************
JUST COMPILE AND EXECUTE IT.
Note : Before that, you must know your image location. Copy and paste your image location into Image a=Toolkit.getDefaultToolkit().getImage(PASTE_IN_THIS);
********************************************************

RELAXING NATURE VIDEO