This Lecture covers three topics very simply, jtextarea, jframe and jscrollpane with the help of code and each line of code explain for more understanding. It is much easier for beginners.
11. 11
What is Jframe?
A frame, implemented as an instance of the JFrame
class, is a window that has decorations such as a
border, a title, and supports button components that
close or iconify the window. Applications with a GUI
usually include at least one frame
Lecture By:- Mr. Akaash
13. 13
Coding of JFrame
import java.awt.FlowLayout;
import javax.swing.JFrame;
public class JTextAreaTest {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextArea Test");
frame.setSize(280, 150);
frame.setLayout(new FlowLayout());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Built_in Packages of Java
Programming Language
Lecture By:- Mr. Akaash
14. 14
Coding of JFrame
import java.awt.FlowLayout;
import javax.swing.JFrame;
public class JTextAreaTest {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextArea Test");
frame.setSize(280, 150);
frame.setLayout(new FlowLayout());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Built_in Packages of Java
Programming Language
Main Class
Lecture By:- Mr. Akaash
15. 15
Coding of JFrame
import java.awt.FlowLayout;
import javax.swing.JFrame;
public class JTextAreaTest {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextArea Test");
frame.setSize(280, 150);
frame.setLayout(new FlowLayout());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Built_in Packages of Java
Programming Language
Main Class
Main Function
Lecture By:- Mr. Akaash
16. 16
Coding of JFrame
import java.awt.FlowLayout;
import javax.swing.JFrame;
public class JTextAreaTest {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextArea Test");
frame.setSize(280, 150);
frame.setLayout(new FlowLayout());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Built_in Packages of Java
Programming Language
Main Class
Main Function
Class
Lecture By:- Mr. Akaash
17. 17
Coding of JFrame
import java.awt.FlowLayout;
import javax.swing.JFrame;
public class JTextAreaTest {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextArea Test");
frame.setSize(280, 150);
frame.setLayout(new FlowLayout());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Built_in Packages of Java
Programming Language
Main Class
Main Function
Class Object
Lecture By:- Mr. Akaash
18. 18
Coding of JFrame
import java.awt.FlowLayout;
import javax.swing.JFrame;
public class JTextAreaTest {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextArea Test");
frame.setSize(280, 150);
frame.setLayout(new FlowLayout());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Built_in Packages of Java
Programming Language
Main Class
Main Function
Class Object Keyword
Lecture By:- Mr. Akaash
19. 19
Coding of JFrame
import java.awt.FlowLayout;
import javax.swing.JFrame;
public class JTextAreaTest {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextArea Test");
frame.setSize(280, 150);
frame.setLayout(new FlowLayout());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Built_in Packages of Java
Programming Language
Main Class
Main Function
Class Object Keyword Constructor
Lecture By:- Mr. Akaash
20. 20
Coding of JFrame
import java.awt.FlowLayout;
import javax.swing.JFrame;
public class JTextAreaTest {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextArea Test");
frame.setSize(280, 150);
frame.setLayout(new FlowLayout());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Built_in Packages of Java
Programming Language
Main Class
Main Function
Class Object Keyword Constructor Value
Lecture By:- Mr. Akaash
21. 21
Coding of JFrame
frame.setSize(280, 150);
frame.setLayout(new FlowLayout());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Object
Lecture By:- Mr. Akaash
22. 22
Coding of JFrame
frame.setSize(280, 150);
frame.setLayout(new FlowLayout());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Object Method
Lecture By:- Mr. Akaash
34. 34
Coding of JFrame
frame.setSize(280, 150);
frame.setLayout(new FlowLayout());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Object Method Width Height
Object Method Keyword Method
Object Method Boolean value
Object Method Class
Lecture By:- Mr. Akaash
35. 35
Coding of JFrame
frame.setSize(280, 150);
frame.setLayout(new FlowLayout());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Object Method Width Height
Object Method Keyword Method
Object Method Boolean value
Object Method Value Class Method
Lecture By:- Mr. Akaash
36. 36
Explanation of Previously Used
Methods
setSize(). Method allows you to set the size like Width & height of JFrame
setResizable(). One method for ensuring that a graphical interface looks the way you intend is
to prevent the user from re-sizing it, using this method of JFrame :
setLayout(). Method allows you to set the layout of the container, often a JPanel, to say
FlowLayout(), BorderLayout(), GridLayout(), null layout(), or
whatever layout desired. The layout manager helps lay out the components held
by this container
setDefaultCloseOperation() method is used to specify one of several options for the close
button. Use one of the following constants to specify your choice:
JFrame.EXIT_ON_CLOSE — Exit the application.
JFrame.HIDE_ON_CLOSE — Hide the frame, but keep the application running.
JFrame.DISPOSE_ON_CLOSE — Dispose of the frame object, but keep the application running.
JFrame.DO_NOTHING_ON_CLOSE — Ignore the click.
If you forget to call setDefaultCloseOperation() you will get JFrame.HIDE_ON_CLOSE by default.
This can be frustrating, because it looks like you have "killed" the program, but it keeps on running,
and you see no frame.
Lecture By:- Mr. Akaash
37. 37
What is JTextArea?
A JTextArea is a multi-line area that displays plain
text. It is intended to be a lightweight component that
provides source compatibility with the
java.awt.TextArea class where it can reasonably do
so.
Lecture By:- Mr. Akaash
39. 39
Coding of JTextArea
JTextArea textArea1 = new JTextArea(text);
textArea1.setPreferredSize(new Dimension(100, 100));
textArea1.setLineWrap(true);
frame.add(textArea1);
Class Object Keyword Constructor Value
Object Method Keyword Method Width Height
Object Method Boolean value
Object Method Object
Lecture By:- Mr. Akaash
40. 40
Constructors and Description
JTextArea()
Constructs a new TextArea.
JTextArea(Document doc)
Constructs a new JTextArea with the given document model, and defaults for all of the other
arguments (null, 0, 0).
JTextArea(Document doc, String text, int rows, int columns)
Constructs a new JTextArea with the specified number of rows and columns, and the given model.
JTextArea(int rows, int columns)
Constructs a new empty TextArea with the specified number of rows and columns.
JTextArea(String text)
Constructs a new TextArea with the specified text displayed.
JTextArea(String text, int rows, int columns)
Constructs a new TextArea with the specified text and number of rows and columns.
Lecture By:- Mr. Akaash
41. 41
Methods
Modifier and Type Method and Description
void
append(String str)
Appends the given text to the end of the document.
protected
Document
createDefaultModel()
Creates the default implementation of the model to be used at
construction if one isn't explicitly given.
AccessibleContext
getAccessibleContext()
Gets the AccessibleContext associated with this JTextArea.
int
getColumns()
Returns the number of columns in the TextArea.
protected int
getColumnWidth()
Gets column width.
int
getLineCount()
Determines the number of lines contained in the area.
int
getLineEndOffset(int line)
Determines the offset of the end of the given line.
Lecture By:- Mr. Akaash
42. 42
Methods
Modifier and Type Method and Description
boolean
getLineWrap()
Gets the line-wrapping policy of the text area.
Dimension
getPreferredScrollableViewportSize()
Returns the preferred size of the viewport if this component is embedded in a
JScrollPane.
Dimension
getPreferredSize()
Returns the preferred size of the TextArea.
protected int
getRowHeight()
Defines the meaning of the height of a row.
int
getRows()
Returns the number of rows in the TextArea.
boolean
getScrollableTracksViewportWidth()
Returns true if a viewport should always force the width of this Scrollable to
match the width of the viewport.
int
getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
Components that display logical rows or columns should compute the scroll
increment that will completely expose one new row or column, depending on
the value of orientation.
Lecture By:- Mr. Akaash
43. 43
Methods
Modifier & Type Method and Description
Int getTabSize()
Gets the number of characters used to expand tabs.
String getUIClassID()
Returns the class ID for the UI.
Boolean getWrapStyleWord()
Gets the style of wrapping used if the text area is wrapping lines.
Void insert(String str, int pos)
Inserts the specified text at the specified position.
protected String paramString()
Returns a string representation of this JTextArea.
Void replaceRange(String str, int start, int end)
Replaces text from the indicated start to end position with the new
text specified.
Void setColumns(int columns)
Sets the number of columns for this TextArea.
Void setTabSize(int size)
Sets the number of characters to expand tabs to.
Void setWrapStyleWord(boolean word)
Sets the style of wrapping used if the text area is wrapping lines.
45. 45
Coding of JTextArea with
ScrollPane
JTextArea textArea2 = new JTextArea(text, 5, 10);
textArea2.setLineWrap(true);
JScrollPane scrollPane = new
JScrollPane(textArea2,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.add(scrollPane);
Lecture By:- Mr. Akaash
46. 46
Why JScrollPane used with
JTextArea?
A JTextArea object represents a multiline area for displaying
text. If Text is too large from JTextArea Boundaries(Width,
Height) then You can also put your JTextArea in a
JScrollPane to make it scrollable.
Lecture By:- Mr. Akaash
What is Jframe?
A frame, implemented as an instance of the JFrame class, is a window that has
decorations such as a border, a title, and supports button components that close
or iconify the window. Applications with a GUI usually include at least one frame.
Coding of JFrame
import java.awt.FlowLayout;
import javax.swing.JFrame;
public class JTextAreaTest {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextArea Test");
frame.setSize(280, 150);
frame.setLayout(new FlowLayout());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Coding of JFrame
import java.awt.FlowLayout;
import javax.swing.JFrame;
public class JTextAreaTest {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextArea Test");
frame.setSize(280, 150);
frame.setLayout(new FlowLayout());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Coding of JFrame
import java.awt.FlowLayout;
import javax.swing.JFrame;
public class JTextAreaTest {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextArea Test");
frame.setSize(280, 150);
frame.setLayout(new FlowLayout());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Coding of JFrame
import java.awt.FlowLayout;
import javax.swing.JFrame;
public class JTextAreaTest {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextArea Test");
frame.setSize(280, 150);
frame.setLayout(new FlowLayout());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Coding of JFrame
import java.awt.FlowLayout;
import javax.swing.JFrame;
public class JTextAreaTest {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextArea Test");
frame.setSize(280, 150);
frame.setLayout(new FlowLayout());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Coding of JFrame
import java.awt.FlowLayout;
import javax.swing.JFrame;
public class JTextAreaTest {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextArea Test");
frame.setSize(280, 150);
frame.setLayout(new FlowLayout());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Coding of JFrame
import java.awt.FlowLayout;
import javax.swing.JFrame;
public class JTextAreaTest {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextArea Test");
frame.setSize(280, 150);
frame.setLayout(new FlowLayout());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Coding of JFrame
import java.awt.FlowLayout;
import javax.swing.JFrame;
public class JTextAreaTest {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextArea Test");
frame.setSize(280, 150);
frame.setLayout(new FlowLayout());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Coding of JFrame
import java.awt.FlowLayout;
import javax.swing.JFrame;
public class JTextAreaTest {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextArea Test");
frame.setSize(280, 150);
frame.setLayout(new FlowLayout());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
What is JTextArea?
A JTextArea is a multi-line area that displays plain text.
It is intended to be a lightweight component that provides
source compatibility with the java.awt.TextArea class where it can reasonably do so.
Coding of JTextArea
JTextArea textArea1 = new JTextArea(text);
textArea1.setPreferredSize(new Dimension(100, 100));
textArea1.setLineWrap(true);
frame.add(textArea1);
Constructors and Description
JTextArea()
Constructs a new TextArea.
JTextArea(Document doc)
Constructs a new JTextArea with the given document model, and defaults for all of the other arguments (null, 0, 0).
JTextArea(Document doc, String text, int rows, int columns)Constructs a new JTextArea with the specified number of rows and columns, and the given model.
JTextArea(int rows, int columns)
Constructs a new empty TextArea with the specified number of rows and columns.
JTextArea(String text)
Constructs a new TextArea with the specified text displayed.
JTextArea(String text, int rows, int columns)
Constructs a new TextArea with the specified text and number of rows and columns.
Methods
Modifier and Type Method and Description
Void append(String str)
Appends the given text to the end of the document.
protected Document createDefaultModel()
Creates the default implementation of the model to be used at construction if one isn't explicitly given.
AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this JTextArea.
Int getColumns()
Returns the number of columns in the TextArea.
protected int getColumnWidth()
Gets column width.
Int getLineCount()
Determines the number of lines contained in the area.
Int getLineEndOffset(int line)
Determines the offset of the end of the given line.
Methods
Modifier and Type Method and Description
Boolean getLineWrap()
Gets the line-wrapping policy of the text area.
Dimension getPreferredScrollableViewportSize()
Returns the preferred size of the viewport if this component is embedded in a JScrollPane.
Dimension getPreferredSize()
Returns the preferred size of the TextArea.
protected int getRowHeight()
Defines the meaning of the height of a row.
Int getRows()
Returns the number of rows in the TextArea.
Boolean getScrollableTracksViewportWidth()
Returns true if a viewport should always force the width of this Scrollable to match the width of the viewport.
Int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) Components that display logical rows or columns should compute the scroll increment that will completely expose one new row or column, depending on the value of orientation.
Methods
Modifier and Type Method and Description
Int getTabSize()
Gets the number of characters used to expand tabs.
String getUIClassID()
Returns the class ID for the UI.
Boolean getWrapStyleWord()
Gets the style of wrapping used if the text area is wrapping lines.
Void insert(String str, int pos)
Inserts the specified text at the specified position.
protected String paramString()
Returns a string representation of this JTextArea.
Void replaceRange(String str, int start, int end)
Replaces text from the indicated start to end position with the new text specified.
Void setColumns(int columns)
Sets the number of columns for this TextArea.
Void setTabSize(int size)
Sets the number of characters to expand tabs to.
Void setWrapStyleWord(boolean word)
Sets the style of wrapping used if the text area is wrapping lines.
Coding of JTextArea with ScrollPane
JTextArea textArea2 = new JTextArea(text, 5, 10);
textArea2.setLineWrap(true);
JScrollPane scrollPane = new JScrollPane(textArea2,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.add(scrollPane);
Why JScrollPane used with JTextArea?
A JTextArea object represents a multiline area for displaying text.
If Text is too large from JTextArea Boundaries(Width, Height) then
You can also put your JTextArea in a JScrollPane to make it scrollable.