Found 9150 Articles for Object Oriented Programming

How can Detached DOM elements cause memory leak in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

2K+ Views

Detached Dom elementsDetached DOM elements are the elements which have been removed from the DOM but their memory is still retained because of JavaScript. This means that as long the element have a reference to any variable or an object anywhere, it does not garbage collected even after destroyed from the DOM.DOM is like an double-linked tree which means a reference to a node in the tree will halt the entire tree from garbage collection.Let's take an example of creating a DOM element in javascript. After creating the element destroy it but forget to delete the variable holding it. This ... Read More

Why AWT components are heavy-weight while Swing components are light-weight in Java?

raja
Updated on 06-Feb-2020 10:46:30

2K+ Views

AWT stands for Abstract Window ToolKit and it supports Java GUI programming. It is a portable GUI library for Stand-alone Java applications/applets. The AWT provides the connection between our application and the native GUI while Java Swing implements a set of GUI components that build on AWT technology and it can provide a pluggable look and feel. Java Swing is implemented entirely in the Java programming language.First of all, by a heavy-weight, it means the code will take comparatively more time to load and it will consume more System resources. AWT is considered to be heavy-weight because its components are dependent ... Read More

Can I get the node at a specified index in a JTree with Java?

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

457 Views

To get the node at a specified index in a JTree, use the getChildAt() method. Here, we are finding the node at index 3 i.e. 4th node −node.getChildAt(3)The following is an example to get the node at a specified index in a JTree −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");       DefaultMutableTreeNode node = new DefaultMutableTreeNode("Products");       DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("Clothing (Product1 - P66778)");       DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Accessories (Product2 ... Read More

How to get this nodes’s parent in a JTree with Java?

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

856 Views

Let’s say we want the parent of a node, then use the getParent() method -node3.getFirstChild()You can also get the parent of child node. Here, “nine” is the child node −nine.getParent()The output is as follows displaying this node’s parent on Console −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");       DefaultMutableTreeNode node = new DefaultMutableTreeNode("Products");       DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("Clothing (Product1 - P66778)");       DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Accessories (Product2 - P66779)");   ... Read More

How to set the alignment of the JLabel content along the Y axis in Java?

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

547 Views

To set the alignment of the label content along the Y axis, use the setVerticalAlignment() method. Let us first set a label component. We have set the label background color as well so that we can check the alignment of the label’s content properly −JLabel label = new JLabel("Product Name "); label.setPreferredSize(new Dimension(190, 100)); label.setOpaque(true); label.setBackground(Color.ORANGE);Now, we will align the label content along the Y axis −label.setVerticalAlignment(JLabel.CENTER);The following is an example to set the alignment of the JLabel content along the Y axis −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("Our Frame ... Read More

How to set the alignment of the JLabel content along the X axis in Java?

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

793 Views

To set the alignment of the label content along the X axis, use the setHorizontalAlignment() method. Let us first set a label component. We have set the label background color as well so that we can check the alignment of the label’s content properly −JLabel label = new JLabel("Team "); label.setPreferredSize(new Dimension(190, 100)); label.setOpaque(true); label.setBackground(Color.BLUE); label.setForeground(Color.WHITE);Now, we will align the label content along the X axis −label.setHorizontalAlignment(JLabel.CENTER);The following is an example to set the alignment of the JLabel content along the X axis −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 ... Read More

How to create a JLabel with an image icon in Java?

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

6K+ Views

Let us create a label with image icon −JLabel label = new JLabel("SUBJECT "); label.setIcon(new ImageIcon("E:ew.png"));Now, create another component −JTextArea text = new JTextArea(); text.setText("Add subject here...");Align the components with GridBagLayout −panel.setLayout(new GridBagLayout());The following is an example to center a label with an image icon −Examplepackage my; import java.awt.GridBagLayout; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.WindowConstants; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame("Demo Frame");       JPanel panel = new JPanel();       JLabel label = new JLabel("SUBJECT "); ... Read More

Java Program to insert styled text in a JTextPane Component

Smita Kapse
Updated on 23-Dec-2024 18:10:28

796 Views

In this article we will learn to insert styled text into a JTextPane component in Java. The JTextPane component is a versatile text editor that supports rich text features, including the ability to style text with different fonts, colors, and sizes. By leveraging the StyledDocument and Style classes, you can insert styled text dynamically into a JTextPane using Java Swing. Insert styled text in a JTextPane component using the SimpleAttributeSet and StyleConstants classes. With that, we will also use StyledDocument. Java Swing The Swing API is a collection of flexible GUI components designed to simplify the development of Java-based front-end ... Read More

Program to combine BorderLayout, GridLayout and FlowLayout in Java Swing?

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

1K+ Views

Here, we have set panels with BorderLayout, GridLayout and FlowLayout. Within the panels, we have created components such as Button, ComboBox, etc. The following is an example to combine layouts in Java −Examplepackage my; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) {       JButton btnA = new JButton("Button1 (Left)");       JButton btnB = new JButton("Button2 (Right)");       JButton btnC = new JButton("Button3 (Left)");       JButton btnD = new JButton("Button4 (Right)");       ... Read More

How to create titled border for a Panel in Java?

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

545 Views

To create titled border for a panel, use the createTitledBorder() method. Let us create a panel first −JPanel panel = new JPanel(); JButton btn1 = new JButton("One"); JButton btn2 = new JButton("Two"); panel.add(btn1); panel.add(btn2);Now, set titled border with BorderFactory class −panel.setBorder(BorderFactory.createTitledBorder("Title of the border"));The following is an example to create titled border for a panel in Java −Examplepackage my; import java.awt.Component; import java.awt.Dimension; import javax.swing.BorderFactory; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       frame.getContentPane().setLayout(new ... Read More

Advertisements