How do I create a simple JTable component?

The code snippet presented below shows you how to create a simple JTable component in a swing application. To create a JTable component we initializes it using the constructor that accept two parameters.

The first parameter is the table’s row of data which type is Object[][], a two-dimensional array of Object. The second parameter is the table’s column names which type is Object[], an array of object.

After the JTable instance is created we places it inside a scroll pane component which in turn is added to the frame’s content pane.

package org.kodejava.example.swing;

import javax.swing.*;
import java.awt.*;
import java.text.MessageFormat;
import java.util.Calendar;

public class SimpleJTableDemo extends JFrame {
    public SimpleJTableDemo() throws HeadlessException {
        initializeUI();
    }

    private void initializeUI() {
        // Defines table's column names.
        String[] columnNames = {
            "ID", "Name", "Date of Birth", "Sex"
        };

        // Defines table's data.
        Object[][] rowData = {
            {1, "Alice", createDOB(1980, Calendar.JANUARY, 1), "F"},
            {2, "Bob", createDOB(1982, Calendar.JUNE, 21), "M"},
            {3, "Carol", createDOB(1970, Calendar.OCTOBER, 12), "M"},
            {4, "Mallory", createDOB(1988, Calendar.FEBRUARY, 19), "M"},
            {5, "Ignasia", createDOB(1984, Calendar.NOVEMBER, 28), "F"}
        };

        // Initializes an instance of JTable and specifies the table
        // data and column names. After that we place the table in a
        // scroll pane.
        JTable table = new JTable(rowData, columnNames);
        JScrollPane pane = new JScrollPane(table);

        // Sets the frame setting.
        setTitle("Simple JTable Demo");
        setSize(new Dimension(400, 200));
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        getContentPane().add(pane, BorderLayout.CENTER);
    }

    private String createDOB(int year, int month, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, day);
        return MessageFormat.format("{0,date,dd-MMM-yyyy}", calendar.getTime());
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new SimpleJTableDemo().setVisible(true);
            }
        });
    }
}

When we running the program we will see the following result:

Simple JTable Demo

How do I format a message that contains number information?

This example show you how to use java.text.MessageFormat class to format a message that contains numbers.

package org.kodejava.example.text;

import java.text.MessageFormat;
import java.util.Locale;

public class MessageFormatNumber {
    public static void main(String[] args) {

        // Set the Locale for the MessageFormat.
        Locale.setDefault(Locale.US);

        // Use the default formatting for number.
        String message = MessageFormat.format("This is a {0} and {1} numbers",
                10, 75);
        System.out.println(message);

        // This line have the same format as above.
        message = MessageFormat.format("This is a {0,number} and {1,number} " +
                "numbers", 10, 75);
        System.out.println(message);

        // Format a number with 2 decimal digits.
        message = MessageFormat.format("This is a formatted {0, number,#.##} " +
                "and {1, number,#.##} numbers", 25.7575, 75.2525);
        System.out.println(message);

        // Format a number as currency.
        message = MessageFormat.format("This is a formatted currency " +
                "{0,number,currency} and {1,number,currency} numbers",
                25.7575, 25.7575);
        System.out.println(message);

        // Format numbers in percentage.
        message = MessageFormat.format("This is a formatted percentage " +
                "{0,number,percent} and {1,number,percent} numbers", 0.10, 0.75);
        System.out.println(message);
    }
}

The result of the program are the following lines:

This is a 10 and 75 numbers
This is a 10 and 75 numbers
This is a formatted  25.76 and  75.25 numbers
This is a formatted currency $10.00 and $75.00 numbers
This is a formatted percentage 10% and 75% numbers

How do I format a message that contains time information?

Here we demonstrate how to use the java.text.MessageFormat class to format a message contains a time information.

package org.kodejava.example.text;

import java.util.Date;
import java.util.Calendar;
import java.util.Locale;
import java.text.MessageFormat;

public class MessageFormatTime {
    public static void main(String[] args) {
        Date today = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR, 7);

        Date nextWeek = calendar.getTime();

        // We want the message to be is Locale.US
        Locale.setDefault(Locale.US);

        // Format a time including date information.
        String message = MessageFormat.format("Now is {0} and the next " +
                "7 hours is {1}", today, nextWeek);
        System.out.println(message);

        // Format a time and display only the time portion
        message = MessageFormat.format("Now is {0, time} and the next " +
                "7 hours is {1, time}", today, nextWeek);
        System.out.println(message);

        // Format a time using a short format (eg. HH:mm am/pm)
        message = MessageFormat.format("Now is {0, time, short} and " +
                "the next 7 hours is {1, time, short}", today, nextWeek);
        System.out.println(message);

        // Format a time using a medium format (eg. HH:mm:ss am/pm).
        message = MessageFormat.format("Now is {0, time, medium} and " +
                "the next 7 hours is {1, time, medium}", today, nextWeek);
        System.out.println(message);

        // Format a time using a long format (eg. HH:mm:ss am/pm TIMEZONE).
        message = MessageFormat.format("Now is {0, time, long} and the " +
                "next 7 hours is {1, time, long}", today, nextWeek);
        System.out.println(message);

        // Format a time using a full format (eg. HH:mm:ss am/pm TIMEZONE).
        message = MessageFormat.format("Now is {0, time, full} and the " +
                "next 7 hours is {1, time, full}", today, nextWeek);
        System.out.println(message);

        // Format a time using a custom pattern.
        message = MessageFormat.format("Now is {0, time, HH:mm:ss.sss} " +
                "and the next 7 hours is {1, time, HH:mm:ss.sss}", today, nextWeek);
        System.out.println(message);
    }
}

The above program produces:

Now is 2/15/18, 9:07 PM and the next 7 hours is 2/16/18, 4:07 AM
Now is 9:07:26 PM and the next 7 hours is 4:07:26 AM
Now is 9:07 PM and the next 7 hours is 4:07 AM
Now is 9:07:26 PM and the next 7 hours is 4:07:26 AM
Now is 9:07:26 PM CST and the next 7 hours is 4:07:26 AM CST
Now is 9:07:26 PM China Standard Time and the next 7 hours is 4:07:26 AM China Standard Time
Now is  21:07:26.026 and the next 7 hours is  04:07:26.026

How do I format a message that contains date information?

This example demonstrate how you can use the java.text.MessageFormat class to format a message with a date information in it.

package org.kodejava.example.text;

import java.util.Date;
import java.util.Calendar;
import java.util.Locale;
import java.text.MessageFormat;

public class MessageFormatDate {
    public static void main(String[] args) {
        Date today = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, 7);

        Date nextWeek = calendar.getTime();

        // We want the message to be is Locale.US
        Locale.setDefault(Locale.US);

        // Format a date, the time value is included
        String message = MessageFormat.format("Today is {0} and next " +
            "week is {1}", today, nextWeek);
        System.out.println(message);

        // Format a date and display only the date portion
        message = MessageFormat.format("Today is {0,date} and next " +
            "week is {1,date}", today, nextWeek);
        System.out.println(message);

        // Format a date using a short format (eg. dd/MM/yyyy)
        message = MessageFormat.format("Today is {0,date,short} and " +
            "next week is {1,date,short}", today, nextWeek);
        System.out.println(message);

        // Format a date using a medium format, it display the month long name,
        // but using a two digit date and year.
        message = MessageFormat.format("Today is {0,date,medium} and " +
            "next week is {1,date,medium}", today, nextWeek);
        System.out.println(message);

        // Format a date using a long format, two digit for date, a long month
        // name and a four digit year.
        message = MessageFormat.format("Today is {0,date,long} and " +
            "next week is {1,date,long}", today, nextWeek);
        System.out.println(message);

        // Format a date using a full format, the same as above plus a full day
        // name.
        message = MessageFormat.format("Today is {0,date,full} and " +
            "next week is {1,date,full}", today, nextWeek);
        System.out.println(message);

        // Format a date using a custom pattern.
        message = MessageFormat.format("Today is {0,date,dd-MM-yyyy} and " +
            "next week is {1,date,dd-MM-yyyy}", today, nextWeek);
        System.out.println(message);
    }
}

The above program produces:

Today is 2/15/18, 8:57 PM and next week is 2/22/18, 8:57 PM
Today is Feb 15, 2018 and next week is Feb 22, 2018
Today is 2/15/18 and next week is 2/22/18
Today is Feb 15, 2018 and next week is Feb 22, 2018
Today is February 15, 2018 and next week is February 22, 2018
Today is Thursday, February 15, 2018 and next week is Thursday, February 22, 2018
Today is 15-02-2018 and next week is 22-02-2018