How to deactivate a TextField and a Label by Combobox selection in javafx

假如想象 提交于 2019-12-25 04:43:13

问题


I would like to deactivate a TextField and its Label by a specific selection pre defined in my combobox in my JavaFX application. i have already tried the below code but its not working..

   public class Controller extends Application {
    @FXML
    private ComboBox<String>cmb1;
    @FXML
    private ComboBox<String>cmb2;
    @FXML
    private Button btn1,btn2;
    @FXML
    private TextField tf1,tf2,tf3,tf4,tf5,tf6;
    @FXML
    String v1,v2,v3,v4,v5,v6,cm1,cm2;
    @FXML
    private Label l6,l7;

    @Override
    public void start(Stage primaryStage) throws Exception {
    }

    private void play()
    {
        String check="Uwt-0";
        System.out.println("play control");
        tf5.setEditable(false);
        tf6.setEditable(false);
        //tf5.setVisible(false);
        //tf6.setVisible(false);
    }
    public void submit(ActionEvent event) {

        System.out.println("Submit Button Pressed");
        v1=tf1.getText();
        v2=tf2.getText();
        v3=tf3.getText();
        v4=tf4.getText();
        v5=tf5.getText();
        v6=tf6.getText();

        cm1=cmb1.getSelectionModel().getSelectedItem();
        cm2=cmb2.getSelectionModel().getSelectedItem();
        if(v1.isEmpty()||v2.isEmpty()||v3.isEmpty()||v4.isEmpty()||v5.isEmpty()||v6.isEmpty()||cmb1.getSelectionModel().getSelectedItem()==null||null==cmb2.getSelectionModel().getSelectedItem())
        {
            AlertBox.display("Error Dialouge", "Set All The Parameters");
               System.out.println("Submit Button Clicked Without Filling All Parameters..");
        }
        else
        {   
        System.out.println(v1 +"\n"+ v2+"\n"+v3+"\n"+cm1+"\n"+cm2+"\n"+v4+"\n"+v5+"\n"+v6);
        }
    }
    public void clear(ActionEvent event)
    {
        System.out.println("Clear Button Pressed");
        tf1.setText(null);
        tf2.setText(null);
        tf3.setText(null);
        tf4.setText(null);
        tf5.setText(null);
        tf6.setText(null);

        cmb1.setValue(null);
        cmb2.setValue(null);
    }
    //Action for second combobox fx:id-cmb2
    public void handle(ActionEvent event)
    {
        System.out.println("Control Here");
        if(cmb2.getSelectionModel().getSelectedItem()=="Uwt-0")
        {

        tf5.setEditable(false);
        tf6.setEditable(false);
        }
    }

} i have also tried this post:How to hide or deactivate a TextField and a Label with javafx

still its not working..


回答1:


Please change "==" to "equals(obj)" and check.

if(cmb2.getSelectionModel().getSelectedItem().equals("Uwt-0"))


来源:https://stackoverflow.com/questions/38069355/how-to-deactivate-a-textfield-and-a-label-by-combobox-selection-in-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!