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
**********************************************************************
Related Posts Plugin for WordPress, Blogger...

I Also Can Create T-Shirt Logo For You