单选按钮启用文本区域

时间:2014-10-04 08:06:03

标签: java javafx radio-button

我仍然是Java的新手,如果现在选择一个单选按钮,我一直试图弄清楚如何启用文本区域进行编辑。我一直在谷歌搜索和阅读各种论坛上的帖子,但是我不知道他们在做什么或如何在我的计划中做到这一点。

这是我到目前为止所做的:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package labscheduler;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.*;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author S02105032
 */
public class FXMLLabSchedulerDocController implements Initializable {

    @FXML
    private TextField txtRequestorName;
    @FXML
    private TextField txtRequestorEmail;
    @FXML
    private TextField txtEventTitle;
    @FXML
    private TextField txtNumParticipants;
    @FXML
    private TextField txtEventDate;
    @FXML
    private TextField txtStartTime;
    @FXML
    private TextField txtEndTime;
    @FXML
    private ToggleButton togStartTime;
    @FXML
    private ToggleButton togEndTime;
    @FXML
    private CheckBox chkPrinter;
    @FXML
    private RadioButton rbYes;
    @FXML
    private RadioButton rbNo;
    @FXML
    private TextArea txtareaMessage;

    private String requestorName;
    private String requestorEmail;
    private String eventTitle;
    private int participants = 0;
    private Date eventDate;  
    private Date startTime;  
    private Date endTime;  
    private String printer;
    private String message;

    @FXML
    private void handleBtnSubmit(ActionEvent event) {
        requestorName = txtRequestorName.getText();
        requestorEmail = txtRequestorEmail.getText();
        eventTitle = txtEventTitle.getText();
        participants = Integer.parseInt(txtNumParticipants.getText());

        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/YY");
        try {
            eventDate = sdf.parse(txtEventDate.getText());
        } catch (ParseException ex) {
            System.out.println("invalid date format");
        }

        SimpleDateFormat stdf = new SimpleDateFormat("hh:mm");
        try {
            startTime = stdf.parse(txtStartTime.getText());
        } catch (ParseException ex) {
            System.out.println("invalid start time format");
        }

        SimpleDateFormat etdf = new SimpleDateFormat("hh:mm");
        try {
            endTime = etdf.parse(txtEndTime.getText());
        } catch (ParseException ex) {
            System.out.println("invalid end time format");
        }

        if(chkPrinter.isSelected()) {
            printer = "printer requested";
        }
        else {
            printer = "no printer needed";
        }

        if(rbYes.isSelected() && !rbNo.isSelected()) {
            txtareaMessage.setEditable(true);
            message = txtareaMessage.getText();
        }
        else {
            txtareaMessage.setEditable(false);
            message = "None";
        }

    }

    @FXML
    private void handleButtonClose(ActionEvent event) {
        System.out.println("Application Close!");
        Platform.exit();
    }

    @FXML
    private void handleButtonClear(ActionEvent e) {
        System.out.println("Clear All Fields!");
        txtRequestorName.setText("");
        txtRequestorEmail.setText("");
        txtEventTitle.setText("");
        txtNumParticipants.setText("");
        txtEventDate.setText("");
        txtStartTime.setText("");
        txtEndTime.setText("");
        chkPrinter.setSelected(false);
        rbYes.setSelected(false);
        rbNo.setSelected(false);
        txtareaMessage.setText("");
    }

    // Radio Buttons need to be set to group so only one can be selected at a time
    // If Radio Btn Yes is checked, enable txt area message, else disable
    // Set Toggle Buttons to toggle between AM & PM

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

}

当我选择任一单选按钮时,它似乎不会使文本区域可编辑。

3 个答案:

答案 0 :(得分:1)

JavaFX控件的状态由observable properties表示。因此,RadioButtonselectedPropertyTextAreaeditableProperty。您可以使用常用的set / get(或set / is)方法设置和检索这些值:txtareaMessage.setEditable(rbYes.isSelected()),您还可以观察它们的变化:

rbYes.selectedProperty().addListener(...);

或者您可以将一个属性的值绑定到另一个属性的值。绑定基本上只是将一个侦听器注册到一个属性,并在被观察的属性发生变化时更新另一个属性。

在您的情况下,逻辑非常简单(文本区域的可编辑属性的值应该与单选按钮的selected属性的值相同),因此您只需要在{{1}中建立一个简单的绑定方法:

initialize()

答案 1 :(得分:0)

    if(rbYes.isSelected() && !rbNo.isSelected()) {
        txtareaMessage.setEditable(true);
        message = txtareaMessage.getText();
    }
    else {
        txtareaMessage.setEditable(false);
        message = "None";
    }

由于您的按钮(ActionListener)的代码段位于handleBtnSubmit范围内,因此根据我的理解,您的单选按钮上需要ActionListener才能触发上述代码。

答案 2 :(得分:0)

  1. 像这样创建一个ToggleGroup。

    final ToggleGroup group = new ToggleGroup();
    
  2. 将Radiobuttons添加到“组”。

    RadioButton yes = new RadioButton("yes");   
    yes.setToggleGroup(group);
    
    RadioButton no= new RadioButton("no");    
    no.setToggleGroup(group);
    
  3. 检查是否选择了“是”RadioButton。

     yes.setOnAction(new EventHandler<ActionEvent>() {
     @Override
     public void handle(ActionEvent actionEvent) {
         if(yes.isSelected()){
              textField.setEnabled(true);
    
         }else{
              textField.setEnabled(false);
         }
       }
    });