如何将表格单元格值格式化为HH:MM

时间:2016-03-12 01:56:20

标签: java

我在jspinner列中有一个jtable。正在编辑jspinner时正在显示 07:20:14(HH:MM:SS)并且在选择另一个单元格后,它会在单元格值中写入Fri mar 01 07:20:14 IST 2016

但我希望将其格式化为jspinner值。怎么做? write value in jtable same as in jspinner

   TableColumn col3=DailydatasTbl.getColumnModel().getColumn(3);
   col3.setPreferredWidth(150);
   col3.setCellEditor(new Spinnerex());
   public class Spinnerex extends AbstractCellEditor implement  TableCellEditor{
   private JSpinner spinner;
   private SpinnerDateModel model;
   private SimpleDateFormat format;

    public Spinnerex() {
    Calendar cal = Calendar.getInstance();
    Date date = cal.getTime();
    model = new SpinnerDateModel();
    model.setValue(date);
    spinner = new JSpinner(model);
    format = ((JSpinner.DateEditor) spinner.getEditor()).getFormat();

    format.applyPattern("HH:mm:ss");
    }



    Object Valueactual;

    @Override
    public Object getCellEditorValue() {

         return spinner.getValue();

    }

    @Override
    public Component getTableCellEditorComponent(JTable jtable, Object o, boolean bln, int i, int i1) {

        return spinner;
   }

}

1 个答案:

答案 0 :(得分:0)

添加TableCellRenderer解决了我的问题。

enter code here
class DateRenderer implements TableCellRenderer
 {

@Override
public Component getTableCellRendererComponent(JTable table, Object  value,boolean isSelected, boolean hasFocus, int row, int column) {

JPanel c = new JPanel();

if (value instanceof Date)
    {


 SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
 c.add(new JLabel(dateFormat.format(value)));

    }

    return c;
   }

并将此类称为

enter code here
  YourTableName.getColumnModel().getColumn(4).setCellRenderer(new DateRenderer());