Java Articles - Page 173 of 440

How to display a JRadioButtonMenuItem in Java?

raja
Updated on 03-Jul-2020 12:47:44

1K+ Views

A JRadioButtonMenuItem is a subclass of the JMenuItem class in Java. A JRadioButtonMenuItem is a menu item that is part of a group of menu items in which only one item in the group can be selected and the selected item displays its selected state. We can add multiple radio button menu items to a ButtonGroup object to form a button group. If one radio button menu item in a button group is selected, all other radio button menu items will be unselected.Syntaxpublic class JRadioButtonMenuItem extends JMenuItem implements AccessibleExampleimport javax.swing.*; import java.awt.*; public class JRadioButtonMenuItemTest extends JFrame {    private JMenuBar mb;   ... Read More

How can we initialize a boolean array in Java?

Vivek Verma
Updated on 05-May-2025 13:46:37

25K+ Views

Initializing a Boolean Array in Java refers to the process of assigning values (allocating memory) to the Boolean array for the first time. We can initialize an array or any variable - Either at the time of creation. Or, later in the program, using the assignment operator when we are just defining it initially. A Boolean array can be used to store only Boolean values (i.e., either true or false), and the "default value" of each element in a Boolean array is false.  In some cases, we may need to initialize all ... Read More

When can we use the pack() method in Java?

raja
Updated on 03-Jul-2020 12:33:28

5K+ Views

The pack() method is defined in Window class in Java and it sizes the frame so that all its contents are at or above their preferred sizes. An alternative to the pack() method is to establish a frame size explicitly by calling the setSize() or setBounds() methods. In general, using the pack() method is preferable to call than setSize() method, since pack leaves the frame layout manager in charge of the frame size and layout managers are good at adjusting to platform dependencies and other factors that affect the component size.Syntaxpublic void pack()Exampleimport java.awt.*; import javax.swing.*; public class PackMethodTest extends JFrame {    public ... Read More

How to trim white space in StringBuffer in Java?

Maruthi Krishna
Updated on 10-Sep-2019 13:28:48

3K+ Views

The String class of the java.lang package represents a set of characters. All string literals in Java programs, such as "abc", are implemented as instances of this class. Strings objects are immutable, once you create a String object you cannot change their values, if you try to do so instead of changing the value a new object is created with the required value and the reference shifts to the newly created one leaving the previous object unused.The StringBuffer (and StringBuilder) class is used when there is a necessity to make a lot of modifications to a String.Unlike Strings, objects of ... Read More

How can we check if specific string occurs multiple times in another string in Java?

Maruthi Krishna
Updated on 10-Sep-2019 13:24:09

3K+ Views

You can find whether a String contains a specified sequence of characters using any of the methods −The indexOf() method − The indexOf() method of the String class accepts a string value and finds the (starting) index of it in the current String and returns it. This method returns -1 if it doesn’t find the given string in the current one.The contains() method − The contains a () method of the String class accepts a sequence of characters value and verifies whether it exists in the current String. If found it returns true else it returns false.In addition to these, you ... Read More

How can we split a string by sentence as a delimiter in Java?

Maruthi Krishna
Updated on 10-Sep-2019 13:16:45

11K+ Views

The split() method of the String class accepts a String value representing the delimiter and splits into an array of tokens (words), treating the string between the occurrence of two delimiters as one token.For example, if you pass single space “ ” as a delimiter to this method and try to split a String. This method considers the word between two spaces as one token and returns an array of words (between spaces) in the current String.If the String does not contain the specified delimiter this method returns an array containing the whole string as an element.Example Live Demopublic class SplitExample ... Read More

Can I define more than one public class in a Java package?

Maruthi Krishna
Updated on 10-Sep-2019 12:45:18

7K+ Views

No, while defining multiple classes in a single Java file you need to make sure that only one class among them is public. If you have more than one public classes a single file a compile-time error will be generated.ExampleIn the following example we have two classes Student and AccessData we are having both of them in the same class and declared both public. Live Demoimport java.util.Scanner; public class Student {    private String name;    private int age;    Student(){       this.name = "Rama";       this.age = 29;    }    Student(String name, int age){   ... Read More

Which packages contain Wrapper class in Java?

Maruthi Krishna
Updated on 10-Sep-2019 12:42:21

2K+ Views

Java provides certain classes called wrapper classes in the java.lang package. The objects of these classes wrap primitive datatypes within them. Following is the list of primitive data types and their respective classes −Primitive datatypeWrapper classcharCharacterbyteByteshortShortintIntegerlongLongfloatFloatdoubleDoublebooleanBooleanPackageWrapper classes in Java belong to the java.lang package, Therefore there is no need to import any package explicitly while working with them.ExampleThe following Java example accepts various primitive variables from the user and creates their respective wrapper classes. Live Demoimport java.util.Scanner; public class WrapperClassesExample {    public static void main(String args[]){       Scanner sc = new Scanner(System.in);       System.out.println("Enter an integer ... Read More

How to read data from one file and print to another file in Java?

Maruthi Krishna
Updated on 10-Sep-2019 12:38:40

3K+ Views

Java provides I/O Streams to read and write data where a Stream represents an input source or an output destination which could be a file, i/o devise, other programs, etc.In general, a Stream will be an input stream or, an output stream.InputStream − This is used to read data from a source.OutputStream − This is used to write data to a destination.Based on the data they handle there are two types of streams −Byte Streams − These handle data in bytes (8 bits) i.e., the byte stream classes read/write data of 8 bits. Using these you can store characters, videos, ... Read More

How to read/write data from/to .properties file in Java?

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

12K+ Views

The .properties is an extension in java which is used to store configurable application. It is represented by the Properties class in Java, you can store a properties file and read from it using the methods of this class. This class inherits the HashTable class.Creating a .properties file −To create a properties file −Instantiate the Properties class.Populate the created Properties object using the put() method.Instantiate the FileOutputStream class by passing the path to store the file, as a parameter.ExampleThe Following Java program creates a properties file in the path D:/ExampleDirectory/ Live Demoimport java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; public class CreatingPropertiesFile { ... Read More

Advertisements