Programming Articles - Page 2589 of 3363

Java program to set an icon for JOptionPane

Anvi Jain
Updated on 20-Sep-2024 21:38:14

2K+ Views

In this program, we will learn how to set a custom icon in a JOptionPane using Java's Swing framework. A JOptionPane allows displaying dialog boxes for different purposes such as messages, inputs, or confirmations. In this example, we will create a dialog box with a custom image icon displayed alongside some text. This can be particularly useful for creating visually appealing user interfaces. Problem Statement Write a Java program to set an icon for JOptionPane. Below is the demonstration of the same − Output Steps to set an icon for JOptionPane Following are the steps to set an ... Read More

How to set Line Border color and width with Java?

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

5K+ Views

To set Line Border color and width, use the LineBorder. At first, set a panel wherein we need to set the line border −JPanel panel = new JPanel();Now, create a border and set on the panel created above −Border border = new LineBorder(Color.ORANGE, 4, true); panel.setBorder(border);The following is an example to set LineBorder color and width −package my; import java.awt.Color; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.border.Border; import javax.swing.border.LineBorder; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       JPanel panel = new JPanel();   ... Read More

How to add components with a relative Y position in Java?

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

171 Views

To add components with a relative Y position, use the GridBagConstraints.RELATIVE constant. Set this to gridy field −GridBagConstraints constraints = new GridBagConstraints(); constraints.gridy = GridBagConstraints.RELATIVE;The following is an example to add components with a relative Y position in Java −Examplepackage my; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; 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);       JPanel panel = new JPanel();       panel.setLayout(new GridBagLayout());       GridBagConstraints constraints = new GridBagConstraints();       constraints.gridx = ... Read More

How to create Vertical progress bar occupying the entire frame in Java

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

299 Views

For this, create a vertical progress bar −JProgressBar progressBar = new JProgressBar(JProgressBar.VERTICAL); progressBar.setEnabled(true);Also, set the bounds −progressBar.setBounds(70, 50, 120, 30);The following is an example to create vertical progress bar occupying the entire frame −Examplepackage my; import java.awt.BorderLayout; import java.awt.Color; import javax.swing.JFrame; import javax.swing.JProgressBar; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JProgressBar progressBar = new JProgressBar(JProgressBar.VERTICAL);       progressBar.setEnabled(true);       progressBar.setBounds(70, 50, 120, 30);       progressBar.setBackground(Color.orange);       progressBar.setForeground(Color.white);       progressBar.setStringPainted(true);       ... Read More

How to customize how a JTabbedPane looks in Java?

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

943 Views

To customize how a JTabbedPane looks, change its font style, font face, font size and the background and foreground colors.Let’s say the following is the JTabbedPane −JTabbedPane tabbedPane = new JTabbedPane();Now, let us customize the above created JTabbedPane −tabbedPane.setBackground(Color.orange); tabbedPane.setForeground(Color.white); Font font = new Font("Verdana", Font.CENTER_BASELINE, 18); tabbedPane.setFont(font);The following is an example to customize JTabbedPane −Examplepackage 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, panel5;       ... Read More

How to get the number of siblings of this node in a JTree?

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

205 Views

To get the count of siblings of this node, use the getSiblingCount() method. Let’s say you have a node with 4 child nodes. Find the sibling of any of this node’s child node. Here, “eight” is the child node −eight.getSiblingCount());Note − Remember, a node is it’s own sibling.The following is an example to get the number of siblings of this node 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 - P66779)");       DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("Home Decor (Product3 - ... Read More

What are the differences between while loop and do-while loop in Java?

Shriansh Kumar
Updated on 24-Apr-2025 18:17:00

8K+ Views

In Java, the do-while and while loops are used to repeat a block of code multiple times based on a condition. These loops help us to avoid writing the same code again and again. Both loops are used when we don't know exactly how many times the loop should run. In this article, we will understand the difference between the while loop and the do-while loop. The while Loop The while loop in java executes one or more statements after testing the loop continuation condition at the start of each iteration. Syntax The while loop follows the syntax given ... Read More

How can Forgotten timers or callbacks cause memory leaks in JavaScript?

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

731 Views

Forgotten timers/callbacksThere are two timing events in javascript namely setTimeout() and setInterval(). The former executes a function after waiting a specified number of milliseconds, whereas the latter executes a function periodically(repeats for every certain interval of time).When any object is tied to a timer callback, it will not be released until the timeout happens. In this scenario timer resets itself and runs forever till the completion of timeout there by disallowing garbage collector to remove the memory.These timers are most frequent cause of memory leaks in javascript.ExampleIn the following example, the timer callback and its tied object(tiedObject) will not be ... Read More

What is Double-buffering in Java?

raja
Updated on 30-Jul-2019 22:30:26

2K+ Views

Double-buffering is the process of drawing graphics into an off-screen image buffer and then copying the contents of the buffer to the screen all at once.For the complex graphics, using double-buffering can reduce flickering issues.Java Swing automatically supports double-buffering for all of its components.Double-buffering is memory intensive, its use is only justified for components that are repainted very frequently or have particularly complex graphics to display.If a container uses double-buffering, any double-buffered children it has shared the off-screen buffer of the container, the required off-screen buffer is never larger than the on-screen size of the application.To enable double buffering, simply ... Read More

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

Advertisements