2015-04-30 60 views
3

我试图从组合框中选择一个值时更新JavaFX中的标签。 comboBox中的值表示一个数组对象。当我第一次加载页面并选择一个选项时,它成功更新。但是,当我在组合框中选择不同的值时,标签不会更新。任何人都可以提出为什么这可能是?在选择组合框值后更新JavaFX中的标签

package controllers; 

import java.net.URL; 
import java.util.ResourceBundle; 

import app.TreatmentRoom; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.FXMLLoader; 
import javafx.fxml.Initializable; 
import javafx.scene.Node; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.scene.control.ComboBox; 
import javafx.scene.control.Label; 
import javafx.stage.Stage; 

public class DoctorAssessmentPageController implements Initializable { 

    @FXML 
    private ComboBox<String> comboBox; 
    @FXML 
    private Label nhsNumberLabel; 
    @FXML 
    private Label titleLabel; 
    @FXML 
    private Label firstNameLabel; 
    @FXML 
    private Label lastNameLabel; 

    ObservableList<String> list = FXCollections.observableArrayList("Treatment Room 1", "Treatment Room 2", 
     "Treatment Room 3", "Treatment Room 4", "Treatment Room 5"); 

    @Override 
    public void initialize(URL location, ResourceBundle resources) { 
    comboBox.setItems(list); 
    } 

    @FXML 
    private void buttonActionConfirmRoom(ActionEvent event) { 
    switch (comboBox.getValue()) { 
    case ("Treatment Room 1"): 
     setPatientInfo(0); 
     break; 
    case ("Treatment Room 2"): 
     setPatientInfo(1); 
     break; 
    case ("Treatment Room 3"): 
     setPatientInfo(2); 
     break; 
    default: 
     System.out.println("treatment room not recognised"); 
    } 

    } 

    public void setPatientInfo(int i) { 
    nhsNumberLabel.setText(TreatmentRoom.treat[i].getPatient().getNhsNumber()); 
    titleLabel.setText(TreatmentRoom.treat[i].getPatient().getTitle()); 
    firstNameLabel.setText(TreatmentRoom.treat[i].getPatient().getFirstName()); 
    lastNameLabel.setText(TreatmentRoom.treat[i].getPatient().getLastName()); 
    } 



    @FXML 
    private void btnLogout(ActionEvent event) throws Exception { 
    FXMLLoader loader = new FXMLLoader(); 
    loader.setLocation(getClass().getResource("/views/FXMLLoginPage.FXML")); 
    loader.load(); 
    Parent p = loader.getRoot(); 
    Stage stage = new Stage(); 
    stage.setScene(new Scene(p)); 
    stage.show(); 
    stage.centerOnScreen(); 
    // hides current page 
    ((Node) (event.getSource())).getScene().getWindow().hide(); 
    } 

} 

FXML为FXMLDoctorAssessmentPage

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.shape.*?> 
<?import javafx.scene.text.*?> 
<?import javafx.scene.control.*?> 
<?import java.lang.*?> 
<?import javafx.scene.layout.*?> 
<?import javafx.scene.layout.AnchorPane?> 

<AnchorPane prefHeight="600.0" prefWidth="1000" stylesheets="@../styles/DoctorAssessmentPage1.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.DoctorAssessmentPageController"> 
    <children> 
    <TabPane prefHeight="665.0" prefWidth="1385.0" tabClosingPolicy="UNAVAILABLE"> 
     <tabs> 
     <Tab text="Doctor Assessment"> 
      <content> 
      <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="665.0" prefWidth="1385.0"> 
       <children> 
       <Rectangle arcHeight="5.0" arcWidth="5.0" fill="#517da6" height="90.0" stroke="WHITE" strokeType="INSIDE" styleClass="rectangle-pane" width="1385.0" /> 
       <Rectangle arcHeight="5.0" arcWidth="5.0" fill="#72b2ee" height="53.0" layoutX="341.0" layoutY="494.0" stroke="WHITE" strokeType="INSIDE" styleClass="rectangle-pane2" width="193.0" /> 
       <Rectangle arcHeight="5.0" arcWidth="5.0" fill="#72b2ee" height="426.0" layoutX="43.0" layoutY="121.0" stroke="WHITE" strokeType="INSIDE" styleClass="rectangle-pane2" width="237.0" /> 
       <Label fx:id="nhsNumberLabel" layoutX="59.0" layoutY="226.0" prefHeight="20.0" prefWidth="158.0" styleClass="label-subTitle" /> 
       <Label fx:id="titleLabel" layoutX="57.0" layoutY="282.0" styleClass="label-subTitle" text="Label" /> 
       <Label fx:id="allergiesLabel" layoutX="56.0" layoutY="442.0" styleClass="label-subTitle" text="allergiesLabel" /> 
       <Label layoutX="58.0" layoutY="403.0" styleClass="label-info" text="Allergies:" /> 
       <Label fx:id="bloodTypeLabel" layoutX="57.0" layoutY="365.0" styleClass="label-subTitle" text="bloodTypeLabel" /> 
       <Label layoutX="57.0" layoutY="329.0" styleClass="label-info" text="Blood type:" /> 
       <Label layoutX="58.0" layoutY="251.0" prefHeight="25.0" prefWidth="66.0" styleClass="label-info" text="Name:" /> 
       <Label layoutX="58.0" layoutY="201.0" styleClass="label-info" text="NHS Number:" /> 
       <Label fx:id="lastNameLabel" layoutX="181.0" layoutY="282.0" styleClass="label-subTitle" text="Label" /> 
       <Label fx:id="firstNameLabel" layoutX="119.0" layoutY="282.0" styleClass="label-subTitle" text="Label" /> 
       <Button fx:id="btnExtend5mins" layoutX="1038.0" layoutY="389.0" mnemonicParsing="false" onAction="#btnExtend5minsClick" prefHeight="53.0" prefWidth="161.0" styleClass="button-extension" text="5+ minutes" /> 
       <Button layoutX="1038.0" layoutY="124.0" mnemonicParsing="false" prefHeight="53.0" prefWidth="161.0" text="Finalise Patient" /> 
       <TextArea layoutX="312.0" layoutY="122.0" prefHeight="321.0" prefWidth="270.0" promptText="Triage assessment" /> 
       <Label id="label-subTitle1" layoutX="57.0" layoutY="167.0" text="Patient" textFill="WHITE"> 
        <font> 
        <Font name="System Bold" size="15.0" /> 
        </font> 
       </Label> 
       <TextArea layoutX="621.0" layoutY="121.0" prefHeight="321.0" prefWidth="394.0" promptText="Doctor assessment" /> 
       <Label fx:id="timeSpentLabel" layoutX="438.0" layoutY="508.0" prefHeight="21.0" prefWidth="66.0" styleClass="label-subTitle" text="Label" /> 
       <Label layoutX="348.0" layoutY="508.0" prefHeight="25.0" prefWidth="89.0" styleClass="label-info" text="Time spent:" /> 
       <Button layoutX="1200.0" layoutY="25.0" mnemonicParsing="false" onAction="#btnLogout" text="Logout" /> 
       <Label fx:id="triageCategoryLabel" layoutX="57.0" layoutY="140.0" text="triageCategoryLabel" textFill="WHITE"> 
        <font> 
        <Font name="System Bold" size="15.0" /> 
        </font> 
       </Label> 
       <Label layoutX="348.0" layoutY="16.0" styleClass="label-title" text="Doctor Assessment &amp; Treatment Room" /> 
         <Button layoutX="823.0" layoutY="504.0" mnemonicParsing="false" onAction="#buttonActionConfirmRoom" prefHeight="40.0" prefWidth="190.0" text="Confirm" /> 
         <ComboBox fx:id="comboBox" layoutX="619.0" layoutY="505.0" prefHeight="39.0" prefWidth="190.0" promptText="Select Room" visibleRowCount="5" /> 
       </children> 
      </AnchorPane> 
      </content> 
     </Tab> 
     <Tab text="Queue"> 
      <content> 
      <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"> 
       <children> 
       <fx:include source="FXMLQueueTabPage.fxml" /> 
       </children> 
      </AnchorPane> 
      </content> 
     </Tab> 
     <Tab closable="false" text="Treatment Rooms"> 
      <content> 
      <fx:include source="FXMLTreatmentRoomPage.fxml" /> 
      </content> 
     </Tab> 
     </tabs> 
    </TabPane> 
    </children> 
</AnchorPane> 

感谢, ķ

+0

当被称为'buttonActionConfirmRoom'? – ItachiUchiha

+0

这是FXML页面上的一个按钮。当按下它应该得到comboBox的值并相应地更新标签。 – KvnH

+0

显示相关的FXML –

回答

0

如果我理解得很好,你想不按一下按钮,更新的标签。

这里是一个解决方案:

public class DoctorAssessmentPageController implements Initializable { 

@FXML 
private ComboBox<String> comboBox; 
@FXML 
private Label nhsNumberLabel; 
@FXML 
private Label titleLabel; 
@FXML 
private Label firstNameLabel; 
@FXML 
private Label lastNameLabel; 

ObservableList<String> list = FXCollections.observableArrayList("Treatment Room 1", "Treatment Room 2", 
     "Treatment Room 3", "Treatment Room 4", "Treatment Room 5"); 

@Override 
public void initialize(URL location, ResourceBundle resources) { 
    /* ******* Here is the Trick ********* 
    listens to the value property of the comboBox. 
    calls the updateLabels function when the value changes. */ 
    comboBox.valueProperty().addListener(new ChangeListener<String>() { 

     @Override 
     public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) { 
      updateLabels(newValue); 
     } 
    }); 
    comboBox.setItems(list); 
} 

private void updateLabels(String newValue) { 
    switch (newValue) { 
     case ("Treatment Room 1"): 
      setPatientInfo(0); 
      break; 
     case ("Treatment Room 2"): 
      setPatientInfo(1); 
      break; 
     case ("Treatment Room 3"): 
      setPatientInfo(2); 
      break; 
     default: 
      System.out.println("treatment room not recognised"); 
    } 

} 

public void setPatientInfo(int i) { 
    nhsNumberLabel.setText(TreatmentRoom.treat[i].getPatient().getNhsNumber()); 
    titleLabel.setText(TreatmentRoom.treat[i].getPatient().getTitle()); 
    firstNameLabel.setText(TreatmentRoom.treat[i].getPatient().getFirstName()); 
    lastNameLabel.setText(TreatmentRoom.treat[i].getPatient().getLastName()); 
} 

@FXML 
private void btnLogout(ActionEvent event) throws Exception { 
    FXMLLoader loader = new FXMLLoader(); 
    loader.setLocation(getClass().getResource("/views/FXMLLoginPage.FXML")); 
    loader.load(); 
    Parent p = loader.getRoot(); 
    Stage stage = new Stage(); 
    stage.setScene(new Scene(p)); 
    stage.show(); 
    stage.centerOnScreen(); 
    // hides current page 
    ((Node) (event.getSource())).getScene().getWindow().hide(); 
} 
} 

问候,

西里尔