TimeViewer.java Source code

Java tutorial

Introduction

Here is the source code for TimeViewer.java

Source

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class TimeViewer extends JPanel {
    protected AbstractTableModel tableModel;

    protected Date selectedDate = new Date();

    protected final static Locale[] availableLocales;

    static {
        availableLocales = Locale.getAvailableLocales();
    }

    public final static int LOCALE_COLUMN = 0;

    public final static int SHORT_COLUMN = 1;

    public final static int MEDIUM_COLUMN = 2;

    public final static int LONG_COLUMN = 3;

    public final static int FULL_COLUMN = 4;

    public final static String[] columnHeaders = { "Locale", "Short", "Medium", "Long", "Full" };

    // Create the window for the Time viewer,
    // and make sure that later components will fit
    public static void main(String[] args) {
        JFrame f = new JFrame("Time Viewer");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new TimeViewer());
        f.pack();
        f.setVisible(true);
    }

    public TimeViewer() {
        tableModel = new LocaleTableModel();
        JTable table = new JTable(tableModel);
        add(new JScrollPane(table));

        refreshTable();
    }

    protected void refreshTable() {
        int style = DateFormat.SHORT;

        DateFormat parser = DateFormat.getTimeInstance(style);
        selectedDate = new Date();
        tableModel.fireTableDataChanged();
    }

    class LocaleTableModel extends AbstractTableModel {
        public int getRowCount() {
            return availableLocales.length;
        }

        public int getColumnCount() {
            return columnHeaders.length;
        }

        public Object getValueAt(int row, int column) {
            Locale locale = availableLocales[row];
            DateFormat formatter = DateFormat.getInstance();

            switch (column) {
            case LOCALE_COLUMN:
                return locale.getDisplayName();
            case SHORT_COLUMN:
                formatter = DateFormat.getTimeInstance(DateFormat.SHORT, locale);
                break;
            case MEDIUM_COLUMN:
                formatter = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
                break;
            case LONG_COLUMN:
                formatter = DateFormat.getTimeInstance(DateFormat.LONG, locale);
                break;
            case FULL_COLUMN:
                formatter = DateFormat.getTimeInstance(DateFormat.FULL, locale);
            }
            return formatter.format(selectedDate);
        }

        public String getColumnName(int column) {
            return columnHeaders[column];
        }
    }
}