Pages

Add scrollbar to JFrame

Source code below will show you, how to add scrollbar to a JFrame.

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


import javax.swing.JScrollPane;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class AddScrollBarToJFrame
{
public static void main(String[]args)
{
//Create a JPanel
JPanel panel=new JPanel();

//Create a scrollbar using JScrollPane and add panel into it's viewport
//Set vertical and horizontal scrollbar always show
JScrollPane scrollBar=new JScrollPane(panel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

//Create a JFrame with title ( AddScrollBarToJFrame )
JFrame frame=new JFrame("AddScrollBarToJFrame");

//Add JScrollPane into JFrame
frame.add(scrollBar);

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

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

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

//So, if you want to add other component like JTextArea, just add them into JPanel.After that add
//the JPanel into JScrollPane before add the JScrollPane into JFrame.
}
}


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

Add menu bar into JFrame

Source code below will show you, how to create menu bar for a JFrame using JMenuBar. It also show you, how to use JMenu that will be add into JMenuBar and how to use JMenuItem that will be list of menu item in a menu. JMenuItem will be add into JMenu. We also can add other JMenu into a JMenu.

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


import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import javax.swing.JFrame;

import javax.swing.SwingConstants;

public class AddMenuBarIntoJFrame
{
public static void main(String[]args)
{
//Create a menu bar
JMenuBar frameMenuBar=new JMenuBar();

//Create first menu in menubar
JMenu menu1=new JMenu("Menu1");

//Create second menu in menubar
JMenu menu2=new JMenu("Menu2");

//Create third menu in menubar
JMenu menu3=new JMenu("Menu3");

//Create menu that will be add into third menu
JMenu menu3_internalMenu1=new JMenu("Menu3_Menu1");

//Create first menu item for first menu
JMenuItem menuItem1=new JMenuItem("Menu1_MenuItem1");

//Create first menu item for second menu
JMenuItem menuItem2=new JMenuItem("Menu2_MenuItem1");

//Create second menu item for second menu
JMenuItem menuItem3=new JMenuItem("Menu2_MenuItem2");

//Create first menu item for third menu
JMenuItem menuItem4=new JMenuItem("Menu3_MenuItem1");

//Create menu item that will be add into internal menu in third menu
JMenuItem menuItem5=new JMenuItem("Menu3_Menu1_MenuItem1");

//add first menu item into first menu
menu1.add(menuItem1);

//add first menu item into second menu
menu2.add(menuItem2);

//add second menu item into second menu
menu2.add(menuItem3);

//add first menu item into third menu
menu3.add(menuItem4);

//add internal menu into third menu
menu3.add(menu3_internalMenu1);

//add menu item into internal menu
menu3_internalMenu1.add(menuItem5);

//add first menu into menu bar
frameMenuBar.add(menu1);

//add second menu into menu bar
frameMenuBar.add(menu2);

//add third menu into menu bar
frameMenuBar.add(menu3);

//Create a JFrame with title ( JFrame with menu bar )
JFrame frame=new JFrame("JFrame with menu bar");

//Set menu bar for JFrame
frame.setJMenuBar(frameMenuBar);

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

Add JTextArea into JFrame

Source code below will show you how to create text area in java, and add it into JFrame. There are two example of text area in source code below. First, without initial text and second with initial text.

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


import javax.swing.JFrame;
import javax.swing.JTextArea;

import java.awt.GridLayout;

public class AddJTextAreaIntoJFrame
{
public static void main(String[]args)
{
//Create JTextArea without initial text
JTextArea textArea1=new JTextArea();

//Create JTextArea with initial text ( Text area with initial text )
JTextArea textArea2=new JTextArea("Text area with initial text");

//Create a JFrame with title ( Add JTextArea into JFrame )
JFrame frame=new JFrame("Add JTextArea into JFrame");

//Set JFrame layout
frame.setLayout(new GridLayout(2,1,1,5));

//Add first JTextArea into JFrame
frame.add(textArea1);

//Add second JTextArea into JFrame
frame.add(textArea2);

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

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

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


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

Add JTextField into JFrame

Source code below will show you how to add text field in JFrame. There are two example of text field. First is without initial text. Second is text field with initial text.

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


import javax.swing.JTextField;
import javax.swing.JFrame;

import java.awt.GridLayout;

public class AddJTextFieldIntoJFrame
{
public static void main(String[]args)
{
//Create JTextField with no initial text
JTextField textField1=new JTextField();

//Create JTextField with initial text ( Put your text here )
JTextField textField2=new JTextField("Put your text here");

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

//Set layout for JFrame
frame.setLayout(new GridLayout(2,1,1,3));

//Add first JTextField into JFrame
frame.add(textField1);

//Add second JTextField into JFrame
frame.add(textField2);

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

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

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


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

Add JList into JFrame

Source code below will show you, how to create a list in java using JList. After that we will add it, into a jframe.

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


import javax.swing.JList;
import javax.swing.JFrame;

import java.awt.FlowLayout;

public class AddJListIntoJFrame
{
public static void main(String[]args)
{
//Create contents for JList
String[]listContents={"Honda","Toyota","Ford","Ferrari","Cooper"};

//Create a JList with it contents
JList myList=new JList(listContents);

//Create a JFrame with title ( Add JList into JFrame )
JFrame frame=new JFrame("Add JList into JFrame");

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

//Add JList into JFrame
frame.add(myList);

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

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

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


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

Add JComboBox into JFrame

Source code below will show you how create contents for JComboBox. After that we will add JComboBox into a JFrame.

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


import javax.swing.JComboBox;
import javax.swing.JFrame;

import java.awt.FlowLayout;

public class AddJComboBoxIntoJFrame
{
public static void main(String[]args)
{
//Create combo box contents from type String
String[]comboBoxContents={"Chicken","Duck","Cow","Sheep"};

//Create combo box using JComboBox
JComboBox myComboBox=new JComboBox(comboBoxContents);

//Create a JFrame with title ( Add JComboBox into JFrame )
JFrame frame=new JFrame("Add JComboBox into JFrame");

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

//Add JComboBox into JFrame
frame.add(myComboBox);

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

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

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


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

Borderless JFrame

Source code below will show you simple way to create borderless JFrame in java.

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


import javax.swing.JFrame;

public class CreateBorderlessJFrame
{
public static void main(String[]args)
{
JFrame myBorderlessJFrame=new JFrame();

//setUndecorated(true) for JFrame will remove it's title bar.
//So it make JFrame seem like borderless.
myBorderlessJFrame.setUndecorated(true);

//Set JFrame size to 400 pixels width and 400 pixels height
myBorderlessJFrame.setSize(400,400);

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


*************************************************************************
JUST COMPILE AND EXECUTE IT
*************************************************************************
Related Posts Plugin for WordPress, Blogger...

I Also Can Create T-Shirt Logo For You