1

I have a JTable 6 columns,Column 1 and Column 2 Cells have JDateChooserCellEditor(), i am getting following values like this 11 Mar, 2016,12 Mar, 2016.But I want to format JDateChooserCellEditor() to show something like this 11 Mar, 2016 HH:mm and 12 Mar, 2016 HH:mm.And my third cell have to give result of difference in time.24 hrs.my JTable is looking like this.pls see image and code.

image of table

  // column 1---
TableColumn col1=DailyTbl.getColumnModel().getColumn(1);
col1.setPreferredWidth(150);
DailyTbl.setDefaultEditor(java.util.Date.class, new JDateChooserCellEditor());

// Column 2------

TableColumn col1=DailyTbl.getColumnModel().getColumn(1);
col1.setPreferredWidth(150);
DailyTbl.setDefaultEditor(java.util.Date.class, new JDateChooserCellEditor()); 
mKorbel
  • 109,525
  • 20
  • 134
  • 319
akathir79
  • 31
  • 4
  • Based on the source code for `com.toedter.calendar.JDateChooserCellEditor` I would suggest that they don't want you to, as the `JDateChooser` is `private` and provides no other "real" methods to gain access to it. You could call its `getTableCellEditorComponent` and cast it to a `JDateChooser` but that seems ... ugly to me. – MadProgrammer Mar 11 '16 at 01:22

2 Answers2

1

Based on some code I found at this link: http://grepcode.com/file/repo1.maven.org/maven2/com.luuuis/jcalendar-tz/1.3.3-3/com/toedter/calendar/JDateChooser.java

I found a comment that suggests you can do what you want by using:

dateChooser.setDateFormatString("yyyy-MM-dd HH:mm");

Maybe that only changes the renderer, not the editor. I don't know as I have never used the class.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • That's probably true, but [`JDateChooserCellEditor`](http://grepcode.com/file/repo1.maven.org/maven2/com.luuuis/jcalendar-tz/1.3.3-3/com/toedter/calendar/JDateChooserCellEditor.java) doesn't provide direct access to the component, so we're left with using reflection (:P), calling `getTableCellEditorComponent` and casting the result (:P) or basically making our own ... I really don't like that API :P – MadProgrammer Mar 11 '16 at 01:46
  • can you explain me further how to implement in the code? or is other any solution to format next cell to 00:00 like this? – akathir79 Mar 11 '16 at 01:51
  • @akathir79, its one line of code. I don't know if it does what you want since I have never used this class before. It is up to you to try it and see what happens. – camickr Mar 11 '16 at 02:39
0

Find the solved answer to format JDateChooserCellEditor()

enter code here
public class JDateChooserCellEditor1 extends AbstractCellEditor implements
    TableCellEditor {

private static final long serialVersionUID = 917881575221755609L;

private JDateChooser dateChooser = new JDateChooser();

public Component getTableCellEditorComponent(JTable table, Object value,
        boolean isSelected, int row, int column) {

    Date date = null;
    if (value instanceof Date)
        date = (Date) value;

    dateChooser.setDate(date);
            dateChooser.setDateFormatString("yyyy-MM-dd HH:mm");
    return dateChooser;
}

public Object getCellEditorValue() {
    return dateChooser.getDate();
}
}

//and TableCellRender

    class DateRenderer implements TableCellRenderer
    {

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

    JPanel c = new JPanel();
      //JTextField c=new JTextField();
    if (value instanceof Date)
    {


        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        c.add(new JLabel(dateFormat.format(value)));
        c.setOpaque(true);


    }

    return c;
}

}

//Final Call the classes

  TableColumn col1=DailydatasTbl.getColumnModel().getColumn(1);
  col1.setPreferredWidth(200);
  Table.setDefaultEditor(java.util.Date.class, new JDateChooserCellEditor1());
   Table.getColumnModel().getColumn(1).setCellRenderer(new  DateRenderer());
akathir79
  • 31
  • 4