mardi 7 mars 2017

Text Input if Statements JavaFX

I am using JavaFX Netbeans and Scene Builder for my program. I am trying to create a student information input scene. I have all of the inputs working and going to a text file right now. It also displays a Success pop up box. However, I am trying to make it so that if one of the inputs is blank, nothing will be inputted and an error popup will come up. Right now both popups work, but it is broken and only the success comes up. No matter if all inputs are there or not, the error box will come up 4 times. I cannot figure out why because I have each if statement there. I also don't know where to start with not inputting any information if one field is not filled out.

public class CController implements Initializable {

@FXML 
private Button Button;

@FXML
private Text Student;

@FXML
private Button Add;

@FXML
private ComboBox Grade;

@FXML
private ComboBox Type;

@FXML
private TextField Name;

@FXML
private TextField ID;   

ObservableList<String> GRADES = FXCollections.observableArrayList("9","10", "11", "12");
ObservableList<String> EVENTS = FXCollections.observableArrayList("Business Law and Ethics", "Buying and Merchandising", "Financial Services", "Hospitality Services", "Marketing Communications", "Sports and Entertainment Marketing", "Travel and Tourism", "Accounting Applications", "Apparel and Accessories Marketing", "Automotive Services Marketing", "Business Finance", "Business Services Marketing", "Food Marketing", "Hotel and Lodging Management", "Human Resources Management", "Marketing Management", "Quick Serve Restaurant Management", "Restaurant and Food Service Management", "Retail Merchandising", "Sports and Entertainment Marketing");

@FXML
private void handleButtonAction(ActionEvent event) throws IOException {
    Parent FXMLDocument2Parent = FXMLLoader.load(getClass().getResource("B.fxml"));
    Scene FXMLDocument2Scene = new Scene(FXMLDocument2Parent);
    Stage AppStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
    AppStage.setScene(FXMLDocument2Scene);
    AppStage.show();
}


@Override
public void initialize(URL location, ResourceBundle resources) {
Grade.setItems(GRADES);
Type.setItems(EVENTS);
}

 @FXML   
 public void addtolist(ActionEvent e) throws IOException {

Alert success = new Alert(Alert.AlertType.INFORMATION);
success.setTitle("Information");
success.setHeaderText(null);
success.setContentText("Student Added");
success.showAndWait(); 

Alert nosave = new Alert(Alert.AlertType.INFORMATION);
     String idinput = ID.getText();
SendToText("\r\n");
SendToText(idinput);   
    if (idinput.isEmpty())
nosave.setTitle("Error");
nosave.setHeaderText(null);
nosave.setContentText("Please Enter All Student Information");
nosave.showAndWait();

 String nameinput = Name.getText();
SendToText(",");
SendToText(nameinput);
    if (nameinput.isEmpty())
nosave.setTitle("Error");
nosave.setHeaderText(null);
nosave.setContentText("Please Enter All Student Information");
nosave.showAndWait();

String grade;
grade = (String) Grade.getValue();
SendToText(",");
SendToText(grade);
    if (grade.isEmpty())
nosave.setTitle("Error");
nosave.setHeaderText(null);
nosave.setContentText("Please Enter All Student Information");
nosave.showAndWait();

String event;
event = (String) Type.getValue();
SendToText(",");
SendToText(event);
    if (event.isEmpty())
nosave.setTitle("Error");
nosave.setHeaderText(null);
nosave.setContentText("Please Enter All Student Information");
nosave.showAndWait();
 }



public void SendToText(String cartone) throws IOException {
    Path file = Paths.get("C:/Users/Shaheer.K/Documents/Shaheer/IAiffy/src/ia/Extra/list.txt");
     byte[] data = cartone.getBytes();
     OutputStream output = null;
try
{
output = new
BufferedOutputStream(Files.newOutputStream(file, APPEND));
output.write(data);
output.flush();
output.close();
}
catch(IOException e)
{
System.out.println("Message: " + e);
}
    }

}    

Aucun commentaire:

Enregistrer un commentaire