Found 2616 Articles for Java

How can we convert a JSON string to a JSON object in Java?

raja
Updated on 03-Jul-2020 14:06:49

5K+ Views

The JSON stands for JavaScript Object Notation and it can be used to transfer and storage of data.  The JSONObject can parse text from a String to produce a map-like object. The object provides methods for manipulating its contents, and for producing a JSON compliant object serialization. The JSONArray can parse text from a String to produce a vector-like object. The object provides methods for manipulating its contents, and for producing a JSON compliant array serialization.In the below two examples, We can convert a JSON string to a JSON object.Example 1import org.json.JSONObject; import org.json.JSONArray; public class StringToJSONTest {    public static void main(String args[]) {   ... Read More

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

raja
Updated on 01-Dec-2023 14:43:00

142 Views

The getClass() method is from Object class and it returns an instance of the Class class. When we declare a new instance of an object, it will be referring to a class. There can only be one class per JVM but multiple object referring to it. So when we get the class of two objects, they might be referring to the same class. Syntax public final Class getClass() Example class User { private int id; private String name; public User(int id, String name) { this.id = id; this.name ... Read More

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

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

17K+ 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

353 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

682 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

141 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?

raja
Updated on 01-Dec-2023 15:03:32

22K+ Views

The boolean array can be used to store boolean datatype values only and the default value of the boolean array is false. An array of booleans are initialized to false and arrays of reference types are initialized to null. In some cases, we need to initialize all values of the boolean array with true or false. We can use the Arrays.fill() method in such cases. Syntax boolean[] booleanArray; Example import java.util.Arrays; public class BooleanArrayTest { public static void main(String[] args) { Boolean[] boolArray = new Boolean[5]; // initialize a boolean array for(int i ... Read More

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

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

4K+ 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 can we apply different borders to JButton in Java?

raja
Updated on 03-Jul-2020 12:29:27

4K+ Views

A JButton is a subclass of AbstractButton class and it can be used for adding platform-independent buttons in a Java Swing application. A JButon can generate an ActionListener interface when the user clicking on a button, it can also generate MouseListener when a user can do some actions from the mouse and KeyListener when a user can do some actions from the keyboard.We can set different borders like LineBorder, BevelBorder, EtchcedBorder, EmptyBorder, TitledBorder, etc to JButton using the setBorder() method of JComponent class.Syntaxpublic void setBorder(Border border)Exampleimport javax.swing.*; import java.awt.*; public class JButtonBordersTest extends JFrame {    private JButton button[];    private JPanel panel;    public JButtonBordersTest() {   ... Read More

How to set a different look and feels to swing components in Java?

raja
Updated on 03-Jul-2020 12:21:53

2K+ Views

The Java Swing allows us to customize the GUI by changing the look and feel(L&F). The look defines the general appearance of components and the feel defines their behavior. L&Fs are subclasses of the LookAndFeel class and each L&F is identified by its fully qualified class name. By default, the L&F is set to the Swing L&F ( Metal L&F)To set the L&F programmatically, we can call the method setLookAndFeel () of the UIManager class. The call to setLookAndFeel must be done before instantiating any Java Swing class, otherwise, the default Swing L&F will be loaded.Syntaxpublic static void setLookAndFeel(LookAndFeel newLookAndFeel) throws UnsupportedLookAndFeelExceptionExampleimport java.awt.*; import ... Read More

Advertisements