Found 2616 Articles for Java

How can we add different font style items to JList in Java?

raja
Updated on 12-Feb-2020 06:54:41

555 Views

A JList is a subclass of JComponent class and it can be used to display a list of objects that allows the user to select one or more items. A JList can generate a ListSelectiionListener interface and need to implement the abstract method valueChanged(). A DefaultListModel class provides a simple implementation of a list model, which can be used to manage items displayed by a JList control. We can add the items to a JList using the addElement() method of the DefaultListModel class, we can also add items with different fonts to JList using HTML tags like for bold style text,   for italic style ... Read More

Object level lock vs Class level lock in Java?

raja
Updated on 28-Nov-2023 09:43:01

5K+ Views

Both Object level lock and Class level lock are used to achieve synchronization mechanisms in a multi-threaded application. Object Level Lock Every object in Java has a unique lock. If a thread wants to execute a synchronized method on a given object, first it has to get a lock of that object. Once thread got the lock then it is allowed to execute any synchronized method on that object. Once method execution completes automatically thread releases the lock. Acquiring and release lock internally is taken care of by the JVM. Object level lock is a mechanism when we want to synchronize a non-static ... Read More

Can we override a start() method in Java?

raja
Updated on 28-Nov-2023 09:51:48

2K+ Views

Yes, we can override the start() method of a Thread class in Java. We must call the super.start() method to create a new thread and need to call run() method in that newly created thread. If we call the run() method directly from within our start() method, it can be executed in the actual thread as a normal method, not in a new thread. Example public class ThreadTest { public static void main(String[] args) { MyThread t = new MyThread(); t.start(); } } class MyThread extends Thread { ... Read More

Can we call the wait() method without acquiring the lock in Java?

raja
Updated on 27-Nov-2023 10:49:02

438 Views

No, we cannot call the wait() method without acquiring the lock. In Java, once the lock has been acquired then we need to call wait() method (with timeout or without timeout) on that object. If we are trying to call the wait() method without acquiring a lock, it can throw java.lang.IllegalMonitorStateException. Example public class ThreadStateTest extends Thread { public void run() { try { wait(1000); } catch(InterruptedException ie) { ie.printStackTrace(); ... Read More

How to restrict the number of digits inside JPasswordField in Java?

raja
Updated on 12-Feb-2020 07:02:45

313 Views

A JPasswordField is a subclass of JTextField and each character entered in a JPasswordField can be replaced by an echo character. This allows confidential input for passwords. The important methods of JPasswordField are getPassword(), getText(), getAccessibleContext() and etc. By default, we can enter any number of digits inside JPasswordField. If we want to restrict the digits entered by a user by implementing a DocumentFilter class and need to override the replace() method.Syntaxpublic void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationExceptionExampleimport java.awt.*; import java.awt.*; import javax.swing.*; import javax.swing.text.*; public class JPasswordFieldDigitLimitTest extends JFrame {    private JPasswordField passwordField;    private JPanel ... Read More

Can a constructor be synchronized in Java?

raja
Updated on 02-Jul-2020 13:00:23

2K+ Views

No, a constructor cannot be synchronized in Java. The JVM ensures that only one thread can invoke a constructor call at a given point in time. That is why no need to declare a constructor as synchronized and it is illegal in Java. However, we can use synchronized blocks inside a constructor.If we are trying to put a synchronized keyword before a constructor, the compiler says that "error: modifier synchronized not allowed here".Examplepublic class SynchronizedConstructorTest {       // declaration of synchronized constructor       public synchronized SynchronizedConstructorTest() {          System.out.println("Synchronized Constructor");       }       public ... Read More

Importance of wait(), notify() and notifyAll() methods in Java?

raja
Updated on 27-Nov-2023 11:04:59

14K+ Views

The threads can communicate with each other through wait(), notify() and notifyAll() methods in Java. These are final methods defined in the Object class and can be called only from within a synchronized context. The wait() method causes the current thread to wait until another thread invokes the notify() or notifyAll() methods for that object. The notify() method wakes up a single thread that is waiting on that object’s monitor. The notifyAll() method wakes up all threads that are waiting on that object’s monitor. A thread waits on an object’s monitor by calling one of the wait() method. These methods can ... Read More

How to get the string representation of numbers using toString() in Java?

raja
Updated on 27-Nov-2023 11:17:23

568 Views

The toString() method is an important method of Object class and it can be used to return the string or textual representation of an object. The object class’s toString() method returns a string as the name of the specified object’s class which is followed by ‘@’ sign and the hashcode of the object (java.lang.String;@36f72f09) We can use the toString() method to get the string representation of numbers as well and it can be useful if the string consists of numbers taken from different variables. In that case, the number can be converted to a string and concatenate to create a combined ... Read More

How can we implement auto-complete JComboBox in Java?

raja
Updated on 12-Feb-2020 06:34:12

1K+ Views

A JComboBox is a subclass of 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 ItemListener interfaces when the user actions on a combo box.We can implement auto-complete JComboBox when the user types an input value from a keyboard by using customization of a combo box (AutoCompleteComboBox) by extending the JComboBox class.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.plaf.basic.*; public class AutoCompleteComboBoxTest extends JFrame {    private JComboBox comboBox;    public AutoCompleteComboBoxTest() {       setTitle("AutoCompleteComboBox");     ... Read More

What will happen if we directly call the run() method in Java?

raja
Updated on 27-Nov-2023 15:43:48

157 Views

A direct call of a Thread object's run() method does not start a separate thread and it can be executed within the current thread. To execute Runnable.run from within a separate thread, do one of the following. Construct a thread using the Runnable object and call start() method on the Thread. Define a subclass of a Thread object and override the definition of its run() method. Then construct an instance of this subclass and call start() method on that instance directly. Example public class ThreadRunMethodTest { public static void main(String args[]) { MyThread runnable = new MyThread(); ... Read More

Advertisements