Showing posts with label text. Show all posts
Showing posts with label text. Show all posts

Java write text file


Program below will ask user to enter their name and some description about them. After that it will write into a text file named MyTextFile.txt with .txt file's extension. You can try with other file format like .doc(Microsoft Word) or .rtf or others that you know to store text file. It's not mean if we write to text file, we must use a file with .txt as it's extension.

Description that i get from Wikipedia about text file is :

A text file (sometimes spelled "textfile": an old alternate name is "flatfile") is a kind of computer file that is structured as a sequence of lines.

So, it's mean a text file can hold any file's extension. Not only for .txt.

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


import java.io.File;
import java.io.PrintWriter;

import java.util.Scanner;

import java.awt.Desktop;

public class WriteTextFile
{
public static void main(String[]args)
{
Scanner scanUserInput=new Scanner(System.in);

File targetedFile=new File("MyTextFile.txt");

System.out.println("What is your name ?");
String name=scanUserInput.nextLine();

System.out.println("Put some description about yourself");
System.out.println("-----------------------------------");
String description=scanUserInput.nextLine();

try
{
PrintWriter pw=new PrintWriter(targetedFile);

pw.println("******************");
pw.println(name + "'s"+" profile");
pw.println("******************");
pw.println("Name : "+name);
pw.println();
pw.println("Description : "+description);

pw.flush();
pw.close();


Desktop.getDesktop().open(targetedFile);
}
catch(Exception exception)
{
System.out.println("Problem in file processing");
}
}
}


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

Set ToolTipText text font

Complete source code below, will show you how to set ToolTipText text font.

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


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

import java.awt.FlowLayout;
import java.awt.Font;

public class SetToolTipTextFont
{
/**
*Create a button with text ( Put your cursor on me )
*/
JButton button=new JButton("Put your cursor on me");

//Create a window using JFrame with text ( Set ToolTipText text font )
JFrame frame=new JFrame("Set ToolTipText text font");

public SetToolTipTextFont()
{
//Create a font :
//Name : Verdana
//Style : Bold
//Size : 20
Font textFont=new Font("Verdana",Font.BOLD,20);

//Create UIManager
UIManager uim=new UIManager();

//Set tooltiptext text font using created Font
uim.put("ToolTip.font",textFont);

//Set tooltiptext for created button
button.setToolTipText("I am a button");

frame.setLayout(new FlowLayout());
frame.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[]args)
{
SetToolTipTextFont stttf=new SetToolTipTextFont();
}
}


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

Set ToolTipText text color

Complete source code below, will show you how to set ToolTipText text color.

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


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

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

public class SetToolTipTextColor
{
/**
*Create a button with text ( Put your cursor on me )
*/
JButton button=new JButton("Put your cursor on me");

//Create a window using JFrame with title ( Set ToolTipText text color )
JFrame frame=new JFrame("Set ToolTipText text color");

public SetToolTipTextColor()
{
//Create a color base on RGB value
//You can get your color value base on RGB using Color picker at above
//R=255 G=255 B=255 is RGB value for white
Color textColor=new Color(255,255,255);

//Create UIManager
UIManager uim=new UIManager();

//Set tooltiptext text color using created Color
uim.put("ToolTip.foreground",textColor);

//Set tooltiptext for created button
//So, it's tooltiptext text color will be WHITE
button.setToolTipText("I am a button");

frame.setLayout(new FlowLayout());
frame.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[]args)
{
SetToolTipTextColor stttc=new SetToolTipTextColor();
}
}


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

Set JButton text color using UIManager

Complete source code below will show you, how to set JButton text color using UIManager. When you set JButton text color using this style, it will make all JButton in your program will have same text color. So, if you want to set only one text color for JButton, i suggest you click here.

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


import javax.swing.UIManager;

import javax.swing.plaf.ColorUIResource;

import javax.swing.*;

import java.awt.*;

public class SetJButtonForegroundColorUseUiManager
{
public static void main(String[]args)
{
/*
*Create color base on RGB color model
*that will use as JButton foreground color.
*Red=255
*Green=0
*Blue=0
*/
Color foregroundColor=new Color(255,0,0);

//Set JButton foreground color
UIManager.put("Button.foreground",new ColorUIResource(foregroundColor));

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

//Create two button
JButton button1=new JButton("Button 1");
JButton button2=new JButton("Button 2");

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

//Add created button into frame
frame.add(button1);
frame.add(button2);

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

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

//Make JFrame locate at center
frame.setLocationRelativeTo(null);

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


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

Read text user input in java

Complete source code below, will show you how to wait and read text user input in command prompt.

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


import java.util.Scanner;

public class ReadTextUserInput
{
public static void main(String[]args)
{
//Create a scanner object
//Scanner is a predefined class in java that will be use to scan text
//System.in is mean, we will receive input from standard input stream
Scanner readUserInput=new Scanner(System.in);

//Print into command prompt(What is your name ?)
System.out.println("What is your name ?");

//Wait and READ TEXT INPUT from user after user hit 'Enter'
String myName=readUserInput.nextLine();

//Print what store in myName
System.out.println(myName);
}
}


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

Open a text file into JTextArea

Complete source code below will show you, how to open a text file with file extension (.txt) into JTextArea.

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


import javax.swing.*;

import java.util.*;

import java.io.*;

public class OpenTextFileIntoJTextArea
{
public static void main(String[]args)
{
try
{
//Read a text file named MyTextFile.txt
//You can download this text file at the link below
//You also can change to other text file by change it's name
//Don't forget to put .txt
//If your text file locate at other location,
//you must put it full location.
//For example :
//C:\\Documents and Settings\\evergreen\\MyTextFile.txt
FileReader readTextFile=new FileReader("MyTextFile.txt");

//Create a scanner object from FileReader
Scanner fileReaderScan=new Scanner(readTextFile);

//Create a String that will store all text in the text file
String storeAllString="";

//Put all text from text file into created String
while(fileReaderScan.hasNextLine())
{
String temp=fileReaderScan.nextLine()+"\n";

storeAllString=storeAllString+temp;
}

//Set JTextArea text to created String
JTextArea textArea=new JTextArea(storeAllString);

//Set JTextArea to line wrap
textArea.setLineWrap(true);

//Set JTextArea text to word wrap
textArea.setWrapStyleWord(true);

//Create scrollbar for text area
JScrollPane scrollBarForTextArea=new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

//Create a window using JFrame with title ( Open text file into JTextArea )
JFrame frame=new JFrame("Open text file into JTextArea");

//Add scrollbar into JFrame
frame.add(scrollBarForTextArea);

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

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

//Make JFrame locate at center on screen
frame.setLocationRelativeTo(null);

//Make JFrame visible
frame.setVisible(true);
}
catch(Exception exception)
{
//Print Error in file processing if it can't process your text file
System.out.println("Error in file processing");
}
}
}


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

CLICK HERE TO DOWNLOAD MyTextFile.txt

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

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

Set JRadioButton text bold and italic

Source code below will show you, how to set JRadioButton text to bold and italic.

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


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

import java.awt.BorderLayout;
import java.awt.Font;

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

//Create font.
//Font Name : Default radio button font
//Font Style : Bold And Italic
//Font Size : Default radio button font size
Font newRadioButtonFont=new Font(radioButton.getFont().getName(),Font.ITALIC+Font.BOLD,radioButton.getFont().getSize());

//Set JRadioButton font using new created font
radioButton.setFont(newRadioButtonFont);

//Create a window using JFrame with title ( Set JRadioButton text bold and italic )
JFrame frame=new JFrame("Set JRadioButton text bold and italic");

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

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

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


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

Set JRadioButton text italic

Source code below will show you, how to set JRadioButton text to italic.

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


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

import java.awt.BorderLayout;
import java.awt.Font;

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

//Create font.
//Font Name : Default radio button font
//Font Style : Italic
//Font Size : Default radio button font size
Font newRadioButtonFont=new Font(radioButton.getFont().getName(),Font.ITALIC,radioButton.getFont().getSize());

//Set JRadioButton font using new created font
radioButton.setFont(newRadioButtonFont);

//Create a window using JFrame with title ( Set JRadioButton text italic )
JFrame frame=new JFrame("Set JRadioButton text italic");

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

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

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


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

Set JRadioButton text bold

Source code below will show you, how to set JRadioButton text to bold.

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


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

import java.awt.BorderLayout;
import java.awt.Font;

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

//Create font.
//Font Name : Default radio button font
//Font Style : Bold
//Font Size : Default radio button font size
Font newRadioButtonFont=new Font(radioButton.getFont().getName(),Font.BOLD,radioButton.getFont().getSize());

//Set JRadioButton font using new created font
radioButton.setFont(newRadioButtonFont);

//Create a window using JFrame with title ( Set JRadioButton text bold )
JFrame frame=new JFrame("Set JRadioButton text bold");

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

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

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


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

Set JRadioButton text size

Source code below will show you, how to set JRadioButton text size.

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


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

import java.awt.BorderLayout;
import java.awt.Font;

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

//Create font.
//Font Name : Default radio button font
//Font Style : Default radio button font style
//Font Size : 22
Font newRadioButtonFont=new Font(radioButton.getFont().getName(),radioButton.getFont().getStyle(),22);

//Set JRadioButton font using new created font
radioButton.setFont(newRadioButtonFont);

//Create a window using JFrame with title ( Set JRadioButton text size )
JFrame frame=new JFrame("Set JRadioButton text size");

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

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

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


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

Set JRadioButton text font

Source code below will show you, how to set JRadioButton text font.

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


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

import java.awt.BorderLayout;
import java.awt.Font;

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

//Create font.
//Font Name : Tahoma
//Font Style : Default radio button font style
//Font Size : Default radio button font size
Font newRadioButtonFont=new Font("Tahoma",radioButton.getFont().getStyle(),radioButton.getFont().getSize());

//Set JRadioButton font using new created font
radioButton.setFont(newRadioButtonFont);

//Create a window using JFrame with title ( Set JRadioButton text font )
JFrame frame=new JFrame("Set JRadioButton text font");

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

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

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


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

Set JCheckBox text bold and italic

Source code below will show you, how to set JCheckBox text to bold and italic.

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


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

import java.awt.BorderLayout;
import java.awt.Font;

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

//Create font.
//Font Name : Default check box font
//Font Style : Bold And Italic
//Font Size : Default check box font size
Font newCheckBoxFont=new Font(checkBox.getFont().getName(),Font.ITALIC+Font.BOLD,checkBox.getFont().getSize());

//Set JCheckBox font using new created font
checkBox.setFont(newCheckBoxFont);

//Create a window using JFrame with title ( Set JCheckBox text bold and italic )
JFrame frame=new JFrame("Set JCheckBox text bold and italic");

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

//Add created check box into JFrame
frame.add(checkBox);

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

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

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


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

Set JCheckBox text italic

Source code below will show you, how to set JCheckBox text to italic.

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


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

import java.awt.BorderLayout;
import java.awt.Font;

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

//Create font.
//Font Name : Default check box font
//Font Style : Italic
//Font Size : Default check box font size
Font newCheckBoxFont=new Font(checkBox.getFont().getName(),Font.ITALIC,checkBox.getFont().getSize());

//Set JCheckBox font using new created font
checkBox.setFont(newCheckBoxFont);

//Create a window using JFrame with title ( Set JCheckBox text italic )
JFrame frame=new JFrame("Set JCheckBox text italic");

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

//Add created check box into JFrame
frame.add(checkBox);

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

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

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


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

RELAXING NATURE VIDEO