Set JButton pressed icon

Complete source code below will show you, how to set JButton pressed icon. When someone click the button, icon on that button we will change to pressed icon that we set for the button. You can download icon that we use in this tutorial at below.When you want to compile and execute it, please make sure, icon file locate at same location with this java file.

Note : If you want to create pressed icon, i suggest you make it size and shape same with non-pressed icon.

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


import javax.swing.*;

public class SetJButtonPressedIcon
{
public static void main(String[]args)
{
//Create image icon from "notpressedicon.png"
//You can download it at below
//If your icon locate at other location,
//please put it's full address location.For example :
//C:\\Documents and Settings\\evergreen\\Desktop\\notpressedicon.png
ImageIcon notPressIcon=new ImageIcon("notpressedicon.png");

//Create image icon from "pressedicon.png"
//You can download it at below
//If your icon locate at other location,
//please put it's full address location.For example :
//C:\\Documents and Settings\\evergreen\\Desktop\\pressedicon.png
ImageIcon pressIcon=new ImageIcon("pressedicon.png");

//Create a button with text ( GO ) and not press icon ( notpressedicon.png )
JButton button=new JButton("GO",notPressIcon);

//Set presses icon for JButton
button.setPressedIcon(pressIcon);

//Set horizontal text position to center
button.setHorizontalTextPosition(SwingConstants.CENTER);

//Set vertical text position to bottom.
//So, text will locate under icon
button.setVerticalTextPosition(SwingConstants.BOTTOM);

//Create a window using JFrame with title ( Set JButton pressed icon )
JFrame frame=new JFrame("Set JButton pressed icon");

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

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

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

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

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


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


pressedicon.png




notpressedicon.png

Set JButton icon

Complete source code below will show you, how to set icon on JButton.

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


import javax.swing.*;

public class SetJButtonIcon
{
public static void main(String[]args)
{
//Create an image icon from png file named ( callme.png )
//You can download this image below
//This file is locate at same location with this java file.
//If your file locate at other location,
//you must put it's full location address. For example :
//C:\\Documents and Settings\\evergreen\\Desktop\\callme.png
ImageIcon iconForButton=new ImageIcon("callme.png");

//Create a button from JButton with text ( Call me ) and image icon ( callme.png )
JButton button=new JButton("Call me",iconForButton);

//set button horizontal text position to center
//You can select one of the following SwingConstants:
//SwingConstants.CENTER
//SwingConstants.RIGHT
//SwingConstants.LEFT
//SwingConstants.LEADING
//SwingConstants.TRAILING (the default)
button.setHorizontalTextPosition(SwingConstants.CENTER);

//set vertical text position to bottom
//It make our text button locate under button icon
//You can select one of the following SwingConstants:
//SwingConstants.CENTER (the default)
//SwingConstants.TOP
//SwingConstants.BOTTOM
button.setVerticalTextPosition(SwingConstants.BOTTOM);

//Create a window from JFrame with title ( Set JButton icon )
JFrame frame=new JFrame("Set JButton icon");

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

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

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


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



callme.png

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

Set file type in JFileChooser

Complete source code below will show you, how set file type that should be display in a JFileChooser. Source code below will display all directories and text files only.

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


import javax.swing.filechooser.FileFilter;

import javax.swing.JFileChooser;

import java.io.File;

public class SetJFileChooserFileType
{
public SetJFileChooserFileType()
{
//Create a file chooser from JFileChooser
JFileChooser fileChooser=new JFileChooser();

//Set file type that should be display by set JFileChooser's file filter
fileChooser.setFileFilter(new TypeOfFile());

//Show JFileChooser
int a=fileChooser.showDialog(null,"Open Text File");
}

//Class TypeOfFile
class TypeOfFile extends FileFilter
{
//Type of file that should be display in JFileChooser will be set here
//We choose to display only directory and text file
public boolean accept(File f)
{
return f.isDirectory()||f.getName().toLowerCase().endsWith(".txt");
}

//Set description for the type of file that should be display
public String getDescription()
{
return ".text files";
}
}

public static void main(String[]args)
{
SetJFileChooserFileType sjfcft=new SetJFileChooserFileType();
}
}


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

Set password length for JPasswordField

Complete source code below will show you, how you can set password length in JPasswordField. In source code below, we will enable user to enter only password that contains five characters.After that it will disable password field. User can delete entered password by press 'Backspace' key.

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


import javax.swing.*;

import javax.swing.event.*;

import java.awt.event.*;

import java.awt.FlowLayout;

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

JLabel label=new JLabel("Press Backspace to delete password");

JFrame frame;

//Create key listener that will listen when someone press Backspace key
KeyListener kl=new KeyAdapter()
{
public void keyPressed(KeyEvent event)
{
if(event.getKeyCode()==KeyEvent.VK_BACK_SPACE)
{
passwordField.setEditable(true);

label.show(false);

frame.repaint();

frame.validate();
}
}
};

public SetPasswordLength()
{
label.show(false);

passwordField.addCaretListener(this);

passwordField.addKeyListener(kl);

frame=new JFrame("Set password length");

frame.setLayout(new FlowLayout());

frame.add(passwordField);

frame.add(label);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(500,100);

frame.setVisible(true);
}

//Listen for changes in the caret position
public void caretUpdate(CaretEvent event)
{
int passwordLength=passwordField.getText().length();

//If password length equal to five characters, password field will uneditable
//You can change to any password length that you want by change 5 to any other value
if(passwordLength==5)
{
passwordField.setEditable(false);

label.show(true);

frame.repaint();

frame.validate();
}
}

public static void main(String[]args)
{
SetPasswordLength spl=new SetPasswordLength();
}
}


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

Add scroll bar to JTextArea

Complete source code below will show you, how to add scroll bar to JTextArea.You can use scroll pane constants like below to set your text area scroll bar.

**************
HORIZONTAL
**************
HORIZONTAL_SCROLLBAR_ALWAYS
HORIZONTAL_SCROLLBAR_AS_NEEDED
HORIZONTAL_SCROLLBAR_NEVER

**************
VERTICAL
**************
VERTICAL_SCROLLBAR_ALWAYS
VERTICAL_SCROLLBAR_AS_NEEDED
VERTICAL_SCROLLBAR_NEVER

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


import javax.swing.*;

public class AddScrollBarToJTextArea
{
public static void main(String[]args)
{
//Create JTextArea
JTextArea textArea=new JTextArea("Put your text here");

//Create JScrollPane that will be use as JTextArea scrollbar from JTextArea object
JScrollPane scrollBar=new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

//Create a window using JFrame
JFrame frame=new JFrame("Add scrollbar to JTextArea");

//add created JScrollPane into JFrame
frame.add(scrollBar);

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

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

//Make JFrame get to center
frame.setLocationRelativeTo(null);

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


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

Set word wrap in JTextArea

Complete source code below will show you, how to set word wrap in JTextArea.

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


import javax.swing.*;

public class SetWordWrapInJTextArea
{
public static void main(String[]args)
{
JTextArea textArea=new JTextArea();

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

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

JFrame frame=new JFrame("Set word wrap in JTextArea");

frame.add(textArea);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(500,500);

frame.setLocationRelativeTo(null);

frame.setVisible(true);
}
}


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

Set JTextArea background color to JFrame default color

Complete source code below will show you, how to set JTextArea background color same to JFrame default color.

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


import javax.swing.*;

import java.awt.Color;

public class SetJTextAreaBackgroundColorToJFrameColor
{
public static void main(String[]args)
{
//Create a window using JFrame with title ( Set JTextArea background color to JFrame color )
JFrame frame=new JFrame("Set JTextArea background color to JFrame color");

//Get JFrame background color
Color jframeColor=frame.getBackground();

//Create a text area with initial text ( Put your text here )
JTextArea textArea=new JTextArea("Put your text here");

//Set text area background color same to JFrame background color
textArea.setBackground(jframeColor);

//Add created text area into JFrame
frame.add(textArea);

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

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

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

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


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

Easiest way to make JFrame center

Complete source code below will show you, the easiest way to make JFrame get to the center of screen by only one line of code. We will use method setLocationRelativeTo(null) to make our JFrame get to the center. Remember, when you want to use this code, you must put it after you set JFrame size.

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


import javax.swing.JFrame;

public class EasiestWayToMakeJFrameCenter
{
public static void main(String[]args)
{
JFrame frame=new JFrame("Easiest way to make JFrame center");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,400);

//setLocationRelativeTo(null) will make JFrame get to the center
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}


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

Get default JFrame background color

Complete source code below will show you, how to get default JFrame background color and after that, it will print RED,GREEN,BLUE(RGB) value for the color.

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


import javax.swing.JFrame;

import java.awt.Color;

public class GetDefaultJFrameBackgroundColor
{
public static void main(String[]args)
{
//Create a window using JFrame
JFrame frame=new JFrame();

//Get JFrame background color using method getBackground()
Color frameBackgroundColor=frame.getBackground();

//Get RED value from color
int redValue=frameBackgroundColor.getRed();

//Get GREEN value from color
int greenValue=frameBackgroundColor.getGreen();

//Get BLUE value from color
int blueValue=frameBackgroundColor.getBlue();

//Print RGB value that we get
System.out.println("R : "+redValue);
System.out.println("G : "+greenValue);
System.out.println("B : "+blueValue);

//You can check what color that match the RGB value that we get using 'Color picker' at above
}
}


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

RELAXING NATURE VIDEO