Found 9150 Articles for Object Oriented Programming

How can we remove a selected row from a JTable in Java?

raja
Updated on 02-Jul-2020 13:12:48

8K+ Views

A JTable is a subclass of JComponent class for displaying complex data structures. A JTable component can follow the Model View Controller (MVC) design pattern for displaying the data in rows and columns. A JTable can generate TableModelListener, TableColumnModelListener, ListSelectionListener, CellEditorListener and RowSorterListener interfaces. We can remove a selected row from a JTable using the removeRow() method of the DefaultTableModel class.Syntaxpublic void removeRow(int row)Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class RemoveSelectedRowTest extends JFrame {    private JTable table;    private DefaultTableModel model;    private Object[][] data;    private String[] columnNames;    private JButton button;    public RemoveSelectedRowTest() {       setTitle("RemoveSelectedRow ... Read More

Importance of @Override annotation in Java?

Vivek Verma
Updated on 18-Jun-2025 18:25:31

10K+ Views

What is an Annotation? An annotation is like metadata that provides additional information about the code. It does not affect the code directly but provides the information so that the compiler or runtime environment can use it while executing the code. To define or use any annotation in Java, you need to specify the annotation name starting with the "@" symbol. Here is the syntax to use annotations in Java: @annotation_name Where the @ symbol specifies the annotation, and annotation_name is the name of the annotation. Following is a list of a few important annotations and their usage: ... Read More

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

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

709 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

6K+ 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?

Vivek Verma
Updated on 26-May-2025 19:06:03

3K+ Views

Yes! we can override the start() method of the Thread class in Java. But, if we do so, we must call it using super.start(), which will create a new thread; otherwise, no new thread will be created, and the run() method will not execute in parallel for the other threads. Overriding the start() Method In Java, the start() method is used to begin (start) the execution of a new Thread. When this method is called, it invokes the run() method, which executes in parallel with other threads.  Example In the following example, we override the start() method within the subclass of ... Read More

Regular functions vs Arrow functions in JavaScript?

vineeth.mariserla
Updated on 26-Aug-2019 15:49:36

568 Views

Regular functions vs Arrow functionsAn arrow function is used to write the code concisely. Both functions regular and arrow work in a similar manner but there are some differences between them. Let's discuss those differences in a nutshell.syntax of an arrow functionlet x = (params) => { // code };syntax of a regular functionlet x = function functionname(params){ // code };Usage of "this" keywordIt is unable to use "this" keyword in arrow functions whereas in regular functions it can be used without any disturbance.ExampleIn the following example, both regular(rectangle) and arrow(square) functions were used inside the object "num", which consists of len(length) ... Read More

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

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

554 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

437 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?

Vivek Verma
Updated on 28-Aug-2025 16:19:04

2K+ Views

No, a constructor cannot be synchronized in Java. If you try to use the synchronized keyword before a constructor, it may throw a compile-time error in the IDE. Synchronization is a mechanism used to control the access of multiple threads to any shared resource. When multiple threads access any shared resources, the synchronization prevents data corruption by coordinating access, avoiding race conditions (i.e., occurs when multiple threads concurrently access and modify shared data), and ensuring that operations are performed atomically. The JVM ensures that only one thread can invoke a constructor call at a given point in time. That is ... Read More

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

Vivek Verma
Updated on 23-Jun-2025 11:05:39

16K+ Views

The threads can communicate with each other through wait(), notify(), and notifyAll() methods in Java. These are the 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. These methods will throw an IllegalMonitorStateException if the current thread is not the owner of the object's monitor. The wait() Method In Java, the wait() method of the Object class allows threads to pause their execution and wait for a specific condition to be ... Read More

Advertisements