Pages

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