Found 4338 Articles for Java 8

Java Program to get text from JTextPane and display in Console

Anvi Jain
Updated on 30-Jul-2019 22:30:26

581 Views

To get text from JTextPane, use the getText() method. At first, create a text pane component −JTextPane textPane = new JTextPane();Now, get the text −textPane.getText()); Display in Console: System.out.println(textPane.getText());The following is an example to get JTextPane and display in Console −Examplepackage my; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextPane; import javax.swing.text.BadLocationException; import javax.swing.text.SimpleAttributeSet; import javax.swing.text.StyleConstants; public class SwingDemo {    public static void main(String args[]) throws BadLocationException {       JFrame frame = new JFrame("Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       Container container = frame.getContentPane();       JTextPane textPane = new JTextPane(); ... Read More

Java Program to format text in JTextPane

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

316 Views

To format text in JTextPane, use the SimpleAttributeSet and StyleConstants class. This allows you to set the style of text, background color, foreground color, etc.At first, create a new JTextPane −JTextPane pane = new JTextPane();Now, use the classes to set the style and color −SimpleAttributeSet attributeSet = new SimpleAttributeSet(); StyleConstants.setItalic(attributeSet, true); StyleConstants.setForeground(attributeSet, Color.black); StyleConstants.setBackground(attributeSet, Color.orange);Now, apply the set to the pane −pane.setCharacterAttributes(attributeSet, true);The following is an example to format text in JTextPane −Examplepackage my; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextPane; import javax.swing.text.BadLocationException; import javax.swing.text.SimpleAttributeSet; import javax.swing.text.StyleConstants; public class SwingDemo {    public static void ... Read More

Java Program to set JTextArea to wrap by word in Java

Smita Kapse
Updated on 30-Jul-2019 22:30:26

497 Views

To set JTextAream to wrap by word, you need to use the setWrapStyleWord(). Let’s say we have created a new JTextArea and set demo text −JTextArea textArea = new JTextArea("This is a text displayed for our example. More content is added in it now. More content is added in it now. We will now wrap this text!!!!!!!!!!!!!!!!!!!");Now, wrap by word and set it TRUE −textArea.setWrapStyleWord(true)The following is an example to set JTextArea to wrap by word −Examplepackage my; import java.awt.GridLayout; import javax.swing.*; public class SwingDemo {    SwingDemo() {       JFrame frame = new JFrame("Demo");       ... Read More

Java Program to replace the first 10 characters in JTextArea

Anvi Jain
Updated on 30-Jul-2019 22:30:26

199 Views

To replace the first 10 character in text area, use the replaceRange() method in Java and replace the old text with the new text.Let’s say the following is oude demo text set with JTextArea −JTextArea textArea = new JTextArea("This is a text displayed for our example. We have replaced some of the text.");Now, replace the characters in a range −int begn = 0; int end = 10; // replace textArea.replaceRange("Replaced! ", begn, end);The following is an example to replace the first 10 charactrers in JTextArea −Examplepackage my; import java.awt.GridLayout; import javax.swing.*; public class SwingDemo {    SwingDemo() {     ... Read More

Can we select only some of the text in JTextArea?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

297 Views

Yes, we can do that using built-in methods of JTextArea components. Let’say the following is our JTextArea −JTextArea textArea = new JTextArea("This is a text displayed for our example. We have selected some of the text.");Now, use the methods setSelectionStart() and setSelectionEnd() to select some text in a range −textArea.setSelectionStart(5); textArea.setSelectionEnd(20);The following is an example to select some of the text in a JTextArea −Examplepackage my; import java.awt.GridLayout; import javax.swing.*; public class SwingDemo {    SwingDemo() {       JFrame frame = new JFrame("Demo");       JTextArea textArea = new JTextArea("This is a text displayed for our example. ... Read More

Java Program to paste clipboard text to JTextArea

Smita Kapse
Updated on 30-Jul-2019 22:30:26

571 Views

To paste clopboard text to JTextArea, use tha paste () method. Let’s say the following is our text area −JTextArea textArea = new JTextArea("");Now, paste the clipboard text −textArea.paste();Note − Let’s say our clipboard text is “This is clipboard text that got inserted in the text area”.The following is an example to paste clipboard text to JTextArea −Examplepackage my; import java.awt.Container; import java.awt.GridLayout; import javax.swing.*; public class SwingDemo {    SwingDemo() {       JFrame frame = new JFrame("Demo");       JTextArea textArea = new JTextArea("");       Container c = frame.getContentPane();       c.setLayout(new GridLayout(0, ... Read More

Delete the first 10 characters from JTextArea in Java

Anvi Jain
Updated on 30-Jul-2019 22:30:26

313 Views

Let’s say the following is our JTextArea with default text −JTextArea textArea = new JTextArea("The text added here is just for demo. "    + "This demonstrates the usage of JTextArea in Java. In this example we have"    + "deleted some text.");Now to remove the first 10 characters, use replaceRange() method and set null from one end to another i.e. deleting characters in a range. The replaceRaneg() method Replaces text from the indicated start to end position with the new text specified i.e.e here null will replace the first 10 characters −int start = 0; int end = 10; ... Read More

Create empty border with BorderFactory class in Java?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

366 Views

To create empty border, use the createEmptyBorder() method. Let us first create a label component −JLabel label = new JLabel("Label with empty border!");Now, create empty border with BorderFactory class −label.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));The following is an example to create empty border −Examplepackage my; import javax.swing.BorderFactory; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JLabel; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new JFrame("Demo");       JLabel label;       label = new JLabel("Label with empty border!");       label.setFont(new Font("Verdana", Font.PLAIN, 16));       label.setVerticalAlignment(JLabel.BOTTOM);     ... Read More

How to get the leaves of nodes in a JTree with Java?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

374 Views

To get the leaves of nodes, use the getLeafCount() method.Let’s say you want the leaves of the entire tree, then use the root node, Let’s say “node” is our root node −node.getLeafCount()Now, let’s say we want to get the leaves of a node, which is not a root node, therefore set the node. Here, node1 is not a root node −node1.getLeafCount()The following is an example to get the leaves of nodes −Examplepackage my; import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new JFrame("Demo"); ... Read More

Java Program to deselect all cells in a JTable

Anvi Jain
Updated on 30-Jul-2019 22:30:26

345 Views

At first, let’s say, we have selected a row using addRowSelectionInterval() as shown in the demo screenshot −Now, we will deselect all these cells using clearSelection() as shown in the following example. This method clears the selected cells from the table −Examplepackage my; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class SwingDemo {    public static void main(String[] argv) throws Exception {       DefaultTableModel tableModel = new DefaultTableModel();       JTable table = new JTable(tableModel);       tableModel.addColumn("Language/ Technology");       tableModel.addColumn("Text Tutorial");       tableModel.addColumn("Video Tutorial");       tableModel.addColumn("Interview QA"); ... Read More

Advertisements