Found 2616 Articles for Java

How can we add padding to a JTextField in Java?

raja
Updated on 03-Jul-2020 11:49:59

2K+ Views

A JTextField is a subclass of JTextComponent class and it is one of the most important components that allow the user to input text value in a single-line format. A JTextField class will generate an ActionListener interface when we trying to enter some input inside it. The important methods of a JTextField class are setText(), getText(), setBorder(), setEnabled(),  etc. We can add padding to a JTextField using the setMargin(Insets s) of JTextComponent class.Syntaxpublic void setMargin(Insets m)Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class JTextfieldPaddingTest extends JFrame {    private JTextField jtf;    public JTextfieldPaddingTest() {       jtf = new JTextField("Welcome to Tutorials Point"); ... Read More

How to serialize and deserialize an object in Java?

raja
Updated on 01-Dec-2023 10:38:28

1K+ Views

The Serialization is a process of changing the state of an object into a byte stream, an object is said to be serializable if its class or parent classes implement either the Serializable or Externalizable interface and the Deserialization is a process of converting the serialized object back into a copy of an object. During serialization, if we don’t want to write the state of a particular variable in a byte stream using a transient keyword. When the JVM comes up to the transient keyword, it ignores the original state of a variable and stores a default value of that data type ... Read More

How can we stop a thread in Java?

raja
Updated on 01-Dec-2023 09:32:48

23K+ Views

Whenever we want to stop a thread from running state by calling stop() method of Thread class in Java. This method stops the execution of a running thread and removes it from the waiting threads pool and garbage collected. A thread will also move to the dead state automatically when it reaches the end of its method. The stop() method is deprecated in Java due to thread-safety issues. Syntax @Deprecated public final void stop() Example import static java.lang.Thread.currentThread; public class ThreadStopTest { public static void main(String args[]) throws InterruptedException { UserThread userThread = new UserThread(); ... Read More

How to set the color to alternate rows of JTable in Java?

raja
Updated on 03-Jul-2020 11:48:46

2K+ Views

A JTable is a subclass of JComponent class and it can be used to create a table with information displayed in multiple rows and columns. When a value is selected from a JTable, a TableModelEvent is generated, which is handled by implementing a TableModelListener interface.We can set the color to alternate rows of JTable by overriding the prepareRenderer() method of JTable class.Syntaxpublic Component prepareRenderer(TableCellRenderer renderer, int row, int column)Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class AlternateRowColorTableTest extends JFrame {    public AlternateRowColorTableTest() {       setTitle("AlternateRowColorTable Test");       JTable table = new JTable(new Object[][] {{"115", "Ramesh"}, {"120", "Adithya"}, {"125", "Jai"}, {"130", "Chaitanya"}, ... Read More

How can we implement a Custom HashSet in Java?

raja
Updated on 01-Dec-2023 10:02:50

2K+ Views

A HashSet implements Set interface which does not allow duplicate values. A HashSet is not synchronized and is not thread-safe. When we can add any duplicate element to a HashSet, the add() method returns false and does not allow to add a duplicate element to HashSet. Syntax public class HashSet extends AbstractSet implements Set, Cloneable, Serializable In the below example, we can implement a Custom HashSet. Example import java.util.*; public class CustomHashSetTest extends AbstractSet { private HashMap map = null; private static final Object tempObject = new Object(); public CustomHashSetTest() { map = new HashMap(); ... Read More

Importance of a Locale class in Java?

raja
Updated on 01-Dec-2023 10:43:28

175 Views

A Locale class is used to perform locale operations and supply locale information to the user. A Locale is defined as a set of parameters that represents a geographical location or place where some operation occurs. The important methods of Locale class are getAvailableLocales(), getCountry(), getDefault(), getDisplayLanguage(), getDisplayCountry(), getUnicodeLocaleKeys() etc. The Locale class uses the following constructors − Locale(String L)− Initializes locale from the language code passed as an argument. Locale(String L, String C) − Initializes locale from the language, country code passed as arguments. Locale(String L, String C, String V) − Initializes locale from the language, country, variant passed as arguments. Example import java.text.SimpleDateFormat; import java.util.Locale; public ... Read More

How can we get the name of the Enum constant in Java?

raja
Updated on 29-Nov-2023 10:25:32

5K+ Views

An Enum is a special datatype which is added in Java 1.5 version and it can be used to define a collection of constants, when we need a predefined list of values that do not represent some kind of numeric or textual data, we can use an Enum. The enums are constants and by default, they are static and final, so the names of an enum type fields are in uppercase letters. The name of the enum constant is returned by the method java.lang.Enum.name(). This method returns the name exactly as it was declared in the enum declaration. Example enum Shape { CIRCLE, TRIANGLE, ... Read More

Which collection classes are thread-safe in Java?

raja
Updated on 29-Nov-2023 10:31:28

11K+ Views

A thread-safe class is a class that guarantees the internal state of the class as well as returned values from methods, are correct while invoked concurrently from multiple threads. The collection classes that are thread-safe in Java are Stack, Vector, Properties, Hashtable, etc. Stack The Stack class in Java implements the stack data structure that is based on the principle of LIFO. So, the Stack class can support many operations such as push, pop, peek, search, empty, etc. Example import java.util.*; public class StackTest { public static void main (String[] args) { Stack stack = new Stack(); ... Read More

When can a .class file get created in Java?

raja
Updated on 03-Jul-2020 08:24:35

1K+ Views

A Java class file has a ".class" extension and contains the Java bytecode. This class file can be executed by the Java Virtual Machine (JVM). A  ".class" file is created as a result of successful compilation by the Java compiler from the ".java" file. Each class in the .java file is compiled into a separate class file if the ".java " file has more than one class.Exampleclass A {    A() {       System.out.println("This is class A");    } } class B {    B() {       System.out.println("This is class B");    } } class C { ... Read More

How can we check an underflow occurs in Java?

raja
Updated on 03-Jul-2020 08:19:34

282 Views

When a value is assigned to a variable that is less than the minimum allowed value for that variable, then an underflow occurs. There is no exception thrown by the JVM if an underflow occurs in Java and it is the responsibility of a programmer to handle the underflow conditions.Examplepublic class UnderlowTest {    public static void main(String[] args) {       int num1 = -2147483648;       int num2 = -1;       System.out.println("Number 1: " + num1);       System.out.println("Number 2: " + num2);       long sum = (long)num1 + (long)num2;     ... Read More

Advertisements