Programming Articles - Page 2461 of 3363

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

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

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

Vivek Verma
Updated on 28-Aug-2025 14:46:52

3K+ Views

Yes, we can synchronize a run() method in Java using the synchronized keyword before this method. If this method is synchronized, only one thread can execute this on a given instance of the object at any point in time. Here is a code snippet that shows how to define the run() method as synchronized: @Override synchronized run(){ //code implementation } Here, the @Override annotation specifies that the run() method overrides a method from the Runnable interface, and synchronized is a reserved keyword in Java used to define a method or block as synchronized. Synchronization of a run() ... Read More

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

Program to reverse an array up to a given position in Python

Hafeezul Kareem
Updated on 27-Aug-2019 13:02:36

881 Views

In this tutorial, we will learn how to reverse an array upto a given position. Let's see the problem statement.We have an array of integers and a number n. Our goal is to reverse the elements of the array from the 0th index to (n-1)th index. For example, Input array = [1, 2, 3, 4, 5, 6, 7, 8, 9] n = 5 Output [5, 4, 3, 2, 1, 6, 7, 8, 9]Procedure to achieve the goal. Initialize an array and a number Loop until n / 2. Swap the (i)th index and (n-i-1)th elements.Print the array you will get the result.Example## initializing array and ... Read More

Python program to remove leading zeros from an IP address

Hafeezul Kareem
Updated on 27-Aug-2019 12:52:36

596 Views

In this tutorial, we are going to write a program which removes leading zeros from the Ip address. Let's see what is exactly is. Let's say we have an IP address 255.001.040.001, then we have to convert it into 255.1.40.1. Follow the below procedure to write the program.Initialize the IP address.Split the IP address with. using the split functionConvert each part of the IP address to int which removes the leading zeros.Join all the parts by converting each piece to str.The result is our final output.Example## initializing IP address ip_address = "255.001.040.001" ## spliting using the split() functions parts = ip_address.split(".") ## ... Read More

Python program to merge two Dictionaries

Hafeezul Kareem
Updated on 27-Aug-2019 12:48:32

708 Views

In this tutorial, we are going to learn how to combine two dictionaries in Python. Let's see some ways to merge two dictionaries.update() methodFirst, we will see the inbuilt method of dictionary update() to merge. The update() method returns None object and combines two dictionaries into one. Let's see the program.Example## initializing the dictionaries fruits = {"apple": 2, "orange" : 3, "tangerine": 5} dry_fruits = {"cashew": 3, "almond": 4, "pistachio": 6} ## updating the fruits dictionary fruits.update(dry_fruits) ## printing the fruits dictionary ## it contains both the key: value pairs print(fruits)If you run the above program, Output{'apple': 2, 'orange': 3, ... Read More

Python program to find all duplicate characters in a string

Sarika Singh
Updated on 26-Aug-2023 08:14:18

40K+ Views

This article teaches you how to write a python program to find all duplicate characters in a string. Characters that repeat themselves within a string are referred to as duplicate characters. When we refer to printing duplicate characters in a string, we mean that we shall print every character, including spaces, that appears more than once in the string in question. Input-Output Scenarios Following is the input-output scenario to find all the duplicate characters in a string − Input: TutorialsPoint Output: t, o, i As we can see, the duplicate characters in the given string "TutorialsPoint" are "t" with ... Read More

Python program to find all close matches of input string from a list

Hafeezul Kareem
Updated on 27-Aug-2019 12:23:17

672 Views

In this tutorial, we are going to find a solution to a problem. Let's see what the problem is. We have a list of strings and an element. We have to find strings from a list in which they must closely match to the given element. See the example.Inputs strings = ["Lion", "Li", "Tiger", "Tig"] element = "Lion" Ouput Lion LiWe can achieve this by using the startswith built-in method. See the steps to find the strings.Initialize string list and a string.Loop through the list.If string from list startswith element or element startswith the string from the listPrint the stringExample## initializing ... Read More

Program to check if all the values in a list that are greater than a given value in Python

Hafeezul Kareem
Updated on 27-Aug-2019 12:12:11

1K+ Views

In this tutorial, we will check whether all the elements in a list are greater than a number or not. For example, we have a list [1, 2, 3, 4, 5] and a number 0. If every value in the list is greater than the given value then, we return True else False.It's a simple program. We write it in less than 3 minutes. Try it yourself first. If you are not able to find the solution, then, follow the below steps to write the program.Initialise a list and any numberLoop through the list.If yes, return **False**Return True.Example## initializing the list    values ... Read More

Advertisements