62,625
社区成员
发帖
与我相关
我的任务
分享
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
import java.text.*;
public class DateTableFrame extends JFrame
{
private String[] columnNames = new String[] {"序号", "时间"};
private DefaultTableModel model;
private JTable table;
public DateTableFrame()
{
model = new DefaultTableModel(columnNames, 0);
model.addRow(new Object[] {1, new Date()});
table = new JTable(model);
TableColumn dateColumn = table.getColumn("时间");
dateColumn.setCellEditor(new DateTableCellEditor());
dateColumn.setCellRenderer(new DateTableCellRenderer());
add(new JScrollPane(table));
}
public static void main(String[] args)
{
JFrame frame = new DateTableFrame();
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class DateTableCellEditor extends AbstractCellEditor implements TableCellEditor
{
private JTextField tfDate = new JTextField();
private Date date;
public Object getCellEditorValue()
{
return date;
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
{
Date date = (Date) value;
this.date = date;
tfDate.setText(new SimpleDateFormat("yyyy-MM-dd").format(date));
return tfDate;
}
}
class DateTableCellRenderer extends JLabel implements TableCellRenderer
{
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
Date date = (Date) value;
this.setText(new SimpleDateFormat("yyyy-MM-dd").format(date));
return this;
}
}
public Object[] toArray() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Object[] vars = new Object[5];
vars[0] = this.no;
vars[1] = this.name;
vars[2] = this.capboard;
vars[3] = sdf.format(this.start);
vars[4] = sdf.format(this.end);
return vars;
}public Access(int no,String name,int capboard,String start,String end){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
this.no = no;
this.name = name;
this.capboard = capboard;
this.start = sdf.parse(start);
this.end = sdf.parse(end);
}