Clear JTextField text

Complete source code below will show you, how to clear text in JTextField.

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


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

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

import java.awt.GridLayout;

public class ClearJTextFieldText implements ActionListener
{
//Create text field with initial text ( PUT YOUR TEXT HERE )
JTextField textField=new JTextField("PUT YOUR TEXT HERE");

//Create button with text ( Click here to clear text )
JButton button=new JButton("Click here to clear text");

//Create layout that will be use by JFrame
GridLayout gl=new GridLayout(2,1);

//Create window using JFrame with title ( Clear JTextField text )
JFrame frame=new JFrame("Clear JTextField text");

//Constructor
public ClearJTextFieldText()
{
button.addActionListener(this);

frame.setLayout(gl);
frame.add(textField);
frame.add(button);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,130);
frame.setVisible(true);
}

//override actionPerformed method
public void actionPerformed(ActionEvent event)
{
//Action that will perform when someone click the button
if(event.getSource()==button)
{
//Clear text in JTextField
textField.setText("");
}
}

//Main method
public static void main(String[]args)
{
ClearJTextFieldText cjtft=new ClearJTextFieldText();
}
}


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

Set JLabel text center

Complete source code below will show you, how to make text in JLabel to center.

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


import javax.swing.JLabel;
import javax.swing.JFrame;

import javax.swing.SwingConstants;

public class JLabelTextToCenter
{
public static void main(String[]args)
{
//Create a label with text ( I am a JLabel )
JLabel label=new JLabel("I am a JLabel");

//Make JLabel text to center
label.setHorizontalAlignment(SwingConstants.CENTER);

//Create a window using JFrame with title ( JLabel text to center )
JFrame frame=new JFrame("JLabel text to center");

//Add created label into JFrame
frame.add(label);

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

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

//Make JFrame visible
frame.setVisible(true);
}
}


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

Put image on JLabel

Complete source code below will show you, how to put image on JLabel.



JLabelImage.jpg


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


import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.ImageIcon;

import java.awt.Toolkit;

public class JLabelImage
{
public static void main(String[]args)
{
//Create image icon from your image file
//You must change location for your image file
//My image file is locate at : C:\Documents and Settings\evergreen\Desktop
ImageIcon ii=new ImageIcon("C:\\Documents and Settings\\evergreen\\Desktop\\JLabelImage.jpg");

//Create a label with your image file
JLabel label=new JLabel(ii);

//Create a window using JFrame with title ( Put image on JLabel )
JFrame frame=new JFrame("Put image on JLabel");

//Add created label into JFrame
frame.add(label);

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

Get selected JRadioButton

Complete source code below will show you, how to get selected JRadioButton.

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


import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JOptionPane;

import java.awt.BorderLayout;

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

public class GetSelectedJRadioButton implements ActionListener
{
//Create two radio button that will be put into a group
//Set start radio button selection to female
JRadioButton firstRadioButton=new JRadioButton("Female",true);
JRadioButton secondRadioButton=new JRadioButton("Male");

//Create a button with text ( What i select )
JButton button=new JButton("What i select");

//Create a window using JFrame with title ( Get selected JRadioButton )
JFrame frame=new JFrame("Get selected JRadioButton");

public GetSelectedJRadioButton()
{
//Create a radio button group using ButtonGroup
ButtonGroup bg=new ButtonGroup();

//Add all radio button into created group
bg.add(firstRadioButton);
bg.add(secondRadioButton);

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

//Create a panel that will be put radio button into it
JPanel panel=new JPanel();

//Add all created radio button into panel
panel.add(firstRadioButton);
panel.add(secondRadioButton);

//Add action listener to created button
button.addActionListener(this);

//Add panel and button into JFrame
frame.add(panel,BorderLayout.CENTER);
frame.add(button,BorderLayout.SOUTH);

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

//Set JFrame size
frame.setSize(300,150);

//Make JFrame visible
frame.setVisible(true);
}

//Action for button
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==button)
{
if(firstRadioButton.isSelected())
{
JOptionPane.showMessageDialog(frame,"You select : "+firstRadioButton.getText());
}

else if(secondRadioButton.isSelected())
{
JOptionPane.showMessageDialog(frame,"You select : "+secondRadioButton.getText());
}
}
}

public static void main(String[]args)
{
GetSelectedJRadioButton gsjrb=new GetSelectedJRadioButton();
}
}


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

Put JRadioButton into group

Complete source code below will show you, how to create group for JRadioButton.

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


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

import java.awt.GridLayout;

public class PutJRadioButtonIntoGroup
{
public static void main(String[]args)
{
//Create five radio button that will be put into a group
JRadioButton firstRadioButton=new JRadioButton("First radio button");
JRadioButton secondRadioButton=new JRadioButton("Second radio button");
JRadioButton thirdRadioButton=new JRadioButton("Third radio button");
JRadioButton fourthRadioButton=new JRadioButton("Fourth radio button");
JRadioButton fifthRadioButton=new JRadioButton("Fifth radio button");

//Create a button group using ButtonGroup
ButtonGroup bg=new ButtonGroup();

//Add all radio button into created group
bg.add(firstRadioButton);
bg.add(secondRadioButton);
bg.add(thirdRadioButton);
bg.add(fourthRadioButton);
bg.add(fifthRadioButton);

//Create a window using JFrame with title ( Put JRadioButton into group )
JFrame frame=new JFrame("Put JRadioButton into group");

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

//Add all created button into group
frame.add(firstRadioButton);
frame.add(secondRadioButton);
frame.add(thirdRadioButton);
frame.add(fourthRadioButton);
frame.add(fifthRadioButton);

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

//Set JFrame size
frame.setSize(300,150);

//Make JFrame visible
frame.setVisible(true);
}
}


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

JRadioButton not select at start

Complete source code below will show you, how to make JRadioButton not select at start.

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


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

import java.awt.FlowLayout;

public class RadioButtonNotSelectAtStart
{
public static void main(String[]args)
{
//Create radio button with not select at start
//You can make this radio button select on start by change false to true
JRadioButton radioButton=new JRadioButton("I am a radio button",false);

//Create a window using JFrame with title ( JRadioButton not select on start )
JFrame frame=new JFrame("JRadioButton not select on start");

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

//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(200,65);

//Make JFrame visible
frame.setVisible(true);
}
}


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

JRadioButton select at start

Complete source code below will show you, how to make JRadioButton select at start.

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


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

import java.awt.FlowLayout;

public class RadioButtonSelectAtStart
{
public static void main(String[]args)
{
//Create radio button with select at start
//You can make this radio button not select on start by change true to false
JRadioButton radioButton=new JRadioButton("I am a radio button",true);


//Create a window using JFrame with title ( JRadioButton select on start )
JFrame frame=new JFrame("JRadioButton select on start");

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

//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(200,65);

//Make JFrame visible
frame.setVisible(true);
}
}


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

Set JPasswordField text color

Source code below will show you, how to set JPasswordField text color base on RGB. You can get RGB value for your color using "Color picker" at above.

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


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

import java.awt.Color;
import java.awt.FlowLayout;

public class SetJPasswordFieldTextColor
{
public static void main(String[]args)
{
//Create password field using JPasswordField
JPasswordField passwordField=new JPasswordField(10);

//Create JFrame with title ( Set JPasswordField text color )
JFrame frame=new JFrame("Set JPasswordField text color");

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

//Set color base on RGB
//You can get RGB value for your color at "Color picker" at above
//R=255
//G=0
//B=0
Color color=new Color(255,0,0);

//Set JPasswordField text color to color that you choose
passwordField.setForeground(color);

//Add JPasswordField into JFrame
frame.add(passwordField);

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

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

//Make JFrame visible
frame.setVisible(true);
}
}


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

Set JPasswordField text center

Complete source code below will show you, how to make text in JPasswordField start at center.

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


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

import javax.swing.SwingConstants;

public class SetJPasswordFieldContentsToCenter
{
public static void main(String[]args)
{
JPasswordField passwordField=new JPasswordField(10);

//Make contents in JPasswordField start at center
passwordField.setHorizontalAlignment(SwingConstants.CENTER);

JFrame frame=new JFrame("Set JPasswordField contents to center");
frame.add(passwordField);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,65);
frame.setVisible(true);
}
}


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

Set JTextField text center

Source code below will show you, how to make text in JTextField start at center.

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


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

import javax.swing.SwingConstants;

public class SetJTextFieldTextToCenter
{
public static void main(String[]args)
{
JTextField textField=new JTextField(10);

//Make text in JTextField start at center
textField.setHorizontalAlignment(SwingConstants.CENTER);

JFrame frame=new JFrame("Set JTextField text to center");
frame.add(textField);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,65);
frame.setVisible(true);
}
}


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

RELAXING NATURE VIDEO