Found 9150 Articles for Object Oriented Programming

Java program to count the child of root node in a JTree

George John
Updated on 13-Nov-2024 19:20:01

323 Views

In this article, we will go over a Java program that counts the number of child nodes of the root node in a JTree using Java. This program uses the getChildCount() method to retrieve and display the count of direct child nodes under the root. This is helpful for applications involving hierarchical structures, like directories or organizational charts. Steps to count the child of the root node in a JTree Following are the steps to count the child of the root node in a JTree −Import the necessary classes JFrame, JTree, and DefaultMutableTreeNode from java.swing package.Create a main class named SwingDemo.Define the ... Read More

Disable only the vertical scrollbar in Java

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

1K+ Views

To disable only the vertical scrollbar, use the VERTICAL_SCROLLBAR_NEVER constant, which displays only the horizontal scrollbar.The following is an example to disable only the vertical scrollbar in Java −Examplepackage my; import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.Box; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JButton button1 = new JButton("Online Compiler");       JButton button2 = new JButton("Quiz");       JButton button3 = new JButton("Questions and Answers");       JButton button4 = new JButton("Videos"); ... Read More

Can we prevent the collapse of nodes and child nodes in a JTree with Java?

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

439 Views

Yes, we can prevent the collapse of nodes and child nodes in a JTree using setExpandedState() method. This method sets the expanded state of this JTree.If state istrue, all parents of path and path are marked asexpanded.The following is an example that prevents the collapse of nodes and child nodes in a JTree with Java −Examplepackage my; import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.text.Position; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreePath; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new JFrame("Demo");       DefaultMutableTreeNode node = new DefaultMutableTreeNode("Project");       DefaultMutableTreeNode ... Read More

How to set the content of the label to be right-justified and top-aligned in Java?

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

134 Views

To set the text of the label component to be right-justified and top-aligned, you need to set the alignment. Set the label to be on the right and top aligned −JLabel label = new JLabel("Fav Sports", JLabel.RIGHT); label.setVerticalAlignment(JLabel.TOP);Here, we have set the size of the label as well as the color that includes foreground and background color −label.setPreferredSize(new Dimension(220, 70)); label.setOpaque(true); label.setBackground(Color.YELLOW); label.setForeground(Color.RED);The following is an example to set the content of the lable to be right-justified and top-aligned −package my; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.WindowConstants; public class SwingDemo ... Read More

Set the content of the JLabel to be left-justified and top-aligned in Java

Revathi Satya Kondra
Updated on 17-Jan-2025 19:41:23

803 Views

To set the text of the label component to be left-justified and top-aligned, you need to set the alignment. Set the label to be on the left and top aligned − JLabel label = new JLabel("Department”); label.setVerticalAlignment(JLabel.TOP); Here, we have set the size of the label as well as the color that includes foreground and background color − label.setPreferredSize(new Dimension(220, 70)); label.setOpaque(true); label.setBackground(Color.MAGENTA); label.setForeground(Color.WHITE); In Java, when we talk about setting the content of a JLabel to be left-justified and top-aligned, we are referring to how the text or icon within the label is positioned relative to the label's ... Read More

How to set the location of the Tabs in a JTabbedPane Container to the left in Java?

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

744 Views

To set the location of the tabs in a JTabbedPane container, use the LEFT constant. Here, we will set the position to the left −JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT);The following is an example to set the location of the Tabs in a JTabbedPane Container to the left −Examplepackage my; import javax.swing.*; import java.awt.*; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Devices");       JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT);       JTextArea text = new JTextArea(100, 100);       JPanel panel1, panel2, panel3, panel4, panel5, panel6, panel7, ... Read More

How can we change the default font of the JTabbedPane tabs in Java?

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

411 Views

To display the default font of the tabs, you need to use the Font class. Let’s say we created a JTabbedPane in Java −JTabbedPane tabbedPane = new JTabbedPane();Now, set the Font with font face, style and font size −Font font = new Font("Arial", Font.CENTER_BASELINE, 20); tabbedPane.setFont(font);The following is an example to change the default font of the JTabbedPane tabs −package my; import javax.swing.*; import java.awt.*; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Technologies");       JTabbedPane tabbedPane = new JTabbedPane();       JPanel panel1, panel2, panel3, panel4, ... Read More

Java program to change the background color of rows in a JTable

Nishtha Thakur
Updated on 23-Sep-2024 19:08:45

1K+ Views

In this program, we will create a JTable and change the background color of its rows. A JTable is used to display data in a tabular format, and by adjusting its properties, we can modify its appearance. The goal of this program is to set a custom background color for the rows of a JTable and change the font and row height to improve readability. Steps to change the background color of rows in a JTable Following are the steps to change the background color of rows in a JTable − First, we will import the ... Read More

Can we display a JTabPane with TextArea in one of the tabs with Java

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

263 Views

Yes, we can display a JTabPane with TextArea in one of the tabs. For that, let us first create a JTabbedPane component −JTabbedPane tabbedPane = new JTabbedPane();Now, create a text area you want to set under one of the tabs −JTextArea text = new JTextArea(100, 100);Now, set panels for the tabs. Under one of them, set the text area we created above as shown below −panel2 = new JPanel(); panel2.add(text); panel3 = new JPanel(); panel4 = new JPanel(); panel5 = new JPanel(); panel6 = new JPanel(); panel7 = new JPanel(); panel8 = new JPanel();Now, one by one create different tabs ... Read More

Java program to set the content of the JLabel to be left-justified and bottom-aligned

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

215 Views

To set the text of the label component to be left-justified and bottom-aligned, you need to set the alignment. Set the label to be on the left and bottom aligned −JLabel label = new JLabel("Fav Sports", JLabel.LEFT); label.setVerticalAlignment(JLabel.BOTTOM);Here, we have set the size of the label as well as the color that includes foreground and background color −label.setPreferredSize(new Dimension(220, 70)); label.setOpaque(true); label.setBackground(Color.YELLOW); label.setForeground(Color.RED);The following is an example to set the content of the label to be left-justified and bottom-aligned −Examplepackage my; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.WindowConstants; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame("Frame");       frame.setLayout(new FlowLayout());   ... Read More

Advertisements