The text area created by JTextArea class is not scrollable by default. To make the text area scrollable in both directions (vertical and horizontal) you need to place the text area in the JScrollPane. If you want only the vertical scroll bar, invoke the setLineWrap(boolean wrap) method and the wrap's value is true.
Constructors of the JTextArea class:
-JTextArea()
creates an empty text area.
-JTextArea(String text)
creates a text area with text.
-JTextArea(int rows, int columns)
creates a text area with specified number of rows and columns.
-JTextArea(String text,int rows, int columns)
creates a text area with text and specified number of rows and columns.
-JTextArea(Document doc)
creates a text area with specified document model.
Useful methods of the JTextArea class:
-copy():void
copies the selected text in the text area.
-cut():void
cuts the selected text int the text area.
-getText():String
gets text of the text area.
-paste():void
pastes the copied text.
-select(int start,int end):void
selects a range of text from start to end.
-selectAll():void
selects text in the text area.
-setText(String text):void
sets the text of the text area.
Example:
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import javax.swing.plaf.basic.BasicBorders;
class JTextAreaShow extends JFrame{
JTextArea txta;
JTextAreaShow(String title){
setTitle(title);
setSize(400,300);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contpane=getContentPane();
Border border=new BasicBorders.ButtonBorder(Color.GREEN,Color.BLACK,Color.GREEN,Color.YELLOW);
JButton btcopy=new JButton();
btcopy.setBorder(border);
btcopy.setAction(new AAction("Copy"));
JButton btcut=new JButton("Cut");
btcut.setBorder(border);
btcut.setAction(new AAction("Cut"));
JButton btpaste=new JButton("Paste");
btpaste.setBorder(border);
btpaste.setAction(new AAction("Paste"));
JButton btselect=new JButton();
btselect.setBorder(border);
btselect.setAction(new AAction("Select"));
JPanel panelbt=new JPanel();
panelbt.add(btcopy);
panelbt.add(btcut);
panelbt.add(btpaste);
panelbt.add(btselect);
panelbt.setLayout(new FlowLayout(FlowLayout.LEFT));
JPanel panel=new JPanel();
panel.add(panelbt);
txta=new JTextArea(10,30);
txta.setLineWrap(true);
JScrollPane scroll=new JScrollPane(txta);
panel.add(scroll);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
contpane.add(panel);
setVisible(true);
}
class AAction extends AbstractAction{
AAction(String title){
super(title);
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("Copy"))
txta.copy();
else if(e.getActionCommand().equals("Cut"))
txta.cut();
else if(e.getActionCommand().equals("Paste"))
txta.paste();
else{
txta.requestFocus();
txta.selectAll();
}
}
}
}
public class JFrameSwing {
public static void main(String[] args){
new JTextAreaShow("JTextArea");
}
}
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import javax.swing.plaf.basic.BasicBorders;
class JTextAreaShow extends JFrame{
JTextArea txta;
JTextAreaShow(String title){
setTitle(title);
setSize(400,300);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contpane=getContentPane();
Border border=new BasicBorders.ButtonBorder(Color.GREEN,Color.BLACK,Color.GREEN,Color.YELLOW);
JButton btcopy=new JButton();
btcopy.setBorder(border);
btcopy.setAction(new AAction("Copy"));
JButton btcut=new JButton("Cut");
btcut.setBorder(border);
btcut.setAction(new AAction("Cut"));
JButton btpaste=new JButton("Paste");
btpaste.setBorder(border);
btpaste.setAction(new AAction("Paste"));
JButton btselect=new JButton();
btselect.setBorder(border);
btselect.setAction(new AAction("Select"));
JPanel panelbt=new JPanel();
panelbt.add(btcopy);
panelbt.add(btcut);
panelbt.add(btpaste);
panelbt.add(btselect);
panelbt.setLayout(new FlowLayout(FlowLayout.LEFT));
JPanel panel=new JPanel();
panel.add(panelbt);
txta=new JTextArea(10,30);
txta.setLineWrap(true);
JScrollPane scroll=new JScrollPane(txta);
panel.add(scroll);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
contpane.add(panel);
setVisible(true);
}
class AAction extends AbstractAction{
AAction(String title){
super(title);
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("Copy"))
txta.copy();
else if(e.getActionCommand().equals("Cut"))
txta.cut();
else if(e.getActionCommand().equals("Paste"))
txta.paste();
else{
txta.requestFocus();
txta.selectAll();
}
}
}
}
public class JFrameSwing {
public static void main(String[] args){
new JTextAreaShow("JTextArea");
}
}
No comments:
Post a Comment