Found 2616 Articles for Java

How can we implement transparent JDialog in Java?

raja
Updated on 12-Feb-2020 06:36:45

327 Views

A JDialog is a subclass of Dialog class and it does not hold minimize and maximize buttons at the top right corner of the window. There are two types of dialog boxes namely, modal and non-modal. The default layout for a dialog box is BorderLayout.In the below program, we can implement a transparent JDialog by customizing the AlphaContainer class and override the paintComponent() method.Exampleimport java.awt.*; import javax.swing.*; public class TransparentDialog {    public static void main (String[] args) {       JDialog dialog = new JDialog();       dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);       dialog.getRootPane().setOpaque(false);       dialog.setUndecorated(true);       dialog.setBackground(new Color (0, 0, ... Read More

How to print the maximum occurred character of a string in Java?

raja
Updated on 24-Nov-2023 11:20:31

401 Views

A String class can be used to represent the character strings, all the string literals in Java programs are implemented as an instance of the String Class. The Strings are constants and their values cannot be changed (immutable) once created. In the below program, we can print the maximum occurred character of a given string. Example public class MaxOccuredCharacterTest { public static void main(String[] args) { String str1 = maxOccuredChar("tutorialspoint"); System.out.println(str1); String str2 = maxOccuredChar("AABBAABBCCAABBAA"); System.out.println(str2); ... Read More

How to check the memory used by a program in Java?

raja
Updated on 24-Nov-2023 11:29:24

2K+ Views

For a long-running java code, which makes heavy use of dynamic memory, we may end up with Out-Of-Memory errors due to a memory shortage of the heap space. In the below program, we can test the free java heap space used by a program. If the heap space is used more than 90 percent, then the garbage collector is explicitly called. The System.gc() call is blocking the calling thread until the garbage collector has completed. Therefore, this code can be executed in a separate thread. Example public class GCTest { public void runGC() { Runtime runtime ... Read More

Importance of SerialVersionUID keyword in Java?

raja
Updated on 02-Jul-2020 09:02:37

5K+ Views

SerialVersionUIDThe SerialVersionUID must be declared as a private static final long variable in Java. This number is calculated by the compiler based on the state of the class and the class attributes. This is the number that will help the JVM to identify the state of an object when it reads the state of the object from a file.The SerialVersionUID can be used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible w.r.t serialization. If the deserialization object is different than serialization, then it can throw an InvalidClassException.If the serialVersionUID is ... Read More

How many ways to call garbage collector (GC) in Java?

raja
Updated on 02-Jul-2020 08:57:04

1K+ Views

The garbage collection in Java is carried by a daemon thread called Garbage Collector(GC). Instead of waiting until JVM to run a garbage collector we can request JVM to run the garbage collector. There is no guarantee whether the JVM will accept our request or not.In Java, we can call the garbage collector manually in two waysBy using System classBy using Runtime classBy using System classSystem class has a static method gc(), which is used to request JVM to call garbage collector.Examplepublic class SystemClassTest {    public static void main(String[] args){       SystemClassTest test = new SystemClassTest();   ... Read More

How can we call the invokeLater() method in Java?

raja
Updated on 02-Jul-2020 08:12:57

3K+ Views

An invokeLater() method is a static method of the SwingUtilities class and it can be used to perform a task asynchronously in the AWT Event dispatcher thread. The SwingUtilities.invokeLater() method works like SwingUtilities.invokeAndWait() except that it puts the request on the event queue and returns immediately. An invokeLater() method does not wait for the block of code inside the Runnable referred by a target to execute.Syntaxpublic static void invokeLater(Runnable target)Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class InvokeLaterTest extends Object {    private static void print(String msg) {       String name = Thread.currentThread().getName();       System.out.println(name + ": " + msg);    }   ... Read More

What is the use of Thread.sleep() method in Java?

raja
Updated on 27-Nov-2023 09:23:59

4K+ Views

The sleep() method is a static method of Thread class and it makes the thread sleep/stop working for a specific amount of time. The sleep() method throws an InterruptedException if a thread is interrupted by other threads, that means Thread.sleep() method must be enclosed within the try and catch blocks or it must be specified with throws clause. Whenever we call the Thread.sleep() method, it can interact with the thread scheduler to put the current thread to a waiting state for a specific period of time. Once the waiting time is over, the thread changes from waiting state to runnable state. Syntax public ... Read More

Importance of join() method in Java?

raja
Updated on 27-Nov-2023 09:31:03

3K+ Views

A join() is a final method of Thread class and it can be used to join the start of a thread's execution to the end of another thread's execution so that a thread will not start running until another thread has ended. If the join() method is called on a thread instance, the currently running thread will block until the thread instance has finished executing. Syntax public final void join() throws InterruptedException Example public class JoinTest extends Thread { public void run() { for(int i=1; i

What is the purpose of overriding a finalize() method in Java?

raja
Updated on 02-Jul-2020 07:25:16

1K+ Views

The finalize() method is a pre-defined method in the Object class and it is protected. The purpose of a finalize() method can be overridden for an object to include the cleanup code or to dispose of the system resources that can be done before the object is garbage collected. If we are overriding the finalize() method then it's our responsibility to call the finalize() method explicitly. The finalize() method can be invoked only once by the JVM or any given object.Syntaxprotected void finalize() throws ThrowableExamplepublic class FinalizeMethodTest {    protected void finalize() throws Throwable {       try {          System.out.println("Calling finalize() method ... Read More

How to implement a rollover effect for a JButton in Java?

raja
Updated on 02-Jul-2020 07:04:18

964 Views

A JButton is a subclass of AbstractButton and it can be used for adding platform-independent buttons to a GUI application. A JButon can generate an ActionListener interface when the button is pressed or clicked, it can also generate the MouseListener and KeyListener interfaces. We can implement the rollover effect when the mouse moves over a JButton by overriding the mouseEntered() method of the MouseListener interface.Syntaxvoid mouseEntered(MouseEvent e)Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class RollOverButtonTest extends JFrame {    private JButton button;    public RollOverButtonTest() {       setTitle("RollOverButton Test");       button = new JButton("Rollover Button");       button.addMouseListener(new MouseAdapter() {     ... Read More

Advertisements