简体   繁体   中英

JavaFX: How do you call and pass the control object?

This is a method for getting the text of different JavaFx controls.

private String getText(Node item) {
        
    String txt = "";

    if (item instanceof Label) {
        if(!((Label)item).getText().isEmpty()){
            txt = ((Label)item).getText();
        }
    }
    if (item instanceof TextField) {
        if(!((TextField)item).getText().isEmpty()){
            txt = ((TextField)item).getText();
        }
    }
    if (item instanceof CheckBox) {
        if(!((CheckBox)item).getText().isEmpty()){
            txt = ((CheckBox)item).getText();
        }
    }
    return txt;
}

I'm trying to lessen the lines of codes. Instead of defining Label, TextField, CheckBox, etc. Is there an object class that I could call for all other controls and put in beside the Node item so that I don't need to repeat and specify each control? Is there a way to make it like this?

private String getText(Node item, X x) {

    String txt = "";

    if (item instanceof x) {
        if(!((x)item).getText().isEmpty()){
            txt = ((x)item).getText();
        }
    return txt;
}

What should I replace X & x. I don't know what object should I call or replace the Control. Just don't mind about the logic of isEmpty. My main concern is what object should I call or replace the Control. And call the method like this

getText(txtSample, TextField.class) 

or

getText(lblSample, Label.class)

Thank you.

Since the common superclass Control has no method getText() (because not all controls have text), this is not possible.

EDIT: as James_D pointed out, Label and CheckBox do have a common ancestor that has the method getText() , namely Labeled . This saves us another instanceof check.

However, the rest of the similarities can be extracted. Suppose we first write a helper method to extract the text from the control (note the down casting in the same statement as the instanceof :

    private String getRawText( final Node item )
    {
        if( item instanceof Labeled labeled )
            return labeled.getText();
        else if( item instanceof TextField field )
            return field.getText();
        else //this is the default
            return "";
    }

then the original function becomes:

    private String getText( final Node item ) 
    {

        final var  txt = getRawText( item );

        return txt.isEmpty() 
                ? ""
                : txt;
    }

But note that you check text.isEmpty() while the default, with which you initialized the variable txt is itself empty ( "" ). Thus the check for empty is not needed. Did you mean .isBlank() (stripping whitespace only strings)? Or getText() == null ?

Otherwise, it can be removed, and then your left with the function getRawText() only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM