雖然我從訪問數據庫檢索日期字段並在JTable中顯示它,但它是yyyy-MM-dd格式。我希望它是dd-MM-yyyy。雖然我在MS Access中更改了格式,但在面板中顯示的格式只有yyyy-MM-dd。我該如何改變它?JTable中的日期字段格式
日期字段是我用查詢檢索的許多字段之一。所以,我不明白在哪裏放置SimpleDateFormatter併爲每一行設置格式。請幫助我。提前致謝。
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();
}
}
先生,我應該在哪裏添加這個代碼?還有,先生,怎麼打電話? – Gomathi 2012-07-17 08:47:47
該方法應該是表模型的一種方法。如果您不知道TableModel是什麼,那麼一定要閱讀JTable教程:http://docs.oracle.com/javase/tutorial/uiswing/components/table.html – 2012-07-17 08:51:53
先生,我已經上傳了我的整個編碼。我也包括課程。但是我得到'}'返回聲明失蹤,儘管沒有任何遺漏先生。 – Gomathi 2012-07-17 09:36:39