我想在JTable Cell中添加一个JDateChooser。我知道如何添加文本框、组合框和复选框。
我允许用户向表中输入数据。用户应该能够为每一行选择一个日期。
如有任何帮助,我们将不胜感激
感谢
按如下方式设置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();
}
}
屏幕截图
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;
}