While developing Java Swing applications, you may have cases when you need to modify the space around JButtons to develop attractive applications. In this article, we will learn to set the margin of a JButton in Java. What is a JButton? A JButton is a subclass of AbstractButton, and it can be used for adding platform-independent buttons to a Java Swing application. A Button can generate an ActionListener interface when the button is pressed or clicked, it can also generate the MouseListener and KeyListener interfaces. Syntax The following is the syntax for JButton initialization: JButton button = new JButton("Button"); We ... Read More
When you have to show long text messages, you might find problems with the dialog becoming too big or the text getting cut off. In this article, we will learn to implement a long text of the JOptionPane message dialog in Java. What is a JOptionPane? A JOptionPane is a subclass of the JComponent class, which includes static methods for creating and customizing modal dialog boxes. A JOptionPane class can be used instead of a JDialog class to minimize the complexity of the code. Syntax The following is the syntax for JOptionPane initialization: JOptionPane.showMessageDialog(null, "Tutorials Point"); The JOptionPane displays the ... Read More
In this article, we will learn how to convert a string to a list of characters in Java. Sometimes, when we handle character-based data structures, we need to convert a string into a list of characters. In Java, a string is a sequence of characters and is represented by the String class. A list is a collection of elements and is represented by the List interface. Converting a String to a List of Characters The following are the different approaches to convert a string to a list of characters in Java - Using String.toCharArray() ... Read More
Try-catch, throw, and throws are keywords that are used for handling exceptions in Java. Exceptions are the problems that occur during the execution of a program. When a problem occurs, the program you are trying to run will terminate abnormally. So, to handle these exceptions, Java has a mechanism called exception handling. We handle different types of exceptions, such as runtime exceptions, compile-time exceptions, using try-catch, throw, and throws keywords. Types of Exceptions There are two types of exceptions in Java: Checked Exceptions: These are the exceptions that are checked at compile time. For example, ClassNotFountException, ... Read More
A Jackson API is a Java-based library, and it can be useful to convert Java objects to JSON and JSON to Java objects. A Jackson API is faster than other API, needs less memory, and is good for large objects. We can process a JSON in three different ways: Streaming API, Tree Model, and Data Binding. We can pretty print JSON using the writerWithDefaultPrettyPrinter() of the ObjectMapper class, which is a factory method for constructing an ObjectWriter that will serialize objects using the default pretty printer for indentation. In this article, we will learn how to pretty print JSON using the Jackson ... Read More
JSON is a format that is used to exchange data between a server and a client. It is lightweight and easy to read and write. In Java, we can convert JSON to/from Map using various libraries. In this article, we will discuss how to convert JSON to/from Map using the Jackson library. The Jackson is a library for Java, and it has very powerful data binding capabilities and provides a framework to serialize custom java objects to JSON and deserialize JSON back to Java object. We can convert JSON to/from Map using readValue() and writeValueAsString() methods of com.fasterxml.jackson.databind.ObjectMapper class. To ... Read More
Gson is a JSON for Java, which is developed by Google. We can format a date using the Gson library in Java. The Gson library provides a GsonBuilder class that allows us to create a Gson instance with custom settings. We call the create() method of the GsonBuilder class to create an instance of the Gson class. Then we use the method setDateFormat() to configure Gson to serialize Date objects according to the pattern provided. In order to use the Gson library, we need to add it to our project. If you are using Maven, add this to your ... Read More
In Java, an IllegalArgumentException is thrown in order to indicate that a method has been passed an illegal argument. An illegal argument is one that does not meet the required input from user. This exception extends the RuntimeException class and thus, belongs to those exceptions that can be thrown during the operation of the Java Virtual Machine (JVM). It is an unchecked exception and thus, it does not need to be declared in a method's or a constructor's throws clause. Reasons for java.lang.IllegalArgumentException Some of the reasons for IllegalArgumentException in Java is as follows: ... Read More
In Python, scoping rules define where you can access or modify a variable in your code. It is important to understand these rules because using a variable in the wrong place can cause errors or unexpected results. Python follows a specific order to find a variable, called the LEGB rule. This rule looks for variables in the following order - Local: First inside the function. Enclosing: Then, in any enclosing function. Global: Then, in the global scope. Built-in: Finally, in Python's built-in names. What is the LEGB Rule? The LEGB rule tells Python how to search for variable ... Read More
Pretty Printing JSON using "org.json" Library Pretty printing something means formatting it in a way that is easy to read, understand, and also looks good. Here we will format JSON data in a good-looking as well as readable way. We will be using the org.json library to pretty print JSON data in Java. The method toString(int indentFactor) of the JSONObject class is used to pretty print JSON data. The indentFactor parameter specifies the number of spaces to add for each level of indentation. To use the org.json library, we need to add it to our project. If you are using ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP