Javafx TableCell中的DatePicker

时间:2014-04-15 05:12:55

标签: javafx tablecell

我已经实现了一个自定义的datePickerTableCell来显示单元格编辑的日期选择器。我正在使用ExtFX DatePicker(https://bitbucket.org/sco0ter/extfx/overview)。一切正常,除非我手动输入日期而不是从选择器中选择它,我在commitEdit()上得到一个NullPointerException。

datePicker.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent t) {
    if (t.getCode() == KeyCode.ENTER) {
        if(getItem() == null) {
            commitEdit(null);
        } else {
            commitEdit(getItem());
        }
    } else if (t.getCode() == KeyCode.ESCAPE) {
        cancelEdit();
    }
}
});

我发现这里出了什么问题。

4 个答案:

答案 0 :(得分:1)

也许看看这个例子。它使用颜色选择器,但也适用于日期选择器:

public class ColorTableCell<T> extends TableCell<T, Color> {    
    private final ColorPicker colorPicker;

    public ColorTableCell(TableColumn<T, Color> column) {
        this.colorPicker = new ColorPicker();
        this.colorPicker.editableProperty().bind(column.editableProperty());
        this.colorPicker.disableProperty().bind(column.editableProperty().not());
        this.colorPicker.setOnShowing(event -> {
            final TableView<T> tableView = getTableView();
            tableView.getSelectionModel().select(getTableRow().getIndex());
            tableView.edit(tableView.getSelectionModel().getSelectedIndex(), column);       
        });
        this.colorPicker.valueProperty().addListener((observable, oldValue, newValue) -> {
            if(isEditing()) {
                commitEdit(newValue);
            }
        });     
        setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
    }

    @Override
    protected void updateItem(Color item, boolean empty) {
        super.updateItem(item, empty);  

        setText(null);  
        if(empty) {     
            setGraphic(null);
        } else {        
            this.colorPicker.setValue(item);
            this.setGraphic(this.colorPicker);
        } 
    }
}

如果您使用的是Java 7,请将lambdas替换为匿名内部类,但它也可以正常工作。完整的博文是here

答案 1 :(得分:0)

查看this example是否有帮助。它使用Java 8和DatePicker控件。

答案 2 :(得分:0)

This example也可能会有所帮助。它使用带有Java 8的DatePicker和TableView控件。

答案 3 :(得分:0)

当使用from tkinter import * import tkinter window = tkinter.Tk() #Here is where we set up the window and it's aesthetics, window.title("BINARY-SUMZ!!!") #here we give the window a name, window.geometry("1000x800") #here we give the window a size, window.wm_iconbitmap('flower3.ico') #and here we give the window an icon. def Destroy(): #this function destroys any widgets on the current page. for widget in window.winfo_children(): widget.destroy() def StartButton(): #This function starts the game after being clicked on. print ("Game started from beginning.") Intro() #This function starts the game after being clicked on. def Menu(): #Creating a menu function SumsTitle = tkinter.Label(window, text="BINARY-SUMS!!!", #Here is where we create the title for the menu screen, we give it a name, fg = "light Green", #a foreground (text) color bg = "tomato", #a backgorund color font = "'Bauhaus 93 bold italic") SumsTitle.pack() #and the text is given a font. StartButtonWid = tkinter.Button(window, text = "Start Learning!!!", fg = "tomato", command= (StartButton)) StartButtonWid.pack() #Setting up the button for the start of the game. TitleCanvas = tkinter.Canvas(window, bg = "light blue" , height = 1000, width = 1000) TitleCanvas.pack() def Intro(): Destroy() #This function works fine SumsTitle = tkinter.Label(window, text="Welcome!!!", #Here is where we create the title for the menu screen, we give it a name, fg = "light green", #a foreground (text) color bg = "tomato", #a backgorund color height = 1, width = 14, font = "'Bauhaus 93 bold italic") SumsTitle.pack() Intro1 = tkinter.Label(window, text='Welcome to BINARY-SUMS!!! The fun, interactive binary learning game! in this game we will be learning about language based topics', font= "30") Intro1.pack() Intro2 = tkinter.Label(window, text='that will be vital to know in your AS computing or computer science exams. Please click the screen to continue.', font= "30") Intro2.pack() IntroCanvas = tkinter.Canvas(window, bg = "light blue" , height = 1500, width = 1000) IntroCanvas.bind("<Button-1>", Activ1()) IntroCanvas.pack() def Activ1(): Destroy() #this function crashes. if __name__ == "__main__": Menu() tkinter.mainloop() 作为表格单元格时,我遇到了类似的问题。问题是我在TextArea实施中使用startEdit。使用TableCell代替TableView#edit解决了这个问题。