Found 9150 Articles for Object Oriented Programming

How to display a JFrame to the center of a screen in Java?

raja
Updated on 03-Jul-2020 13:39:50

22K+ Views

A JFrame is a subclass of Frame class and the components added to a frame are referred to as its contents, these are managed by the contentPane. We can add the components to a JFrame to use its contentPane instead. A JFrame is like a Window with border, title, and buttons. We can implement most of the java swing applications using JFrame.By default, a JFrame can be displayed at the top-left position of a screen. We can display the center position of JFrame using the setLocationRelativeTo() method of Window class.Syntaxpublic void setLocationRelativeTo(Component c)Exampleimport javax.swing.*; import java.awt.*; public class JFrameCenterPositionTest extends JFrame {    public JFrameCenterPositionTest() {     ... Read More

How to save the elements of a TreeSet to a file in Java?

raja
Updated on 01-Dec-2023 14:47:10

479 Views

A TreeSet is a subclass of AbstractSet class and it does not allow duplicate elements. By default, TreeSet stores the elements in an ascending order and retrieval speed of an element out of a TreeSet is faster. The TreeSet class internally uses a TreeMap to store elements. The elements in a TreeSet are sorted according to their natural ordering. We can also save the elements stored in a TreeSet to a file by using the Arrays.asList() method and pass this set as an argument to the writeObject() method of ObjectOutputStream class. Syntax public class TreeSet extends AbstractSet implements NavigableSet, Cloneable, Serializable Example import java.util.*; import java.io.*; public ... Read More

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 use the StringTokenizer class in Java?

raja
Updated on 01-Dec-2023 14:52:22

269 Views

A StringTokenizer is a subclass of Object class and it can allow an application to break a string into tokens. A set of delimiters can be specified either at creation time or on a per-token basis. An instance of StringTokenizer behaves in two ways depending on whether it was created with the returnDelims flag having the value true or false. The object of StringTokenizer internally maintains a current position within the string to be tokenized. The important methods of StringTokenizer c;ass are hasMoreElements(), hasMoreTokens(), nextElement(), nextToken() and countTokens(). Syntax public class StringTokenizer extends Object implements Enumeration Example 1 import java.util.*; public class StringTokenizerTest1 { ... Read More

How can we initialize a boolean array in Java?

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

24K+ 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 read data from PDF file and display on console in Java?

Maruthi Krishna
Updated on 10-Sep-2019 13:44:22

11K+ Views

There are several libraries to read data from a pdf using Java. Let us see how to read data from a PDF document and display it on the console using a library named PDFBox.You can extract text using the getText() method of the PDFTextStripper class. This class extracts all the text from the given PDF document to use this.Load an existing PDF document using the static method load() of the PDDocument class.Instantiate the PDFTextStripper class.Retrieve.read the contents of the PDF page to a String using the getText() method of the PDFTextStripper class.Finally, close the document using the close() method of ... Read More

How to calculate String Buffer capacity in Java?

Maruthi Krishna
Updated on 10-Sep-2019 13:38:26

993 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 replace specific part of String and StringBuffer in Java?

Maruthi Krishna
Updated on 10-Sep-2019 13:33:41

298 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.Example Live Demopublic class StringExample {    public static void main(String[] args) {       String str = new String("Hello how are you");       System.out.println("Contents of the String: "+str);    } }OutputHello how are youStrings 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 ... 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

Advertisements