Compare String in java

Complete source code below will show you, how to compare two string with same characteristic. The characteristic that i mean here is, number of characters, sequence of characters and ordinary letters or capital letters. We will use method equal(). In the next post, i will show you how to compare two string that ignore about ordinary letters or capital letters.

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


public class CompareString
{
public static void main(String[]args)
{
//Create first String = AAaaa
//You can try other string that you want by change AAaaa
String firstString="AAaaa";

//Create second String = AAaaa
//You can try other string that you want by change AAaaa
String secondString="AAaaa";

//Check if first String is same or not to second String
if(firstString.equals(secondString))
{
//If first String equal to second String, it will print ( firstString is same to secondString )
System.out.println("firstString is same to secondString");
}
else
{
//If first String not equal to second String, it will print ( firstString is not same to secondString )
System.out.println("firstString is not same to secondString");
}
}
}


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

Set JButton select color

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

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


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

import javax.swing.plaf.ColorUIResource;

public class SetJButtonSelectColor
{
public static void main(String[]args)
{
//Create UIManager object
UIManager manager=new UIManager();


//Key to know : "Button.select"

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

//Create a button using JButton with text ( CLICK ME AND SEE WHAT COLOR )
JButton button=new JButton("CLICK ME AND SEE WHAT COLOR");

//Create a window using JFrame with title ( JButton select color )
JFrame frame=new JFrame("JButton select color");

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

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

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

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


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

Get selected JCheckBox

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

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


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

import java.awt.GridLayout;

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

public class GetSelectedJCheckBox implements ActionListener
{
//Create String that will be use to hold selected JCheckBox text
String selectedCheckBox="";

//Create check box using JCheckBox
JCheckBox checkBox1=new JCheckBox("Duck");
JCheckBox checkBox2=new JCheckBox("Chicken");
JCheckBox checkBox3=new JCheckBox("Cow");
JCheckBox checkBox4=new JCheckBox("Sheep");

//Create button using JButton with text ( Get selected JCheckBox )
JButton button=new JButton("Get selected JCheckBox");

//method actionPerformed that we override because we implement ActionListener
public void actionPerformed(ActionEvent event)
{
//Action for button
if(event.getSource()==button)
{
//Check first JCheckBox is selected or not
if(checkBox1.isSelected())
{
selectedCheckBox=selectedCheckBox+checkBox1.getText()+"\n";
}

//Check second JCheckBox is selected or not
if(checkBox2.isSelected())
{
selectedCheckBox=selectedCheckBox+checkBox2.getText()+"\n";
}

//Check third JCheckBox is selected or not
if(checkBox3.isSelected())
{
selectedCheckBox=selectedCheckBox+checkBox3.getText()+"\n";
}

//Check fourth JCheckBox is selected or not
if(checkBox4.isSelected())
{
selectedCheckBox=selectedCheckBox+checkBox4.getText()+"\n";
}

//Show message that tell you, what check box that you selected
JOptionPane.showMessageDialog(null,"Selected check box is : \n"+selectedCheckBox);

//Make selectedCheckBox string like initial with empty string
selectedCheckBox=new String("");
}
}

//Constructor
public GetSelectedJCheckBox()
{
//Create a window using JFrame with title ( Get selected JCheckBox )
JFrame frame=new JFrame("Get selected JCheckBox");

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

//Add ActionListener to button
button.addActionListener(this);

//Set JFrame layout
frame.setLayout(gl);

//Add all created check box into JFrame
frame.add(checkBox1);
frame.add(checkBox2);
frame.add(checkBox3);
frame.add(checkBox4);

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

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350,200);
frame.setVisible(true);
}

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


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

Put JCheckBox into group

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

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


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

import java.awt.GridLayout;

public class PutJCheckBoxIntoGroup
{
public static void main(String[]args)
{
//Create a window using JFrame with title ( Put JCheckBox into group )
JFrame frame=new JFrame("Put JCheckBox into group");

//Create check box using JCheckBox
JCheckBox checkBox1=new JCheckBox("Duck");
JCheckBox checkBox2=new JCheckBox("Chicken");
JCheckBox checkBox3=new JCheckBox("Cow");
JCheckBox checkBox4=new JCheckBox("Sheep");

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

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

//Add all created check box into button group
bg.add(checkBox1);
bg.add(checkBox2);
bg.add(checkBox3);
bg.add(checkBox4);

//Set JFrame layout
frame.setLayout(gl);

//Add all created check box into JFrame
frame.add(checkBox1);
frame.add(checkBox2);
frame.add(checkBox3);
frame.add(checkBox4);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350,200);
frame.setVisible(true);
}
}


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

Clear JPasswordField text

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

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


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

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

import java.awt.GridLayout;

public class ClearJPasswordFieldText implements ActionListener
{
//Create password field
JPasswordField passwordField=new JPasswordField(12);

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

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

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

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

frame.setLayout(gl);
frame.add(passwordField);
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 password in JPasswordField
passwordField.setText("");
}
}

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


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

Clear JTextArea text

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

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


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

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

import java.awt.BorderLayout;

public class ClearJTextAreaText implements ActionListener
{
//Create text area with initial text ( PUT YOUR TEXT HERE )
JTextArea textArea=new JTextArea("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
BorderLayout bl=new BorderLayout();

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

//Constructor
public ClearJTextAreaText()
{
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);

button.addActionListener(this);

frame.setLayout(bl);
frame.add(textArea,BorderLayout.CENTER);
frame.add(button,BorderLayout.SOUTH);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,500);
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 JTextArea
textArea.setText("");
}
}

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


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

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

RELAXING NATURE VIDEO