JTable 中的日期字段格式 [英] Date field format in JTable

查看:29
本文介绍了JTable 中的日期字段格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从 access 数据库中检索日期字段并将其显示在 JTable 中时,它采用 yyyy-MM-dd 格式.我希望它是 dd-MM-yyyy.虽然我在 MS Access 中更改了格式,但它在面板中显示时仅显示在 yyyy-MM-dd 中.我怎样才能改变它?

While I retrieve a date field from access database and show it in JTable, it is in yyyy-MM-dd format. I want it to be dd-MM-yyyy. Though I changed the format in MS Access, while it is shown in panel it is in yyyy-MM-dd only. How can I change it?

日期字段是我通过查询检索的众多字段之一.所以,我不明白把 SimpleDateFormatter 放在哪里并为每一行格式化它.请帮帮我.提前致谢.

The date field is one of the many fields I retrieve with a query. So, I don't understand where to put the SimpleDateFormatter and format it for each row. Kindly help me out. Thanks in advance.

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Locale;
import javax.swing.*;
import javax.swing.table.*;
import java.util.Date;
import java.sql.*;
import java.sql.ResultSetMetaData;
import java.text.*;

public class gc implements ActionListener
{
JComboBox cc=new JComboBox();
JFrame frame=new JFrame();
JTable table;
DefaultTableModel model;
String query;
int i;
JPanel panel=new JPanel();

public gc()
{
frame.setTitle("Composition Check");
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JPanel p1=new JPanel();
p1.setLayout(new FlowLayout());

//Locale locale = Locale.getDefault();
//  System.out.println("Before setting, Locale is = " + locale); 
//Locale.setDefault(Locale.English);
//System.out.println("Before setting, Locale is = " + locale); 

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbc:odbc:vasantham","","");
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("select DISTINCT composition from try");

while(rs.next())
{
 cc.addItem(rs.getString("composition"));
}

conn.close();
}
catch(Exception e)
{
System.out.println(e);
}

p1.add(cc);
cc.addActionListener(this);
frame.add(p1,BorderLayout.NORTH);

frame.setVisible(true);
}

public class MyTableModel extends DefaultTableModel
{
public Class getColumnClass(int col) 
{
    if (col == 3) 
    {
        return java.util.Date.class;
    }
}
}

public void addTable(String query)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbc:odbc:vasantham","","");
Statement st=conn.createStatement();
System.out.println(query);
ResultSet rs=st.executeQuery(query);
ResultSetMetaData md=rs.getMetaData();
int cols=md.getColumnCount();
//table=new JTable();
model=new DefaultTableModel();

model.addColumn("Purpose");
model.addColumn("Name");
model.addColumn("Manu");
model.addColumn("Expiry");
model.addColumn("Stock");
model.addColumn("Cost");
model.addColumn("Supplier");
model.addColumn("Supplier Number");
model.addColumn("Rack");

table=new JTable(new MyTableModel());

String[] tabledata=new String[cols];
int i=0;
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

while(rs.next())
{
for(i=0;i<cols;i++)
{
//if(i==3)
//{
 //System.out.println(rs.getObject(i+1).toString()); 
// tabledata[i]=formatter.format(rs.getObject(i+1).toString());
//}

 tabledata[i]=rs.getObject(i+1).toString();

}
model.addRow(tabledata);

}

panel.removeAll();
JScrollPane scroll = new JScrollPane(table); 
panel.setLayout(new BorderLayout());

panel.add(scroll,BorderLayout.CENTER);
frame.add(panel,BorderLayout.CENTER);
conn.close();
}
catch(Exception e)
{
System.out.println(e);
}
}

public void actionPerformed(ActionEvent evt)
{
String ac=(String)cc.getSelectedItem();
System.out.println(ac);

addTable("select * from try where composition='"+ac+"'");
frame.setVisible(true);
}

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

}

推荐答案

您需要自定义表模型来告诉 JTable 该列保存的是 Date 类型的值:

You need to customize the table model to tell the JTable that the column is holding values of type Date:

@Override
public Class getColumnClass(int col) {
    if (col == 3) {
        return java.util.Date.class;
    }
    // return the appropriate class for every column
}

这将使 JTable 使用渲染器以与当前语言环境相关联的格式格式化日期.如果您需要其他格式,则需要将另一个渲染器关联到该列,该列可以根据需要设置日期格式.

This will make the JTable use a renderer formatting the date with the format associated to the current locale. If you need another format, you need to associate another renderer to the column, which formats the date as you want to.

这篇关于JTable 中的日期字段格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆