Found 9150 Articles for Object Oriented Programming

Delete the first 10 characters from JTextArea in Java

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

448 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

580 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

498 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

Alshifa Hasnain
Updated on 31-Dec-2024 22:02:44

639 Views

In this article, we will learn to deselect all cells in a JTable in Java. The JTable component is a powerful tool for displaying tabular data when working with Java Swing applications. Sometimes, you may want to deselect all selected cells in a JTable programmatically. Why Deselecting Cells in JTable is Important In certain scenarios, such as resetting form fields, implementing "Clear Selection" buttons, or preparing the table for a fresh data display, you need to clear the selection in a JTable. This improves user experience and ensures the application behaves as expected. Deselecting All Cells in JTable Following are the ... Read More

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

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

80 Views

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 selection of a cell −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");     ... Read More

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

Chandu yadav
Updated on 30-Jul-2019 22:30:26

396 Views

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; import javax.swing.tree.DefaultMutableTreeNode; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new JFrame("Demo");       DefaultMutableTreeNode node = new DefaultMutableTreeNode("Products");       DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("Clothing (Product1 - P66778)");       DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Accessories ... Read More

How to create a date spinner in Java?

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

1K+ Views

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 create a date spinner in Java −Examplepackage my; import java.awt.GridBagLayout; import java.util.Calendar; import java.util.Date; import javax.swing.*; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame("Spinner Demo");       JPanel panel = new JPanel();       JLabel label = new JLabel("Exam No.");       JLabel label2 = new JLabel(" Appeared On");       panel.setLayout(new GridBagLayout());       int min = 0;       int max = 10;       int step = 1; ... Read More

How to check if two nodes are equal in a JTree with Java?

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

789 Views

To check if two nodes are equal, use the equals() method. Here, we are checking that node1 and node2 are equal or not.node1.equals(node2)The following is an example to check if two nodes are equal in a JTree −package 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");       DefaultMutableTreeNode node = new DefaultMutableTreeNode("Website");       DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("Videos");       DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Tutorials");       DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("QA");     ... Read More

How to set TabLayout Policy to JTabbedPane in Java when all the tabs does not fit in a single run

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

409 Views

To set TabLayout Policy to JTabbedPane in Java when all the tabs does not fit in a single run, use the method setTabLayoutPolicy() −JTabbedPane tabbedPane = new JTabbedPane(); tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);Above we have set the constant to be SCROLL_TAB_LAYOUT, which is a tab layout policy for providing a subset of available tabs when all the tabs will not fit within a single run.The following is an example −package my; import javax.swing.*; import java.awt.*; import java.awt.event.KeyEvent; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Devices");       JTabbedPane tabbedPane = new JTabbedPane(); ... Read More

How to change the background and foreground colors of tab in Java?

George John
Updated on 30-Jul-2019 22:30:26

906 Views

To change the background and foreground colors of tab, use the following methods −JTabbedPane tabbedPane = new JTabbedPane(); tabbedPane.setBackground(Color.blue); tabbedPane.setForeground(Color.white);Above, we have used the Color class to set the colors for the background and foregroud colors −The following is an example to change the background and foreground colors of tab −package my; import javax.swing.*; import java.awt.*; import java.awt.event.KeyEvent; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Devices");       JTabbedPane tabbedPane = new JTabbedPane();       JPanel panel1, panel2, panel3, panel4, panel5;       panel1 = new ... Read More

Advertisements