0

我想在 JTable 单元中添加一个 JDateChooser。我知道如何添加文本框、组合框和复选框。

我允许用户向表中输入数据。用户应该能够为每一行选择一个日期。

任何帮助,将不胜感激。
谢谢

4

3 回答 3

4

阅读 Swing 教程中关于使用其他编辑器的部分。

它展示了如何使用 JColorChooser 创建自定义编辑器。我猜想代码与使用 JDateChooser 类似。

于 2013-02-23T03:08:30.280 回答
1

如下设置 CellEditor:

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

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

这是 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();
  }
}

截屏

于 2017-06-26T10:54:45.647 回答
0
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;
    }
于 2019-05-03T11:40:43.970 回答