Java Articles - Page 256 of 745

How can we set the border to JComboBox items in Java?

raja
Updated on 03-Jul-2020 11:58:06

690 Views

A JComboBox is a subclass of JComponent class and it is a combination of a text field and a drop-down list from which the user can choose a value. A JComboBox can generate an ActionListener, ChangeListener and ItemListener interfaces when the user actions on a combo box. We can set the border to the items of a JComboBox by rendering a JComboBox which extends the DefaultListCellRenderer class and need to override the getListCellRendererComponent() method.Syntaxpublic Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)Exampleimport java.awt.*; import javax.swing.*; public class JComboBoxTest extends JFrame {    public JComboBoxTest() {       setTitle("JComboBox Test");       ... Read More

Util.Arrays and Reflect.Array in Java

Maruthi Krishna
Updated on 06-Sep-2019 13:57:13

133 Views

The util.Arrays classThe java.util.Arrays class contains a static factory that allows arrays to be viewed as lists. Following are the important points about Arrays −This class contains various methods for manipulating arrays (such as sorting and searching).The methods in this class throw a NullPointerException if the specified array reference is null.For example, the equals() method of this class accepts two arrays and compares them.Example Live Demoimport java.util.Arrays; public class ComparingArrays {    public static void main(String args[]){       String[] myArray1 = {"JavaFX", "HBase", "OpenCV", "WebGL", "FlexBox"};       String[] myArray2 = {"JavaFX", "HBase", "OpenCV", "WebGL", "FlexBox"};     ... Read More

Reflect Array class in Java

Maruthi Krishna
Updated on 06-Sep-2019 13:53:15

117 Views

The Array class of the java.lang.reflect package provides static methods to create and access Java arrays dynamically. Array permits widening conversions to occur during a get or set operation but throws an IllegalArgumentException if a narrowing conversion would occur.This class provides the newInstance() method, getter method, and setter methods. The newInstance() method accepts an object of the class named Class representing the required component and an integer representing the length of the array, creates and returns an array with the given specifications.Example Live Demoimport java.lang.reflect.Array; import java.util.Arrays; public class Reflection_ArrayExample {    public static void main(String args[]){       Integer ... Read More

Non static blocks in Java.

Maruthi Krishna
Updated on 06-Sep-2019 13:49:55

4K+ Views

A static block is a block of code with a static keyword. In general, these are used to initialize the static members. JVM executes static blocks before the main method at the time of class loading.Example Live Demopublic class MyClass {    static{       System.out.println("Hello this is a static block");    }    public static void main(String args[]){       System.out.println("This is main method");    } }OutputHello this is a static block This is main methodInstance initialization blocksSimilar to static blocks, Java also provides instance initialization blocks which are used to initialize instance variables, as an alternative to ... Read More

What is the purpose of using a dumpStack() method in Java?

raja
Updated on 01-Dec-2023 14:39:39

700 Views

The dumpStack() method is a static method of Thread class and it can be used to print or display stack tracing of the current thread to System.err. The purpose of the dumpStack() method is basically for debugging and Internally this method is calling the printStackTrace() method of Throwable class. This method does not raise any exceptions. Syntax public static void dumpStack() Example public class ThreadDumpStackTest { public static void main(String args[]) { Thread t = Thread.currentThread(); t.setName("ThreadDumpStackTest"); t.setPriority(1); System.out.println("Current Thread: " + ... Read More

How to Get a slice of a primitive array in Java?

Maruthi Krishna
Updated on 15-Oct-2019 09:25:21

916 Views

You can get a part of a Java array in between two specified indexes in various ways.By Copying contents:One way to do so is to create an empty array and copy the contents of the original array from the start index to the endIndex.Example Live Demoimport java.util.Arrays; public class SlicingAnArray {    public static int[] sliceArray(int array[], int startIndex, int endIndex ){       int size = endIndex-startIndex;       int part[] = new int[size];       //Copying the contents of the array       for(int i=0; iarray[i]);       part = stream.toArray();       //Copying the contents of the array       for(int i=0; i

Assigning arrays in Java.

Maruthi Krishna
Updated on 06-Sep-2019 13:03:58

9K+ Views

While creating variables first of all we will declare them, initialize them, assign/re-assign values to them.Similarly, while creating arrays −You can declare an array just like a variable −int myArray[];You can create an array just like an object using the new keyword −myArray = new int[5];You can initialize the array by assigning values to all the elements one by one using the index −myArray [0] = 101; myArray [1] = 102;Assigning values to an arrayWhen we assign primitive values of one type to a variable of other (datatype) implicitly they are converted.But, when you try to assign a higher datatype ... Read More

How to import classes from within another directory/package in Java?

Maruthi Krishna
Updated on 06-Sep-2019 12:37:48

7K+ Views

In Java classes and interfaces related to each other are grouped under a package. The package is nothing but a directory storing classes and interfaces of a particular concept. For example, all the classes and interfaces related to input and output operations are stored in the java.io package.There are two types of packages namely user-defined packages and built-in packages (pre-defined)The import keywordWhenever you need to use the classes from a particular package −First of all, you need to set a classpath for the JAR file holding the required package.Import the required class from the package using the import keyword. While ... Read More

Traversing contents of a hash map in Java

Maruthi Krishna
Updated on 06-Sep-2019 12:33:45

751 Views

A map is a collection in Java which stores key-value pairs. The keys of this must not be null and each key should point to only one value. It is represented by the Map interface of java.util package. There are various classes which provide implementation to this interface.The HashMap is a class which implements the Map interface. It is based on the Hash table. It allows null values and null keys.In short, you can store key-value pairs in the HashMap object. Once you do so you can retrieve the values of the respective keys but, the values we use for ... Read More

Can we assign values to final arrays in Java?

Maruthi Krishna
Updated on 06-Sep-2019 12:26:23

509 Views

The array is a container that can hold a fixed number of items and these items should be of the same type. Most of the data structures make use of arrays to implement their algorithms. The following are the important terms to understand the concept of Array.Element − Each item stored in an array is called an element.Index − Each location of an element in an array has a numerical index, which is used to identify the element.The size of the array will be determined at the time of creation.Example Live Demopublic class ArrayExample {    public static void main(String args[]){ ... Read More

Advertisements