Found 2616 Articles for Java

How can we filter a JTable in Java?

raja
Updated on 12-Feb-2020 07:54:44

3K+ Views

A JTable provides a very flexible possibility to create and display tables. The TableModel interface defines methods for objects that specify the contents of a table. The AbstractTableModel class is typically extended to provide a custom implementation of a model table. A JTable class provides the ability to edit tables using the method setCellEditor() allows an object of the TableCellEditor interface.We can filter a table using the setRowFilter() method of TableRowSorter class.Exampleimport java.awt.*; import java.awt.event.*; import java.util.regex.*; import javax.swing.*; import javax.swing.table.*; public class FilterTableTest extends JFrame {    private JTable table;    private TableModel model;    public FilterTableTest() {       setTitle("FilterTable Test");       ... Read More

Can a "this" keyword be used to refer to static members in Java?

raja
Updated on 29-Nov-2023 10:58:08

559 Views

No, the "this" keyword cannot be used to refer to the static members of a class. This is because the “this” keyword points to the current object of the class and the static member does not need any object to be called. The static member of a class can be accessed directly without creating an object in Java. Example public class StaticTest { static int a = 50; static int b; static void show() { System.out.println("Inside the show() method"); b = a + 5; ... Read More

How to compare two dates in Java?

raja
Updated on 29-Nov-2023 11:05:25

77K+ Views

In Java, two dates can be compared using the compareTo() method of Comparable interface. This method returns '0' if both the dates are equal, it returns a value "greater than 0" if date1 is after date2 and it returns a value "less than 0" if date1 is before date2. Syntax int compareTo(T o) Example import java.text.*; import java.util.Date; public class CompareTwoDatesTest { public static void main(String[] args) throws ParseException { SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd"); Date d1 = sdformat.parse("2019-04-15"); Date d2 = sdformat.parse("2019-08-10"); System.out.println("The ... Read More

How do threads communicate with each other in Java?

raja
Updated on 29-Nov-2023 11:11:33

4K+ Views

Inter-thread communication involves the communication of threads with each other. The three methods that are used to implement inter-thread communication in Java. wait() This method causes the current thread to release the lock. This is done until a specific amount of time has passed or another thread calls the notify() or notifyAll() method for this object. notify() This method wakes a single thread out of multiple threads on the current object’s monitor. The choice of thread is arbitrary. notifyAll() This method wakes up all the threads that are on the current object’s monitor. Example class BankClient { int balAmount = 5000; synchronized void withdrawMoney(int ... Read More

How does a ClassLoader work in Java?

raja
Updated on 03-Jul-2020 07:39:59

813 Views

A Java Class is stored in the form of byte code in a .class file after it is compiled. The ClassLoader loads the class of the Java program into memory when it is required. The ClassLoader is hierarchical and so if there is a request to load a class, it is delegated to the parent class loader.The types of ClassLoader in Java are given as followsBootstrap ClassLoaderExtensions ClassLoaderSystem ClassLoaderExamplepublic class ClassLoaderTest {    public static void main(String[] args) {       System.out.println("class loader for this class: " + ClassLoaderTest.class.getClassLoader());       System.out.println("class loader for DNSNameService: " + sun.net.spi.nameservice.dns.DNSNameService.class.getClassLoader());     ... Read More

Importance of the getCause() method in Java?

raja
Updated on 03-Jul-2020 07:40:44

2K+ Views

The getCause() method is from Throwable class and we can use this method which returns the cause of the exception or returns null if the cause of the exception is not known. The getCause() method doesn’t accept any arguments and doesn’t throw an exception. It returns the cause that was provided by one of its constructors or that was determined by the formation of the initCause() method of Throwable class.Syntaxpublic Throwable getCause()Examplepublic class GetCauseMethodTest {    public static void main(String[] args) throws Exception {       try {          myException();       } catch(Exception e) {          System.out.println("Cause ... Read More

How to generate an UnsupportedOperationException in Java?

raja
Updated on 03-Jul-2020 07:45:55

392 Views

An UnsupportedOperationException is a subclass of RuntimException in Java and it can be thrown to indicate that the requested operation is not supported. The UnsupportedOperationException class is a member of the Java Collections Framework. This exception is thrown by almost all of the concrete collections like List, Queue, Set and Map.Syntaxpublic class UnsupportedOperationException extends RuntimeExceptionExampleimport java.util.*; public class UnsupportedOperationExceptionTest {    public static void main(String[] args) {       List aList = new ArrayList();       aList.add('a');       aList.add('b');       List newList = Collections.unmodifiableList(aList);       newList.add('c');    } }In the above example, it will generate ... Read More

Can we have an empty catch block in Java?

raja
Updated on 03-Jul-2020 07:18:31

5K+ Views

Yes, we can have an empty catch block. But this is a bad practice to implement in Java.Generally, the try block has the code which is capable of producing exceptions, if anything wrong in the try block, for instance,  divide by zero, file not found,  etc. It will generate an exception that is caught by the catch block. The catch block catches and handles the exception. If the catch block is empty then we will have no idea what went wrong within our code.Examplepublic class EmptyCatchBlockTest {    public static void main(String[] args) {       try {          int a ... Read More

How many ways to iterate a TreeSet in Java?

raja
Updated on 29-Nov-2023 10:07:15

817 Views

A Treeset is a subclass of AbstractSet class and implements NavigableSet Interface. By Default, a Treeset gives an ascending order of output and it will use a Comparable interface for sorting the set elements. Inside a Treeset, we can add the same type of elements otherwise it can generate a ClassCastException because by default TreeSet uses a Comparable interface. Syntax public class TreeSet extends AbstractSet implements NavigableSet, Cloneable, Serializable We can iterate a TreeSet in two ways. Using Iterator We can iterate the elements of a TreeSet using Iterator interface. Example import java.util.*; public class IteratingTreeSetTest { public static void main(String[] args) { ... Read More

How to validate if JTable has an empty cell in Java?

raja
Updated on 12-Feb-2020 08:03:55

1K+ Views

A JTable is a subclass of JComponent class for displaying complex data structures. A JTable can follow the Model View Controller (MVC) design pattern for displaying the data in rows and columns. A JTable will generate TableModelListener, TableColumnModelListener, ListSelectionListener, CellEditorListener and RowSorterListener interfaces.We can validate whether the JTable cell is empty or not by implementing the getValueAt() method of JTable class. If we click on the "Click Here" button, it will generate an action event and display a popup message like "Field is Empty" to the user.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class JTableEmptyValidateTest extends JFrame {    private JPanel panel;    private JTable table;   ... Read More

Advertisements