Create file chooser using JFileChooser

Source code below will show you, how to create a file chooser in java using JFileChooser class.

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


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

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

import java.awt.FlowLayout;

public class CreateFileChooserUsingJFileChooser implements ActionListener
{
//Create file chooser using JFileChooser
JFileChooser fileChooser=new JFileChooser();

//Create a button that use to open file chooser
JButton button=new JButton("Click here to open file chooser");

//Create a JFrame that use to place created button
//JFrame title is ( Create file chooser using JFileChooser )
JFrame frame=new JFrame("Create file chooser using JFileChooser");

public CreateFileChooserUsingJFileChooser()
{
//add ActionListener to created button
button.addActionListener(this);

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

//add button into JFrame
frame.add(button);

//set default close operation for JFrame
//So when you click window close button, this program will exit
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//set JFrame size to :
//WIDTH : 500 pixels
//HEIGHT : 500 pixels
frame.setSize(500,500);

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

//Action that will take by button
public void actionPerformed(ActionEvent event)
{
//When you click the button, a file chooser will appear
if(event.getSource()==button)
{
//Show file chooser with title ( Choose a file )
fileChooser.showDialog(frame,"Choose a file");
}
}

public static void main(String[]args)
{
CreateFileChooserUsingJFileChooser cfcujfc=new CreateFileChooserUsingJFileChooser();
}
}


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

Create file chooser in java

In java, you can create file chooser using JFileChooser class.

Create password field using JPasswordField

Source code below will show you, how to create password field in java using JPasswordField class.

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


import javax.swing.JFrame;
import javax.swing.JPasswordField;

import java.awt.FlowLayout;

public class CreatePasswordFieldUsingJPasswordField
{
public static void main(String[]args)
{
//Create password field with number of columns equal 10
//Number of columns is use to calculate preferred size
//You can change it for what value that you want but make sure it positive integer
JPasswordField passwordField=new JPasswordField(10);

//Create JFrame that will be use to place created JPasswordField
JFrame frame=new JFrame("Create password field");

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

//Add password field into JFrame
frame.add(passwordField);

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

//Set JFrame size to :
//WIDTH : 400 pixels
//HEIGHT : 100 pixels
frame.setSize(400,100);

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


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

Create password field in java

In java, you can create a password field using JPasswordField class.

Create list using JList

Source code below will show you, how to create list in java using JList.

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


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

import java.awt.FlowLayout;

public class CreateListUsingJList
{
public static void main(String[]args)
{
//Create contents for JList using array
//Using String as array type
String[]listContents={"HI","HA","HU"};

//Create a list using JList
JList list=new JList(listContents);

//Create a JFrame that will be use to put JList into it
JFrame frame=new JFrame("Create list using JList");

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

//add created JList into JFrame
frame.add(list);

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

Create list in java

In java, you can create list using JList class.

Create combo box using JComboBox

Source code below will show you, how to create a combo box in java using JComboBox class.

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


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

import java.awt.FlowLayout;

public class CreateComboBoxUsingJComboBox
{
public static void main(String[]args)
{
//Create contents for JComboBox using array
//Using String as array type
String[]comboBoxContents={"HI","HA","HU"};

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

//Create a JFrame that will be use to put JComboBox into it
JFrame frame=new JFrame("Create combo box using JComboBox");

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

//add created JComboBox into JFrame
frame.add(comboBox);

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

Create combo box in java

In java, you can create combo box using JComboBox class.

Create radio button using JRadioButton

Source code below will show you, how to create a radio button in java with text HI using JRadioButton.

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


import javax.swing.JRadioButton;
import javax.swing.JFrame;

public class CreateRadioButtonUsingJRadioButton
{
public static void main(String[]args)
{
//Create radio button with text HI
JRadioButton radioButton=new JRadioButton("HI");

//Create JFrame to place created JRadioButton
JFrame frame=new JFrame("Create radio button using JRadioButton");

//add created radio button into JFrame
frame.add(radioButton);

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

//set JFrame size
frame.setSize(450,65);

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


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

Create radio button in java

In java, you can create radio button using JRadioButton class.

RELAXING NATURE VIDEO