How do I remove trailing white space from a string?

The trim() method of a String class removes both leading and trailing white space from a string. In this example we use a regular expression to remove only the trailing white spaces from a string.

package org.kodejava.lang;

public class TrailingSpace {
    public static void main(String[] args) {
        String text = "     tattarrattat     ";
        System.out.println("Original      = " + text);
        System.out.println("text.length() = " + text.length());

        // Using a regular expression to remove only the trailing white space in
        // a string
        text = text.replaceAll("\\s+$", "");
        System.out.println("Result        = " + text);
        System.out.println("text.length() = " + text.length());
    }
}
Original      =      tattarrattat     
text.length() = 22
Result        =      tattarrattat
text.length() = 17

How do I remove leading white space from a string?

The trim() method of a String class removes both leading and trailing white space from a string. In this example we use a regular expression to remove only the leading white spaces from a string.

package org.kodejava.lang;

public class LeadingSpace {
    public static void main(String[] args) {
        String text = "     tattarrattat     ";
        System.out.println("Original      = " + text);
        System.out.println("text.length() = " + text.length());

        // Using regular expression to remove only the leading white
        // space in string
        text = text.replaceAll("^\\s+", "");
        System.out.println("Result        = " + text);
        System.out.println("text.length() = " + text.length());
    }
}
Original      =      tattarrattat     
text.length() = 22
Result        = tattarrattat     
text.length() = 17

How do I create a queue using LinkedList class?

package org.kodejava.util;

import java.util.LinkedList;
import java.util.Queue;

public class QueueDemo {
    public static void main(String[] args) {
        // Create an instance of a queue, ere we use the LinkedList class which
        // implements the Queue interface. Add some elements to the queue using
        // the offer method.
        Queue<String> queue = new LinkedList<>();
        queue.offer("First visitor");
        queue.offer("Second visitor");
        queue.offer("Third visitor");
        queue.offer("Fourth visitor");

        // Let see the size of our queue
        System.out.println("Size: " + queue.size());

        // The peek and element method read the head of the queue without removing
        // the element. The difference is, if the queue is empty peek method
        // return null while element method throws a NoSuchElementException
        // exception.
        System.out.println("Queue head using peek   : " + queue.peek());
        System.out.println("Queue head using element: " + queue.element());

        // The poll method retrieves and then removes the head of the queue.
        // On the next code will process all the element of the queue. When no
        // item in the queue the poll method will return null.
        Object data;
        while ((data = queue.poll()) != null) {
            System.out.println(data);
        }
    }
}

The code snippet above print the following output:

Size: 4
Queue head using peek   : First visitor
Queue head using element: First visitor
First visitor
Second visitor
Third visitor
Fourth visitor

How do I handle a window closing event in Swing?

Here you will see how to handle the window closing event of a JFrame. What you need to do is to implement a java.awt.event.WindowListener interface and call the frame addWindowListener() method to add the listener to the frame instance. To handle the closing event implements the windowClosing() method of the interface.

Instead of implementing the java.awt.event.WindowListener interface which require us to implement the entire methods defined in the interface, we can create an instance of WindowAdapter object and override only the method we need, which is the windowsClosing() method. Let’s see the code snippet below.

package org.kodejava.swing;

import javax.swing.JFrame;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class WindowClosingDemo extends JFrame {
    public static void main(String[] args) {
        WindowClosingDemo frame = new WindowClosingDemo();
        frame.setSize(new Dimension(500, 500));
        frame.add(new Button("Hello World"));

        // Add window listener by implementing WindowAdapter class to
        // the frame instance. To handle the close event we just need
        // to implement the windowClosing() method.
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("WindowClosingDemo.windowClosing");
                System.exit(0);
            }
        });

        // Show the frame
        frame.setVisible(true);
    }
}

How do I close a JFrame application?

Closing a JFrame application can be done by setting the default close operation of the frame. There are some defined constants for the default operation. These constants defined in the javax.swing.WindowConstants interface include EXIT_ON_CLOSE, HIDE_ON_CLOSE, DO_NOTHING_ON_CLOSE and DISPOSE_ON_CLOSE.

To exit an application we can set the default close operation to EXIT_ON_CLOSE, this option will call the System.exit() method when user initiate a close operation on the frame.

package org.kodejava.swing;

import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class MainFrameClose extends JFrame {
    public static void main(String[] args) {
        MainFrameClose frame = new MainFrameClose();
        frame.setSize(500, 500);

        // Be defining the default close operation of a JFrame to
        // EXIT_ON_CLOSE the application will be exited by calling
        // System.exit() when user initiate a close event on the
        // frame.
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}