Found 9150 Articles for Object Oriented Programming

Disallow resizing a column by dragging between headers in a JTable Component with Java

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

172 Views

Let us first create a table with rows and columns −String data[][] = { {"Australia", "5", "1"},    {"US", "10", "2"},    {"Canada", "9", "3"},    {"India", "7", "4"},    {"Poland", "2", "5"},    {"SriLanka", "5", "6"} }; String col [] = {"Team", "Selected Players", "Rank"}; DefaultTableModel tableModel = new DefaultTableModel(data, col); JTable table = new JTable(tableModel);Now, we will disable resizing of columns by dragging headers −table.getTableHeader().setResizingAllowed(false);The following is an example to disallow resizing a column by dragging between headers in a JTable: −Examplepackage my; import java.awt.Dimension; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JRootPane; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public ... Read More

Explain in detail about memory leaks in JavaScript?

vineeth.mariserla
Updated on 04-Jul-2020 14:46:34

577 Views

Memory leaks in JavaScriptJavaScript is called garbage collected language,  that is when variables are declared, it will automatically allocate memory to them. When there are no more references for the declared variables, allocated memory will be released. Memory leaks or most of the memory related problems will occur while releasing the memory. Some common JavaScript leaks 1) Accidental global variablesWhen a undeclared variable is referenced, javascript creates a new variable in the global object. In the following Example-1 let's say the purpose of languages is to only reference a variable in the "myArray" function. If we don't use var to declare it ... Read More

How to allow only a single tree node to be selected in a JTree with Java?

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

584 Views

Set the selection mode to SINGLE_TREE_SELECTION, if you want only a single tree node to be selected −tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);The following is an example to allow only a single tree node to be selected in a JTree −Examplepackage my; import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreeSelectionModel; 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");       DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Electronics");       DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("Home Decor");   ... Read More

Explain in detail about Reference-counting garbage collection in JavaScript?

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

3K+ Views

 Reference-counting garbage collectionThis is the simplest garbage collection algorithm.This algorithm looks out for those objects which have no references   left.An object becomes eligible for garbage collection if it has no references attached to it.The garbage collection is   explained in the below example.Examplevar obj = {       x: { y: 2 }          };          // 2 objects created. One is referenced by the other as one of its properties.          // Obviously, none can be garbage-collected obj = 1; // what was the 'x' property of the ... Read More

What are the differences between printStackTrace() method and getMessage() method in Java?

Shriansh Kumar
Updated on 19-May-2025 19:36:14

5K+ Views

The errors that occur during runtime after compilation are called exceptions. To handle and rectify these errors, it is necessary to understand the cause and the point where the error occurs. There are two ways to find the details of the exception: one is the printStackTrace() method, and another is the getMessage() method. Both are used for exception handling in Java, but they are different from each other. Let's discuss how. The printStackTrace() Method The printStackTrace() method is defined in Throwable class of java.lang package and it is inherited into both java.lang.Error class and java.lang.Exception class. When you use it ... Read More

What are the differences between StackOverflowError and OutOfMemoryError in Java?

Shriansh Kumar
Updated on 21-May-2025 10:46:45

1K+ Views

A Java program may contain an interface, a variable, a method, a class, and an object. When we execute the program, the operating system allocates some memory to Java Virtual Machine. Then, JVM divides allocated memory into two parts, which are the Heap and the Stack. The values of variables, methods, and classes are stored inside the Heap. And, the reference variables, method names, and classes are stored in the stack. When the Stack becomes full, the JVM throws a StackOverflowError, and when the heap becomes full, it throws an OutOfMemoryError. StackOverflowError in Java A stack is used for the execution of methods. It ... Read More

Can we have a return statement in the catch or, finally blocks in Java?

raja
Updated on 17-Nov-2023 16:33:50

15K+ Views

Yes, we can write a return statement of the method in catch and finally block. There is a situation where a method will have a return type and we can return some value at any part of the method based on the conditions. If we return a value in the catch block and we can return a value at the end of the method, the code will execute successfully. If we return a value in the catch block and we can write a statement at the end of the method after return a value, the code will not execute so it became unreachable ... Read More

How to arrange components in a Flow to be right-justified in Java?

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

118 Views

Use FlowLayout.RIGHT to arrange components in a FlowLayout to be right-justified. −JFrame frame = new JFrame("Language"); frame.setLayout(new FlowLayout(FlowLayout.RIGHT));The following is an example to arrange components in a flow to be right-justified −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("Language");       frame.setLayout(new FlowLayout(FlowLayout.RIGHT));       JLabel label = new JLabel("Most Spoken Language ");       label.setPreferredSize(new Dimension(220, 70));       label.setOpaque(true);       label.setBackground(Color.RED);     ... Read More

Java program to center a JLabel in a JPanel with LayoutManager

Smita Kapse
Updated on 06-Aug-2024 22:40:41

4K+ Views

In this article, we will create a graphical user interface (GUI) using the Swing. The Swing is the GUI framework for Java-based applications Here, we are using the LayoutManager GridBagLayout to center the components of AWT Layouts. We have two components here including a label and we have set the layout as GridBagLayout − JLabel label = new JLabel("Name (Centered Label): "); JTextArea text = new JTextArea(); text.setText("Add name here..."); panel.setLayout(new GridBagLayout()); Steps to center a JLabel in a JPanel with LayoutManager The following is an example to center a JLabel in a JPanel with LayoutManager − ... Read More

Java Program to display 5 different cards in a CardLayout

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

798 Views

Use CardLayout layout and set it to panel −JPanel panel = new JPanel(); CardLayout cardLayout = new CardLayout(); panel.setLayout(cardLayout);In the same way create 5 panels and 5 buttons to display 5 different cards.The following is an example to display 5 different cards in CardLayout −Examplepackage my; import java.awt.BorderLayout; import java.awt.CardLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       frame.setSize(550, 300);       JPanel panel = new JPanel();       JPanel panel1 = new JPanel();     ... Read More

Advertisements