ShowCurrencies.java Source code

Java tutorial

Introduction

Here is the source code for ShowCurrencies.java

Source

import java.util.Currency;
import java.util.Locale;

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

public class ShowCurrencies extends JFrame {
    public ShowCurrencies() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        final Locale[] locales = Locale.getAvailableLocales();
        TableModel model = new AbstractTableModel() {
            public int getColumnCount() {
                return 3;
            }

            public String getColumnName(int column) {
                if (column == 0)
                    return "Locale";
                else if (column == 1)
                    return "Currency Code";
                else
                    return "Currency Symbol";
            }

            public int getRowCount() {
                return locales.length;
            }

            public Object getValueAt(int row, int col) {
                if (col == 0)
                    return locales[row];
                else
                    try {
                        if (col == 1)
                            return Currency.getInstance(locales[row]).getCurrencyCode();
                        else
                            return Currency.getInstance(locales[row]).getSymbol(locales[row]);
                    } catch (IllegalArgumentException iae) {
                        return null;
                    }
            }
        };

        JTable table = new JTable(model);
        JScrollPane sp = new JScrollPane(table);
        getContentPane().add(sp);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        new ShowCurrencies();
    }
}