Programming Articles - Page 2650 of 3366

How to set action command to JButton in Java

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

4K+ Views

With set action command, here we are displaying a message in the console on the click of a button.Set the button first:JButton btn = new JButton("Demo Button");Now, set Action Listener to fire when the button is clicked:ActionListener actionListener = new ActionListener() {    public void actionPerformed(ActionEvent event) {       String str = event.getActionCommand();       System.out.println("Clicked = " + str);    } };The following is an example to set action command to JButton:Exampleimport java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JOptionPane; public class SwingDemo {    public static void main(final String args[]) {       JButton btn ... Read More

How to change Button Border in Java Swing

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

1K+ Views

For Button border, use createLineBorder() method in Java, which allows you to set the color of the Border as well:JButton button = new JButton("Demo Button!"); Border border = BorderFactory.createLineBorder(Color.BLUE);The following is an example to change button border in Java:Exampleimport java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.border.Border; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Button Border");       Container container = frame.getContentPane();       JButton button = new JButton("Demo Button!");       Border border = BorderFactory.createLineBorder(Color.BLUE);       button.setBorder(border);   ... Read More

How to add action listener to JButton in Java

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

10K+ Views

The following is an example to add action listener to Button:Examplepackage my; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingDemo {    private JFrame frame;    private JLabel headerLabel;    private JLabel statusLabel;    private JPanel controlPanel;    public SwingDemo(){       prepareGUI();    }    public static void main(String[] args){       SwingDemo swingControlDemo = new SwingDemo();       swingControlDemo.showButtonDemo();    }    private void prepareGUI(){       frame = new JFrame("Java Swing");       frame.setSize(500, 500);       frame.setLayout(new GridLayout(3, 1));       frame.addWindowListener(new WindowAdapter() {         ... Read More

Can we get the supported image types in Java

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

135 Views

Yes, we can get the supported image types with ImageIO class in Java. The following is an example to get supported image types in Java:Examplepackage my; import javax.imageio.ImageIO; public class SwingDemo {    public static void main(String[] args) throws Exception {       String[] imgTypes = ImageIO.getReaderFileSuffixes();       System.out.print("Supported Image Types = ");       for (String type : imgTypes) {          System.out.print("" + type);       }    } }OutputSupported Image Types = jpg tif tiff bmp gif png wbmp jpeg

C++ Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)

Anvi Jain
Updated on 30-Jul-2019 22:30:26

180 Views

This is a C++ program to repeatedly search the same text.AlgorithmsBegin    Take the original string and pattern to be searched as input.    org_len = store the length of original string    pat_len = store the length of pattern    for i = 0 to (org_len - pat_len)       for j = 0 to pat_len - 1          if (org[i + j] != patt[j])             if (j == pat_len)                Increase m.       Print the position at which the pattern is ... Read More

C++ Program to Solve N-Queen Problem

Farhan Muhamed
Updated on 17-Jun-2025 19:15:44

13K+ Views

The N-Queens problem is a puzzle where we need to place N queens on an N x N chessboard such that no two queens attack each other. A queen will attack another queen if they are in the same row, column, or diagonal. In this problem, you are given a value of N, and you need find possible arrangements for N queens on N x N chessboard. For example, Consider that we have a chessboard of size 4 x 4. In this case, we can place maximum 4 queens on the chessboard such that no two queens attack ... Read More

C++ Program to Check if a Given Set of Three Points Lie on a Single Line or Not

Farhan Muhamed
Updated on 16-Jun-2025 18:09:45

840 Views

In this problem, you are given three coordinates in a 2D plane, and you need to check if these three points are collinear, meaning they lie on a single straight line. There are two approaches to solve this problem. In this article, we will explain both the approaches with example code in C++. // Input: Coordinates of three points points = {{2, 3}, {4, 6}, {6, 9}}; // Output The points are collinear. Check if Three Points are Collinear If three points lie on a single line, then the points are called as collinear points. ... Read More

C++ Program to Show the Duality Transformation of Line and Point

Farhan Muhamed
Updated on 16-Jun-2025 18:09:29

264 Views

The duality transformation is concept in computational geometry that maps coordinate points to lines and lines to coordinate points. In this article, we will learn all about the duality transformation of lines and points, and implement C++ code to show this transformation. What is Duality Transformation? The duality transformation is a process that converts 2 dimensional lines and coordinates into dual plane. In this transformation, a point in the 2D plane can be represented as a line in the dual space, and a line in the 2D plane can be represented as a point in the dual space. This ... Read More

C++ program to Implement Threaded Binary Tree

Farhan Muhamed
Updated on 16-Jun-2025 18:10:10

4K+ Views

Threaded binary tree is a binary tree that provides the facility to traverse the tree in a particular order. In this article, we will learn all about threaded binary trees, their types, and how to implement them in C++. What is a Threaded Binary Tree? A threaded binary tree is a type of binary tree in which NULL pointers are replaced with pointers to the in-order predecessor and successor nodes. This treading will help in faster traversal of the tree without using a stack or recursion. The image below shows a threaded binary tree. There are two types ... Read More

C++ Program to Find All Forward Edges in a Graph

Farhan Muhamed
Updated on 16-Jun-2025 18:09:57

330 Views

In this article, we will learn how to write an algorithm an C++ code to find all forward edges in a directed graph. What is a Forward Edge? A forward edge is an edge in a directed graph that points from a node to one of it's descendants in the depth-first search (DFS) tree. To understand this concept better, consider the image of a directed graph below: In the above graph, the edge from node 2 to node 5 is a forward edge because to reach node 5 in DFS traversal, we need to move through node ... Read More

Advertisements