In Java, the exceptions are of two types: checked and unchecked exceptions. A checked exception is an exception that occurs at compile time; these are also called compile-time exceptions. An unchecked exception occurs at the time of execution. These are also called Runtime Exceptions. In this article, we will learn to create a custom unchecked exception in Java. We can create a custom unchecked exception by extending the RuntimeException class in Java. What are unchecked exceptions? The unchecked exceptions inherit from the Error class or the RuntimeException class. Many programmers feel that we cannot handle these exceptions in our programs because they represent the type of errors ... Read More
In this article, we will learn to implement a JLabel text with different colors and fonts in Java. JLabel is commonly used for simple text display, it can be enhanced to show text with multiple colors and fonts. JLabel A JLabel class can extend the JComponent class, and an object of JLabel provides text instructions or information on a GUI. A JLabel can display a single line of read-only text, an image, or both text and an image. A JLabel can explicitly generate a PropertyChangeListener interface. Different Approaches The following are the two different approaches to implementing a JLabel text ... Read More
In this article, we will learn to change each column width of a JTable in Java. JTable is Swing's strongest component for displaying and editing tabular information. Adjusting column widths appropriately to display content is among the most prevalent needs for customization. JTable A JTable is a subclass of JComponent for displaying complex data structures. A JTable can follow the Model View Controller (MVC) design pattern for displaying the data in rows and columns. The DefaultTableModel class can extend AbstractTableModel and it can be used to add the rows and columns to a JTable dynamically. Syntax The following is ... Read More
The Font class in Java is responsible for setting screen fonts, which are mapped to characters of the language in specific regions. But a FontMetrics class is said to be a font metrics object that encapsulates the data about the rendering of a specific font on a particular screen. Font A Font class can be used to create an instance of a Font object to set the font for drawing text, labels, text fields, buttons, etc, and it can be specified by its name, style, and size. Syntax The following is the syntax for Font initialization: Font font = new ... Read More
In this article, we will learn to center-align the items of a JComboBox in Java. The default rendering of a JComboBox uses a JLabel for each item. JLabel's default alignment is left-justified, hence the misalignment. JComboBox A JComboBox is a subclass of the JComponent class, and it is a combination of a text field and a drop-down list from which the user can choose a value. A JComboBox can generate an ActionListener, ChangeListener, and an ItemListener when the user actions with a combo box. Center-Align the Items of a JComboBox By default, items in the JCombobox are left-aligned. We ... Read More
In this article, we will learn to display the line numbers inside a JTextArea in Java. When using text editors or code viewers in Java Swing applications, the showing of line numbers can greatly improve the user experience. What is a JTextArea? A JTextArea is a subclass of JTextComponent and it is a multi-line text component to display the text or allow the user to enter a text. A JTextArea can generate a CaretListener interface, which can listen to caret update events. Syntax The following is the syntax for JTextArea initialization: JTextArea textArea = new JTextArea(); By default, JTextArea does ... Read More
In this article, we will learn to draw a rounded rectangle using the Graphics object in Java. Drawing shapes is a basic feature of Java 2D graphics programming. Though the Graphics class offers methods for drawing common rectangles, drawing rounded rectangles involves some special techniques. Graphics Class In Java, the drawing takes place via a Graphics object, which is an instance of the java.awt.Graphics class. Each Graphics object has its own coordinate system, and all the methods of Graphics, including those for drawing Strings, lines, rectangles, circles, polygons, etc. We can get access to the Graphics object through the paint(Graphics ... Read More
In Python, when working with files and directories, we may often need to access the parent directory of a given path, especially when going through a file system or managing relative paths in a project. In this article, we are going to explore the different methods available in Python to get the parent directory of the current or any specific path. Using os Module One of the commonly used methods to interact with the file system in Python is using the os module. This module provides various utilities to work with file paths and directories. To get the parent directory, ... Read More
A directory is simply defined as a collection of subdirectories and single files or either one of them. These subdirectories are separated using a "/" operator in a directory hierarchy. A directory hierarchy is constructed by organizing all the files and subdirectories within a main directory and also known as the "root" directory. Calculating the total size of a directory, i.e., all its files and subdirectories which is a common task in Python, especially when dealing with disk usage monitoring, backup management, or cleaning up storage. Python provides multiple ways to accomplish this efficiently using built-in modules such as os ... Read More
In some applications, we have to find the files on our computer programmatically to make use of out time. Python provides several powerful libraries to search and locate files by making this task straightforward and efficient. In this article, we are going to see the different methods to find a file in our system using Python. Using the os Module The os module has a method os.walk(), which is used to search for a file in the directories and their subdirectories. This method returns the file paths in a directory which allows the user to search for a specific file. ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP