[英]JTable setCellRenderer to format a text field to date?
I have a SQLite database with a date stored as VARCHAR
(yyyy-mm-dd), for example '2013-01-25'
. 我有一个SQLite数据库,其日期存储为VARCHAR
(yyyy-mm-dd),例如'2013-01-25'
。 My query retrieves the records from the table and displays it as stored. 我的查询从表中检索记录并将其显示为存储。 I need to display the VARCHAR
data in my JTable
as 'Friday January 25, 2013'. 我需要在我的JTable
显示VARCHAR
数据为'2013年1月25日星期五'。 I suspect using setCellRenderer for the column containing the VARCHAR is the way to go. 我怀疑使用setCellRenderer作为包含VARCHAR的列是可行的方法。 Further, I think it will be a two step process: first, converting the VARCHAR to a date value then formatting the date as desired. 此外,我认为这将是一个两步过程:首先,将VARCHAR转换为日期值,然后根据需要格式化日期。 I can do so as follows if I grab the VARCHAR value from the JTable
and display it in a JTextField
: 如果我从JTable
获取VARCHAR值并将其显示在JTextField
我可以这样做:
MyDate = new SimpleDateFormat("yyyy-MM-dd").parse(rs.getString("My_Date"));
and then formatting it as desired 然后根据需要格式化它
MyDateStr = new SimpleDateFormat("EEEE MMMM d, yyyy").format(MyDate);
That's all well and good; 这一切都很好,也很好; however, I need the formatted display in the JTable
column. 但是,我需要JTable
列中的格式化显示。 I've never used setCellRenderer
, so I could use some help getting started. 我从来没有使用过setCellRenderer
,所以我可以使用一些帮助入门。
Post by Rob Camick
about Table Format Renderers may solve your problem. Rob Camick
关于表格格式的 Rob Camick
渲染器可以解决您的问题。
UPDATE: 更新:
I tried an example (as I am also curious to look DateFormat
in JTable which I have not done so far) using mKorbel code. 我尝试了一个例子(因为我也很想看看JTable中的DateFormat
,我目前还没有做过),使用mKorbel代码。 The format which I have given as input
is "2013-01-25"
. 我input
的格式是"2013-01-25"
。
import java.awt.Dimension;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
public class JTableDateFormat {
public static void main(String[] args) {
Object[][] data = {
{"Amar", "2013-01-25"},
{"Sammy", "2013-01-25"}
};
Object[] columnNames = {"Name", "Date"};
JTable table = new JTable(data, columnNames);
table.getColumnModel().getColumn(1).setCellRenderer(new DateRenderer());
JFrame frame = new JFrame();
frame.add(new JScrollPane(table));
frame.setSize(new Dimension(400, 100));
frame.setVisible(true);
}
}
class DateRenderer extends DefaultTableCellRenderer {
private static final long serialVersionUID = 1L;
private Date dateValue;
private SimpleDateFormat sdfNewValue = new SimpleDateFormat("EE MMM dd hh:mm:ss z yyyy");
private String valueToString = "";
@Override
public void setValue(Object value) {
if ((value != null)) {
String stringFormat = value.toString();
try {
dateValue = new SimpleDateFormat("yyyy-mm-dd", Locale.ENGLISH).parse(stringFormat);
} catch (ParseException e) {
e.printStackTrace();
}
valueToString = sdfNewValue.format(dateValue);
value = valueToString;
}
super.setValue(value);
}
}
Make a class like this... 做一个像这样的课......
public class DateRenderer extends DefaultTableCellRenderer {
public DateRenderer() { // This is a contructor
DateFormatter formatter = new DateFormatter("yyyy-MM-dd");
}
public class DateFormatter extends SimpleDateFormat { //This another class within a class
public DateFormatter(String pattern) {
super(pattern);
}
}
}
And add this to your panel >> jTable.setDefaultRenderer(Date.class, new DateRenderer()); 并将其添加到面板>> jTable.setDefaultRenderer(Date.class,new DateRenderer());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.