Programming Articles - Page 2535 of 3366

What are the differences between paint() method and repaint() method in Java?

Alshifa Hasnain
Updated on 15-Apr-2025 19:15:15

7K+ Views

In this article, we will learn about the differences between the paint() method and the repaint() method in Java. In the AWT and Swing frameworks, rendering graphical components is done in two different roles by the two methods paint() and repaint(). The paint() Method This method holds instructions to paint this component. In Java Swing, we can change the paintComponent() method instead of paint() method as paint calls paintBorder(), paintComponent() and paintChildren() methods. We cannot call this method directly instead we can call repaint(). Syntax The following is the syntax: public void paint(Graphics g) { // paint() method ... Read More

How can we catch a double click and enter key events for a JList in Java?

Alshifa Hasnain
Updated on 09-Jun-2025 14:19:19

878 Views

In this article, we will learn to catch a double click and enter key events for a JList in Java. We will be using Java Swing to detect double click and Enter key events on a JList. When we double click an item in the list or press "Enter" key after selecting an item, the selected item is displayed. What is a JList? A JList can extend the JComponent class, which allows us to choose either a single or multiple selections. A JList can generate a ListSelectionListener interface, and it includes one abstract method, valueChanged(). Below is the graphical representation ... Read More

What are the differences between a Font and a FontMetrics in Java?

raja
Updated on 29-Apr-2025 19:12:54

2K+ Views

The Font class in Java is responsible for setting screen fonts, which are mapped to characters of the language in specific regions. But a FontMetrics class is said to be a font metrics object that encapsulates the data about the rendering of a specific font on a particular screen. Font A Font class can be used to create an instance of a Font object to set the font for drawing text, labels, text fields, buttons, etc, and it can be specified by its name, style, and size. Syntax The following is the syntax for Font initialization: Font font = new ... Read More

How can we set the background color to a JPanel in Java?

Alshifa Hasnain
Updated on 08-May-2025 18:46:11

16K+ Views

In this article, we will learn to set the background color of a JPanel in Java. When designing GUIs in Swing, changing the background color of panels is important for creating visually appealing applications. What is a JPanel? A JPanel is a container, and it is an invisible component in Java. The FlowLayout is a default layout for a JPanel. We can add most of the components, like buttons, text fields, labels, tables, lists, trees, etc., into a JPanel. Syntax The following is the syntax for JPanel initialization: JPanel panel = new JPanel(); Methods The important methods of JPanel are: ... Read More

How many types of selection modes for a JList in Java?

raja
Updated on 10-Feb-2020 07:01:22

828 Views

A JList is a component that can extend JComponent class used to display a list of objects that allows the user to select one or more items.There are three types of selection modes for a JList in JavaListSelectionModel.SINGLE_SELECTION: Only one list index can be selected at a time.ListSelectionModel.SINGLE_INTERVAL_SELECTION: Only one contiguous interval can be selected at a time.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION: In this mode, there is no restriction on what can be selected. This is a default mode.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class JListSelectionModeTest extends JFrame implements ActionListener {    private JList list;    private DefaultListModel listModel;    public JListSelectionModeTest() {     ... Read More

How to implement a program to count the number in Java?

raja
Updated on 10-Feb-2020 06:17:01

1K+ Views

The program uses a JLabel to hold a count label, a JTextField component to hold the number count, JButton component to create add, remove and reset buttons. When we click the add button, the count in the JTextField will get incremented by '1' and by clicking the remove button the count will be decremented by '1'. If we click the Reset button, it will reset the count to '0'.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class CounterTest extends JFrame implements ActionListener {    private JLabel label;    private JTextField text;    private JButton addBtn, removeBtn, resetBtn;    private int count;    public CounterTest() {       setTitle("Counter Test");   ... Read More

How can we set a border to JCheckBox in Java?

Alshifa Hasnain
Updated on 05-May-2025 18:33:11

698 Views

In this article, we will learn to set a border for JCheckBox in Java. JCheckBox is a Swing component that is commonly used in user interface selection. Although it includes a default appearance, we can customize its look by setting borders in order to have a good visual effect. What is a JCheckBox? A JCheckBox is a component that extends JToggleButton, and an object of JCheckBox represents an option that can be checked or unchecked. Syntax The following is the syntax for JCheckBox initialization: JCheckBox Checkbox_name = new JCheckBox(); If there are two or more options, then any ... Read More

How can we implement cut, copy and paste functionality of JTextField in Java?

Alshifa Hasnain
Updated on 21-Apr-2025 19:27:48

2K+ Views

A JTextField is a child class of the JTextComponent class through which a single line of text can be edited. Cut, copy, and paste operations of the JTextField component can be implemented using the cut(), copy(), and paste() methods. These are built-in methods in the JTextField class. built-in methodsJTextField class The Cut Operation Cutting involves removing selected text from the JTextField and placing it on the clipboard. Method Declaration: public void cut() { textField.cut(); } The Copy Operation Copying places a copy of the selected text on the clipboard without removing it from the JTextField. Method ... Read More

C/C++ program to make a simple calculator?

karthikeya Boyini
Updated on 14-Feb-2025 18:07:39

1K+ Views

A simple calculator allows the user to perform basic math operations like addition, subtraction, multiplication, and division . The user inputs two numbers and selects an operation. For example, if the user enters 4 and 3, then selects the "+" operation, the calculator will perform addition and output the result, 7. Similarly, if the user selects "-", the program will perform subtraction (4 - 3 = 1), and so on. In C++, this can be done using conditional statements and basic math operators. In this article, we will show you how to write a simple calculator program in ... Read More

Area of Circumcircle of a Right Angled Triangle?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

1K+ Views

The area of circumcircle of a right-angle triangle when the hypotenuse(H) of the triangle is given is found using the formula πH2/4.This formula is derived using the fact that the circumcircle touches all the corners of the triangle, in this case the maximum length between two points in the hypotheses which passes through the center of the circle. This makes the hypotenuse the diameter of the circle.This is why the area of circle which is πd2/4. (d = 2r) replaces the d by H.ExampleHypotenuse = 8Area of circle = 50.26Example Code Live Demo#include int main(void) {    int H = ... Read More

Advertisements