JDatechooser inside JTable Cell

Related searches

I want to add a JDateChooser inside a JTable Cell. I know how to add a textbox, combobox and checkbox.

I allow the user to enter data to the table. The user should be able to select a date for every row.

Any help would be appreciated. Thank you

Read the section from the Swing tutorial on Using Other Editors.

It shows how to create a custom editor using JColorChooser. I would guess the code would be similiar for using a JDateChooser.

JDateChooser in jtable, I have a jtable with a JDateChooser column. I have used the below render and CellEditor impls. Rnederer class JDateChooserRenderer� JDatechooser inside JTable Cell. 0. Jtable not returning the last cell value. 1. sorting a JTable using getColumnClass() when connected to JDBC. 2.

Set the CellEditor as following:

table.getColumnModel().getColumn(1).setCellEditor(new JDateChooserEditor(new JCheckBox()));

/*************************************************************************/

Here is the code for JDateChooserEditor:

class JDateChooserEditor extends DefaultCellEditor
{
  public JDateChooserEditor(JCheckBox checkBox)
  {
    super(checkBox);

  }

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

         date = new JDateChooser();
         date.setDateFormatString("dd-MM-yyyy");
         return date;
  }

  public Object getCellEditorValue() 
  {
    return new String(((JTextField)date.getDateEditor().getUiComponent()).getText());
  }

  public boolean stopCellEditing()
  {
    return super.stopCellEditing();
  }

  protected void fireEditingStopped() {
    super.fireEditingStopped();
  }
}

Screen Shot

JDateChooser in JTable (Swing / AWT / SWT forum at Coderanch), I have a JTable that has a JDateChooser (toedter) in one of the columns. table, Object value, boolean isSelected, int row, int column) { I have a JTable that has a JDateChooser (toedter) in one of the columns. I have created a custom editor/renderer which sort of works, however I'm seeing 2 problems : 1.

This is a final solution for everyone, enjoy!!:


import com.toedter.calendar.JDateChooser;
import com.toedter.calendar.JTextFieldDateEditor;
import java.awt.Component;
import java.util.Calendar;
import java.util.Date;
import static javax.swing.BorderFactory.createEmptyBorder;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

/**
 *
 * @author Sergio
 */
public final class JDateChooserRenderer extends JDateChooser 
                                  implements TableCellRenderer  
{
    public JDateChooserRenderer() {                
        initilizie();
    }

    public void initilizie(){
        this.setPreferredSize(new java.awt.Dimension(50, 30));                 
        this.setDateFormatString("dd/MM/yyyy");
        this.setBorder(createEmptyBorder());    
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
                                                   boolean isSelected, boolean hasFocus,
                                                   int row, int column) {        
        // Configure text Editor
        JTextFieldDateEditor textEditor = (JTextFieldDateEditor) getDateEditor().getUiComponent();        
        textEditor.setBorder(createEmptyBorder());

        if (isSelected) {        
            textEditor.setBackground(table.getSelectionBackground()); 
        }   else {            
            textEditor.getUiComponent().setBackground(table.getBackground());
        }   

        if (value instanceof Date){
            this.setDate((Date) value);
        } else if (value instanceof Calendar){
            this.setCalendar((Calendar) value);
        }
        return this;        
    }
 }

import com.toedter.calendar.JDateChooser;
import com.toedter.calendar.JTextFieldDateEditor;
import javax.swing.AbstractCellEditor;
import javax.swing.table.TableCellEditor;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Calendar;
import java.awt.Component;
import javax.swing.JTable;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import static javax.swing.BorderFactory.createEmptyBorder;
import javax.swing.border.Border;

/**
 *
 * @author Sergio 
 */
public class JDateChooserEditor extends AbstractCellEditor
        implements TableCellEditor, PropertyChangeListener {

    JDateChooser DateChooser;
    Calendar calendar;  
    Border unselectedBorder = null;
    JTextFieldDateEditor textEditor = null;

    public JDateChooserEditor() {            
        DateChooser = new JDateChooser();         
        calendar = Calendar.getInstance();
        DateChooser.setPreferredSize(new java.awt.Dimension(50, 35));        
        DateChooser.setDateFormatString("dd/MM/yyyy"); 
        DateChooser.setCalendar(Calendar.getInstance());         
        // Configure text Editor
        textEditor = (JTextFieldDateEditor) DateChooser.getDateEditor().getUiComponent();        
        textEditor.setBorder(createEmptyBorder());
        // Listen for property changes
        DateChooser.addPropertyChangeListener(this);                          
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        // If the 'date' property was changed...           
        if ("date".equals(evt.getPropertyName())) {
            JDateChooser edateChooser = (JDateChooser) evt.getSource();
            boolean isDateSelectedByUser = false;
            // Get the otherwise unaccessible JDateChooser's 'dateSelected' field.
            Field dateSelectedField = null;
            try {
                // Get the desired field using reflection
                dateSelectedField = JDateChooser.class.getDeclaredField("dateSelected");
                // This line makes the value accesible (can be read and/or modified)
                if (dateSelectedField != null) {
                    dateSelectedField.setAccessible(true);                    
                    isDateSelectedByUser = dateSelectedField.getBoolean(edateChooser);

                    if (isDateSelectedByUser) {
                        calendar = (Calendar) edateChooser.getCalendar();                                                                                                
                        DateChooser.setCalendar(calendar);                              
                    }                 
                }                 
                fireEditingStopped();                
            } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException ex) {
                Logger.getLogger(JDateChooserEditor.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    // Se implementa el método CellEditor de la clase abstracta AbstractCellEditor.    
    @Override
    public Object getCellEditorValue() {       
       return calendar;
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,int row, int column) {               
        DateChooser.getDateEditor().getUiComponent().setBackground(table.getSelectionBackground());         
        if (value instanceof Date){
            calendar.setTime((Date) value);             
        } else if (value instanceof Calendar){
            calendar = (Calendar) value;
        };  
        DateChooser.setCalendar(calendar);
        return DateChooser;
    }

How to insert JDatePicker to JTable column (Swing / AWT / SWT , A friendly place for programming greenhorns! Jump to bottom of page. Win a copy of Spring in Action this week in the Spring forum! Forums� In my Java panel I have a JTable in which two of the columns use the JDateChooser object, from the package com.toedter.calendar, via a custom cell editor. Unlike cells that use text boxes or other simple components, which you can type into without having to click the cell beforehand, this functionality is not present on date-chooser cells.

JDateChooserCellEditor.java, This program is distributed in the hope that it will be useful, * but WITHOUT ANY new JDateChooser(); public Component getTableCellEditorComponent( JTable boolean isSelected, int row, int column) { Date date = null; if (value instanceof� In this java Tutorial we will see How To get the date from jdatechooser and display it in jtable on button click event JAVA Tutorials: Get Date From JTable And Show It In JDateChooser

Java, How To Take Date From JTable And Set It To JDateChooser in Java How To Get JTable Duration: 7:30 Posted: Dec 14, 2016 The JTable simply takes a tabular range of cells and uses getValueAt(int, int) to retrieve the values from the model during painting. It is important to remember that the column and row indexes returned by various JTable methods are in terms of the JTable (the view) and are not necessarily the same indexes used by the model.

Values to be displayed in JTextfield when Clicked on JTable Cells. Java JTable And JDateChooser 📅 GET & SET [ with source code ] - Duration: 18:03. 1BestCsharp blog 22,189 views.

Comments
  • @Prabhath: This example using jcalendar may guide you.