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.
Related Posts Plugin for WordPress, Blogger...

I Also Can Create T-Shirt Logo For You