Can we select only some of the text in JTextArea?

Nishtha Thakur
Published on 31-May-2019 16:07:56
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); ... Read More

Java Program to paste clipboard text to JTextArea

Smita Kapse
Published on 31-May-2019 16:05:11
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 ... Read More

Set whether the cell in the table model can be selected or deselected in Java?

Nishtha Thakur
Published on 31-May-2019 15:49:04
We can set or disallow selection of cell in the table using setCellSelectionEnabled(). The following is an example. −If you want to allow selection of cell, then set the method to TRUE −table.setCellSelectionEnabled(true);If you want to disallow selection of cell, then set the method to FALSE −table.setCellSelectionEnabled(false);Here we have disallowed ... Read More

How to add a Tab in JTabbedPane with Java?

Arjun Thakur
Published on 31-May-2019 15:38:13
Let us first create a JTabbedPane −JTabbedPane tabbedPane = new JTabbedPane();Create some panels −JPanel panel1, panel2, panel3, panel4, panel5; panel1 = new JPanel(); panel2 = new JPanel(); panel3 = new JPanel(); panel4 = new JPanel(); panel5 = new JPanel();Now, add tabs using the addTab() method and set the panels as ... Read More

Delete the first 10 characters from JTextArea in Java

Anvi Jain
Published on 31-May-2019 16:00:54
Let’s say the following is our JTextArea with default text −JTextArea textArea = new JTextArea("The text added here is just for demo. "    + "\nThis demonstrates the usage of JTextArea in Java. In this example we have"    + "deleted some text.");Now to remove the first 10 characters, use ... Read More

Create empty border with BorderFactory class in Java?

Smita Kapse
Published on 31-May-2019 15:58:32
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 ... Read More

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

Smita Kapse
Published on 31-May-2019 15:55:32
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 ... Read More

Java Program to deselect all cells in a JTable

Anvi Jain
Published on 31-May-2019 15:52:16
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 ... Read More

How to get this node’s first child in a JTree with Java?

Chandu yadav
Published on 31-May-2019 15:49:28
Let’s say we want the first child node of a node, then use the getFirstChild() method −node2.getFirstChild()Display the node’s first child on Console −System.out.println("The first child of node 2 = "+node2.getFirstChild());The following is an example to get this node’s first child in a JTree −package my; import javax.swing.JFrame; import javax.swing.JTree; ... Read More

How to create a date spinner in Java?

Smita Kapse
Published on 31-May-2019 15:47:25
To create a date spinner, use the SpinnerDateModel class. Within that set the date format −Date today = new Date(); JSpinner spinner2 = new JSpinner(new SpinnerDateModel(today, null, null, Calendar.MONTH)); JSpinner.DateEditor editor = new JSpinner.DateEditor(spinner2, "dd/MM/yy"); spinner2.setEditor(editor);Above, we have set the Date format to be −dd/MM/yyThe following is an example to ... Read More
Advertisements