Programming Articles - Page 2663 of 3366

Java Program to select the first item in JList

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

404 Views

To select the first item in JList, use setSelectionInterval() method:String values[]= { "One", "Two", "Three", "Four", "Five", "Six"}; JList list = new JList(values); int begn = 0; int end = 0; list.setSelectionInterval(begn, end);The following is an example to select the first item in JList:Examplepackage my; import java.awt.event.*; import java.awt.*; import javax.swing.*; class SwingDemo extends JFrame {    static JFrame frame;    static JList list;    public static void main(String[] args) {       frame = new JFrame("JList Demo");       SwingDemo s = new SwingDemo();       JPanel panel = new JPanel();       String values[]= ... Read More

Java program to select all the items in a JList

Krantik Chavan
Updated on 10-Oct-2024 11:35:06

830 Views

In this article, we will learn how to select all the items in a JList in Java. The program creates a simple graphical user interface with a list of sports. It uses the setSelectionInterval() method to select all items in the list. This ensures that from the first item to the last item in the list, everything is selected when the program runs. Problem Statement Write a Java program to select all the items in a JList. Below is the demonstration of the same − Input sports[]= {"Football", "Fencing", "Cricket", "Squash", "Hockey", "Rugby"} Output Steps to select all ... Read More

Map to create new value from int array in Java

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

574 Views

Let’s say the following is our int array elements:10, 50, 100, 200, 250, 300, 400, 500Here, we are mapping and creating a new value by incrementing each int element with 1:Arrays.stream(new int[] {10, 50, 100, 200, 250, 300, 400, 500}).map(val -> val + 1)Now find the average:Arrays.stream(new int[] {10, 50, 100, 200, 250, 300, 400, 500}).map(val -> val + 1).average()The following is an example to Map and create new value from int array:Exampleimport java.util.Arrays; public class Demo {    public static void main(String[] args) throws Exception {       Arrays.stream(new int[] {10, 50, 100, 200, 250, 300, 400, 500}).map(val -> val + 1).average()          .ifPresent(System.out::println);    } }Output227.25

Preventing Object Copy in C++

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

292 Views

In C++, when classes are created, we can copy it using some copy constructor or assignment operator. In this section we will see, how to prevent object copy of a class in C++. To prevent object copy, we can follow some rules. These are like below.1. Creating private copy constructor and private assignment operator.Example#include using namespace std; class MyClass {    int x;    public:       MyClass() {          //non-parameterized constructor       }       MyClass(int y): x(y) {       }    private:       MyClass(const MyClass& obj) ... Read More

How to display row count in Java Swing JList

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

563 Views

Use JList getVisibleRowCount() method to display the row count in a JList:String sports[]= {"Tennis", "Archery", "Football", "Fencing", "Cricket", "Squash", "Hockey", "Rugby"}; Jlist list = new JList(sports); int rowCount = list.getVisibleRowCount(); System.out.println("Row Count = "+rowCount);The following is an example to display row count in JList:Exampleimport java.awt.event.*; import java.awt.*; import javax.swing.*; class SwingDemo extends JFrame {    static JFrame frame;    static JList list;    public static void main(String[] args) {       frame = new JFrame("JList Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       String sports[]= {"Tennis", Archery", "Football", "Fencing", "Cricket", "Squash", "Hockey", "Rugby"};       list = ... Read More

Advanced C++ with boost library

Samual Sam
Updated on 30-Jul-2019 22:30:26

1K+ Views

C++ boost libraries are widely useful library. This is used for different sections. It has large domain of applications. For example, using boost, we can use large number like 264 in C++.Here we will see some examples of boost library. We can use big integer datatype. We can use different datatypes like int128_t, int256_t, int1024_t etc. By using this we can get precision up to 1024 easily.At first we are multiplying two huge number using boost library.Example#include #include using namespace boost::multiprecision; using namespace std; int128_t large_product(long long n1, long long n2) {    int128_t ans = (int128_t) n1 * ... Read More

Object Slicing in C++

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

560 Views

Object slicing is used to describe the situation when you assign an object of a derived class to an instance of a base class. This causes a loss of methods and member variables for the derived class object. This is termed as information being sliced away. For example, class Foo {    int a; }; class Bar : public Foo {    int b; }Since Bar extends Foo, it now has 2 member variables, a and b. So if you create a variable bar of type Bar and then create a variable of type Foo and assign bar, you'll lose ... Read More

transform() in C++

Samual Sam
Updated on 30-Jul-2019 22:30:26

3K+ Views

The transform function is present in the C++ STL. To use it, we have to include the algorithm header file. This is used to perform an operation on all elements. For an example if we want to perform square of each element of an array, and store it into other, then we can use the transform() function.The transform function works in two modes. These modes are −Unary operation modeBinary operation modeUnary Operation ModeIn this mode the function takes only one operator (or function) and convert into output.Example#include #include using namespace std; int square(int x) {    //define square ... Read More

What is NaN in C++?

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

920 Views

The NaN is the abbreviation of Not a Number. It indicates undefined or non-representable floating point elements. One example of NaN is square root of some negative number, or result of 0/0.Example#include #include using namespace std; int main() {    cout >> "Square root of -5: " >> sqrt(-5) >> endl; }OutputSquare root of -5: nan

What does buffer flush means in C++ ?

Samual Sam
Updated on 30-Jul-2019 22:30:26

2K+ Views

The buffer flush is used to transfer of computer data from one temporary storage area to computers permanent memory. If we change anything in some file, the changes we see on the screen are stored temporarily in a buffer.In C++, we can explicitly have flushed to force the buffer to be written. If we use std::endl, it adds one new line character, and also flush it. If this is not used, we can explicitly use flush. In the following program at first no flush is used. Here we are trying to print the numbers, and wait for one seconds. For ... Read More

Advertisements