Found 2616 Articles for Java

How to display "No records available" text in a JTable in Java?

raja
Updated on 12-Feb-2020 07:15:04

331 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.In the below program, we can display "No records available" text if the rows are not available in a JTable.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class NoRecordTableTest extends JFrame {    private JPanel panel;    private JTable table;    private JScrollPane scrollPane;    public NoRecordTableTest() {       panel = new JPanel();       panel.setLayout(new BorderLayout()); ... Read More

How to print the first character of each word in a String in Java?

raja
Updated on 29-Nov-2023 10:10:45

5K+ Views

A String class can be used to represent the character strings, all the string literals in a Java program are implemented as an instance of a String class. The Strings are constants and their values cannot be changed (immutable) once created. We can print the first character of each word in a string by using the below program. Example public class FirstCharacterPrintTest { public static void main(String[] args) { String str = "Welcome To Tutorials Point"; char c[] = str.toCharArray(); System.out.println("The first character of each word: ... Read More

What are the uses of generic collections in Java?

raja
Updated on 03-Jul-2020 05:57:29

6K+ Views

The generic collections are introduced in Java 5 Version. The generic collections disable the type-casting and there is no use of type-casting when it is used in generics. The generic collections are type-safe and checked at compile-time. These generic collections allow the datatypes to pass as parameters to classes. The Compiler is responsible for checking the compatibility of the types.Syntaxclass, interfaceType safetyGenerics allows a single type of object.List list = new ArrayList(); // before generics list.add(10); list.add("100"); List list1 = new ArrayList(); // adding generics list1.add(10); list1.add("100"); // compile-time error.Type CastingNo need for type-casting while using generics.List list = new ArrayList(); list.add("Adithya"); String str = list.get(0); // ... Read More

How can we disable the leaf of JTree in Java?

raja
Updated on 03-Jul-2020 05:46:15

261 Views

A JTree is a component that presents a hierarchical view of data. The user has the ability to expand or collapse individual sub-trees. A TreeNode interface defines the methods that must be implemented nodes of a JTree object. The DefaulMutableTreeNode class provides a default implementation of a TreeNode interface. We can disable the leaf of JTree by overriding the getTreeCellRendererComponent() method of DefaultTreeCellRenderer class.Syntaxpublic Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus)Exampleimport java.awt.*; import javax.swing.tree.*; import javax.swing.*; public class JTreeLeafNodeDisableTest extends JFrame {    private TreeNode treeNode;    private JTree tree;    public JTreeLeafNodeDisableTest() {       setTitle("JTreeLeafNodeDisable Test");     ... Read More

How to print the elements of a HashMap in Java?

raja
Updated on 27-Nov-2023 15:49:03

2K+ Views

A HashMap is a subclass of AbstractMap class and it is used to store key & value pairs. Each key is mapped to a single value in the map and the keys are unique. It means we can insert a key only once in a map and duplicate keys are not allowed, but the value can be mapped to multiple keys. We can add the elements using the put() method of HashMap class and iterate the elements using the Iterator interface. Syntax public V put(K key, V value) Example import java.util.*; import java.util.Map.*; public class HashMapTest { public static void main(String[] args) { ... Read More

Differences between Collection and Collections in Java?

raja
Updated on 27-Nov-2023 15:57:27

7K+ Views

The Collection is an interface whereas Collections is a utility class in Java. The Set, List, and Queue are some of the subinterfaces of Collection interface, a Map interface is also part of the Collections Framework, but it doesn't inherit Collection interface. The important methods of Collection interface are add(), remove(), size(), clear() etc and Collections class contains only static methods like sort(), min(), max(), fill(), copy(), reverse() etc. Syntax for Collection Interface public interface Collection extends Iterable Syntax for Collections Class public class Collections extends Object Example import java.util.*; public class CollectionTest { public static void main(String args[]) { ArrayList ... Read More

How to implement the mouse right-click on each node of JTree in Java?

raja
Updated on 12-Feb-2020 07:26:46

605 Views

A JTree is a subclass of JComponent class that can be used to display the data with the hierarchical properties by adding nodes to nodes and keeps the concept of parent and child node. Each element in the tree becomes a node. The nodes are expandable and collapsible. We can implement the mouse right-click on each node of a JTree using the mouseReleased() method of MouseAdapter class and need to call show() method of JPopupMenu class to show the popup menu on the tree node.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.tree.*; public class JTreeRightClickTest extends JFrame {    public JTreeRightClickTest() {       ... Read More

Can we synchronize a run() method in Java?

raja
Updated on 28-Nov-2023 09:22:45

2K+ Views

Yes, we can synchronize a run() method in Java, but it is not required because this method has been executed by a single thread only. Hence synchronization is not needed for the run() method. It is good practice to synchronize a non-static method of other class because it is invoked by multiple threads at the same time. Example public class SynchronizeRunMethodTest implements Runnable { public synchronized void run() { System.out.println(Thread.currentThread().getName() + " is starting"); for(int i=0; i < 5; i++) { try ... Read More

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

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

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

raja
Updated on 28-Nov-2023 09:33:44

9K+ Views

The @Override annotation is one of a default Java annotation and it can be introduced in Java 1.5 Version. The @Override annotation indicates that the child class method is over-writing its base class method. The @Override annotation can be useful for two reasons. It extracts a warning from the compiler if the annotated method doesn't actually override anything. It can improve the readability of the source code. Syntax public @interface Override Example class BaseClass { public void display() { System.out.println("In the base class, test() method"); } } class ChildClass extends BaseClass { @Override public void ... Read More

Advertisements