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 DateViewer 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" };
public static void main(String[] args) {
JFrame f = new JFrame("Date Viewer");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new DateViewer());
f.pack();
f.setVisible(true);
}
public DateViewer() {
tableModel = new LocaleTableModel();
JTable table = new JTable(tableModel);
add(new JScrollPane(table));
refreshTable();
}
protected void refreshTable() {
int style = DateFormat.SHORT;
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.getDateInstance(DateFormat.SHORT, locale);
break;
case MEDIUM_COLUMN:
formatter = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
break;
case LONG_COLUMN:
formatter = DateFormat.getDateInstance(DateFormat.LONG, locale);
break;
case FULL_COLUMN:
formatter = DateFormat.getDateInstance(DateFormat.FULL, locale);
}
return formatter.format(selectedDate);
}
public String getColumnName(int column) {
return columnHeaders[column];
}
}
}
|