Pages

Set JPanel background color

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

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


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

import java.awt.Color;

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

//Create JFrame with title ( Set JPanel background color )
JFrame frame=new JFrame("Set JPanel background color");

//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 JPanel background color to color that you choose
panel.setBackground(color);

//Add JPanel into JFrame
frame.add(panel);

//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 JButton text color

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

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


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

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

public class SetJButtonTextColor
{
public static void main(String[]args)
{
//Create button using JButton
JButton button=new JButton("Click me");

//Create JFrame with title ( Set JButton text color )
JFrame frame=new JFrame("Set JButton 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 JButton text color to color that you choose
button.setForeground(color);

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

//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 JButton background color

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

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


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

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

public class SetJButtonBackgroundColor
{
public static void main(String[]args)
{
//Create button using JButton
JButton button=new JButton("Click me");

//Create JFrame with title ( Set JButton background color )
JFrame frame=new JFrame("Set JButton background 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 JButton background color to color that you choose
button.setBackground(color);

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

//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 JTextField text color

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

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


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

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

public class SetJTextFieldTextColor
{
public static void main(String[]args)
{
//Create text field using JTextField
JTextField textField=new JTextField(10);

//Create JFrame with title ( Set JTextField text color )
JFrame frame=new JFrame("Set JTextField 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 JTextField text color to color that you choose
textField.setForeground(color);

//Add JTextField into JFrame
frame.add(textField);

//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 JTextField background color

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

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


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

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

public class SetJTextFieldBackgroundColor
{
public static void main(String[]args)
{
//Create text field using JTextField
JTextField textField=new JTextField(10);

//Create JFrame with title ( Set JTextField background color )
JFrame frame=new JFrame("Set JTextField background 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 JTextField background color to color that you choose
textField.setBackground(color);

//Add JTextField into JFrame
frame.add(textField);

//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 JTextArea text color

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

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


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

import java.awt.Color;

public class SetJTextAreaTextColor
{
public static void main(String[]args)
{
//Create text area using JTextArea
JTextArea textArea=new JTextArea();

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

//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 JTextArea text color to color that you choose
textArea.setForeground(color);

//Add JTextArea into JFrame
frame.add(textArea);

//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 JTextArea background color

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

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


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

import java.awt.Color;

public class SetJTextAreaBackgroundColor
{
public static void main(String[]args)
{
//Create text area using JTextArea
JTextArea textArea=new JTextArea();

//Create JFrame with title ( Set JTextArea background color )
JFrame frame=new JFrame("Set JTextArea background color");

//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 JTextArea background color to color that you choose
textArea.setBackground(color);

//Add JTextArea into JFrame
frame.add(textArea);

//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 JLabel text color

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

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


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

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

public class SetJLabelTextColor
{
public static void main(String[]args)
{
//Create label with text HI using JLabel
JLabel label=new JLabel("HI");

//Create JFrame with title ( Set JLabel text color )
JFrame frame=new JFrame("Set JLabel 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 JLabel text color to color that you choose
label.setForeground(color);

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

//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 JLabel background color

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

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


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

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

public class SetJLabelBackgroundColor extends JLabel
{
//Create JFrame with title ( Set JLabel background color )
JFrame frame=new JFrame("Set JLabel background color");

//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);

public SetJLabelBackgroundColor()
{
//Set text for JLabel
setText("Set JLabel Background Color");

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

//Add JLabel into JFrame
frame.add(this);

//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);
}

//Fill background color to JLabel
public void paint(Graphics g)
{
g.setColor(color);
g.fillRect(0,0,getWidth(),getHeight());
super.paint(g);
}

public static void main(String[]args)
{
SetJLabelBackgroundColor sjlbc=new SetJLabelBackgroundColor();
}
}


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

Set JCheckBox text color

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

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


import javax.swing.JCheckBox;
import javax.swing.JFrame;

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

public class SetJCheckBoxTextColor
{
public static void main(String[]args)
{
//Create check box with text HI using JCheckBox
JCheckBox checkBox=new JCheckBox("HI");

//Create JFrame with title ( Set JCheckBox text color )
JFrame frame=new JFrame("Set JCheckBox 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 JCheckBox text color to color that you choose
checkBox.setForeground(color);

//Add JCheckBox into JFrame
frame.add(checkBox);

//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 JCheckBox background color

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

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


import javax.swing.JCheckBox;
import javax.swing.JFrame;

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

public class SetJCheckBoxBackgroundColor
{
public static void main(String[]args)
{
//Create check box with text HI using JCheckBox
JCheckBox checkBox=new JCheckBox("HI");

//Create JFrame with title ( Set JCheckBox background color )
JFrame frame=new JFrame("Set JCheckBox background 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 JCheckBox background color to color that you choose
checkBox.setBackground(color);

//Add JCheckBox into JFrame
frame.add(checkBox);

//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 JRadioButton text color

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

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


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

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

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

//Create JFrame with title ( Set JRadioButton text color )
JFrame frame=new JFrame("Set JRadioButton 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 JRadioButton text color to color that you choose
radioButton.setForeground(color);

//Add JRadioButton into JFrame
frame.add(radioButton);

//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 JRadioButton background color

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

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


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

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

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

//Create JFrame with title ( Set JRadioButton background color )
JFrame frame=new JFrame("Set JRadioButton background 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 JRadioButton background color to color that you choose
radioButton.setBackground(color);

//Add JRadioButton into JFrame
frame.add(radioButton);

//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 JComboBox text color

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

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


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

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

public class SetJComboBoxTextColor
{
public static void main(String[]args)
{
//Create JComboBox contents
String[]comboBoxContents={"Cow","Sheep","Horse"};

//Create comboBox using JComboBox
JComboBox comboBox=new JComboBox(comboBoxContents);

//Create JFrame with title ( Set JComboBox text color )
JFrame frame=new JFrame("Set JComboBox 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 JComboBox text color to color that you choose
comboBox.setForeground(color);

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

//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 JComboBox background color

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

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


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

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

public class SetJComboBoxBackgroundColor
{
public static void main(String[]args)
{
//Create JComboBox contents
String[]comboBoxContents={"Cow","Sheep","Horse"};

//Create comboBox using JComboBox
JComboBox comboBox=new JComboBox(comboBoxContents);

//Create JFrame with title ( Set JComboBox background color )
JFrame frame=new JFrame("Set JComboBox background 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 JComboBox background color to color that you choose
comboBox.setBackground(color);

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

//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 JList text color

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

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


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

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

public class SetJListTextColor
{
public static void main(String[]args)
{
//Create JList contents
String[]listContents={"Cow","Sheep","Horse"};

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

//Create JFrame with title ( Set JList text color )
JFrame frame=new JFrame("Set JList 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 JList text color to color that you choose
list.setForeground(color);

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

//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 JList background color

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

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


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

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

public class SetJListBackgroundColor
{
public static void main(String[]args)
{
//Create JList contents
String[]listContents={"Cow","Sheep","Horse"};

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

//Create JFrame with title ( Set JList background color )
JFrame frame=new JFrame("Set JList background 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 JList background color to color that you choose
list.setBackground(color);

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

//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 password symbol or echo character color for JPasswordField

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

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


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

import java.awt.Color;

public class SetJPasswordFieldEchoCharacterColor
{
public static void main(String[]args)
{
//Create password field with number of columns equal to 10
JPasswordField passwordField=new JPasswordField(10);

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

//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 echo character 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,65);

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


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

Set JPasswordField background color

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

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


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

import java.awt.Color;

public class SetJPasswordFieldBackgroundColor
{
public static void main(String[]args)
{
//Create password field with number of columns equal to 10
JPasswordField passwordField=new JPasswordField(10);

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

//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 background color to color that you choose
passwordField.setBackground(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,65);

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


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

Get password length from JPasswordField

Source code below will show you, how to get password length from a JPasswordField.

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


import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

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

import java.awt.GridLayout;

public class GetPasswordLength implements ActionListener
{
//Create a password field using JPasswordField with number of columns equal to 10
JPasswordField passwordField=new JPasswordField(10);

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

//Create a button with text ( GET PASSWORD LENGTH )
JButton button=new JButton("GET PASSWORD LENGTH");

public GetPasswordLength()
{
//Add action listener to button
button.addActionListener(this);

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

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

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

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

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

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

//Action for button
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==button)
{
//Show message box that contain password length
JOptionPane.showMessageDialog(frame,"PASSWORD LENGTH : "+(passwordField.getText().length()));
}
}

public static void main(String[]args)
{
GetPasswordLength gpl=new GetPasswordLength();
}
}


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

Set password symbol or echo character for JPasswordField

Source code below will show you, how to set symbol that will be use in password field that build using JPasswordField. It also called echo character.

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


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

import java.awt.FlowLayout;

public class SetPasswordSymbol
{
public static void main(String[]args)
{
//Create a password field with number of columns equal to 10
JPasswordField passwordField=new JPasswordField(10);

//Set password symbol
//In my case i use *
//You can change for what you want.But make sure it is only one character
//This is because, method setEchoChar receive only one character
passwordField.setEchoChar('*');


//Create a JFrame with title ( Set Password Symbol )
JFrame frame=new JFrame("Set Password Symbol");

//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 : 300 pixels
//HEIGHT : 65 pixels
frame.setSize(300,65);

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


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

I Also Can Create T-Shirt Logo For You