Increase JTextArea Font Size with JSlider

Here is sample tutorial to increase JTextArea font size using JSlider.


import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
class JTextAreaFont extends JFrame
{

JTextArea jt;
JScrollPane js;
JSlider jl;

public JTextAreaFont()
{

setTitle("Increase JTextArea Font Size");
setLayout(new GridLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);

jt=new JTextArea();
jt.setLineWrap(true);
jt.setWrapStyleWord(true);
jt.setFont(new Font("Times New Roman",Font.BOLD,20));
js=new JScrollPane(jt);
add(js);

jl=new JSlider(1,200);
jl.setValue(20);
add(jl);

jl.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent ce)
{
jt.setFont(new Font("Times New Roman",Font.BOLD,jl.getValue()));
}
 });

 setExtendedState(MAXIMIZED_BOTH);

}

public static void main(String args[])
{
new JTextAreaFont();
}

}


javax.swing.*: For JFrame, JTextArea, JSlider components.

java.awt.*: For GridLayout, Font

javax.swing.event.*: For ChangeListener, ChangeEvent

JTextArea: Holds text of multiple lines of text.

jt.setLineWrap(true): Proceed to next line, when reached to end ignoring words.

jt.setWrapStyleWord(true): When, word reaches end of line incompletely, entire word settles in a next line. As observed in Notepad.

JScrollPane: For JTextArea, when size is increased, JTextArea looks odd, so scrollpane is added.

JSlider: Observed in Media players, used in adjusting progress. Here used for increasing font size.

jl.setValue(20): Font size of JTextArea is 20 initially. See above.

ChangeEvent: Generated when user changes JSlider value.

ChangeListener: Listener for ChangeEvent.

setFont(font): Used to set font for JTextArea, takes java.awt.Font as parameter, Font class constructor consists of three parameters, font name, style, size respectively.

setExtendedState(MAXIMIZED_BOTH): Make frame maximized both the sides.