Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I want to share something I just wrote for my project, I had to make a simple and quick date editor for a JTable that can edit in-place, it can also be used as a normal control. The date format as you can see can be modified switching the order of the string used in SimpleDateFormat, but don't forget to switch the ranges array. Here is the code:</p> <pre><code>public class DateControl extends JPanel { @SuppressWarnings("unchecked") private JComboBox&lt;String&gt;[] combos = new JComboBox[3]; SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd"); final int ranges[][] = {{2012,2050},{1,12},{1,31}}; public DateControl() { super(); combos[0] = new JComboBox&lt;String&gt;(); combos[1] = new JComboBox&lt;String&gt;(); combos[2] = new JComboBox&lt;String&gt;(); // Fill the combos for (int i = 0; i&lt;combos.length; i++) for (int j = ranges[i][0]; j&lt;ranges[i][1]; j++) combos[i].addItem((j&lt;9?"0":"")+Integer.toString(j)); this.setLayout(new BoxLayout(this,BoxLayout.X_AXIS)); for (JComboBox&lt;String&gt; c: combos) { // Remove the arrow button from the combo boxes (optional) c.setUI(new BasicComboBoxUI() { protected JButton createArrowButton() { return new JButton() { public int getWidth() { return 0; } }; } }); this.add(c); } //This is just for a nice look touch (optional) this.setBorder(BorderFactory.createRaisedBevelBorder()); // Set to today's date setDate(new Date()); } // Date argument constructor public DateControl(Date date) { this(); setDate(date); } public void setDate(Date d) { String[] date = df.format(d).split("/"); for (int i=0;i&lt;combos.length; i++) combos[i].setSelectedItem(date[i]); } public Date getDate() { String str = combos[0].getSelectedItem()+"/"+combos[1].getSelectedItem()+"/"+combos[2].getSelectedItem(); Date ret = null; try { ret = df.parse(str); } catch (ParseException e) {e.printStackTrace();} return ret; } } </code></pre> <p>it can then be used as a cell editor like this:</p> <pre><code>class DateCellEditor extends AbstractCellEditor implements TableCellEditor { SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd"); private DateControl dateControl; public DateCellEditor() { dateControl = new DateControl(); } @Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { Date d = new Date(); try { Object str = table.getValueAt(row, column); if (str!=null) d = df.parse((String)str); } catch (ParseException e) { e.printStackTrace(); } dateControl.setDate(d); return dateControl; } @Override public Object getCellEditorValue() { return df.format(dateControl.getDate()); } } </code></pre> <p>The new cell editor can be used in your table columns like this:</p> <pre><code> TableColumn c = myTable.getColumnModel().getColumn(0); c.setCellEditor(new DateCellEditor()); </code></pre> <p>I must remind you that this is a cell "Editor", it will only be displayed when the cell is under edition. The date value will be displayed by the normal cell "Renderer" as a simple string with the format yyyy/MM/dd.</p> <p>I hope all this helps somebody :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload