Found 7442 Articles for Java

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

121 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

805 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

Java Program to set the content of the JLabel to be right-justified and bottom-aligned

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

122 Views

To set the text of the label component to be right-justified and bottom-aligned, you need to set the alignment. Set the label to be on the right and bottom aligned −JLabel label = new JLabel("Total Runs", JLabel.RIGHT); 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 lable to be right-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 ... Read More

Java program to set the height of only a single row in a JTable with multiple rows

Smita Kapse
Updated on 29-Oct-2024 00:38:55

840 Views

In this article, we create a JTable in Java Swing to display information about different programming technologies and their tutorial availability. The table is set up with specific columns and rows, each containing data on a particular technology. We also demonstrate how to set a custom height for a single row in the table. Specifically, the height of the 4th row is adjusted to 30 pixels to show how you can control row height individually in a JTable. The table is then displayed within a scrollable JFrame window. table.setRowHeight(3, 30); The above sets row height to 30 pixels for row index ... Read More

Java Program to append a row to a JTable in Java Swing

Alshifa Hasnain
Updated on 17-Jan-2025 19:44:35

1K+ Views

Adding rows dynamically to a JTable is a common task in Java Swing when working with user interfaces that involve tabular data. This article walks you through creating a Java Swing application that appends a new row to a JTable when the user inputs data and clicks a button. What is JTable? A JTable is a powerful Swing component for displaying and editing tabular data. By using a DefaultTableModel, we can manage the table's data dynamically, including adding or removing rows. This makes it an excellent choice for applications requiring user interaction. Approach Following are the steps to append a row ... Read More

How to add a title to JTable in Java Swing?

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

2K+ Views

To display a title to the JTable, you can set a title for the JPanel, which already consists of a JTable.Here, we are using createTitledBorder() for the JPanel to set the title for the panel border that would eventually work for table title.Let’s say the following is the JPanel −JPanel panel = new JPanel();Now, use setBorder() and the BorderFactory class to set a title border for the panel that would be our table title as well −panel.setBorder(BorderFactory.createTitledBorder(    BorderFactory.createEtchedBorder(), "My Demo Table", TitledBorder.LEFT, TitledBorder.TOP));The following is an example to add a title to a JTable −Examplepackage my; import javax.swing.BorderFactory; import ... Read More

Advertisements