2016-11-20 6 views
0

私は基本的なMovie Rentalシミュレータアプリケーションを作成しています。現在、TextFieldsとComboBoxの入力を変数に格納する際に問題が発生しています。ほとんどの変数をStringに変換することができましたが、テストするために出力を試してみると、常にnullが返されます。JavaFXのTextFieldsとComboBoxからの入力を格納していますか?

ユーザーがコンボボックスで選択したものをGETして文字列として保存する方法を理解する必要があり、私のメソッドの結果を適切に保存する方法を理解する必要があります。私はこれまでにこの問題に遭遇したことはありません。だから私はそれにどのように取り組むべきか本当に分かりません。次のように私のコードは次のとおりです。

public class RentGameDialogController extends RentalStoreGUIControllerimplements Initializable{

/** TextField Objects **/ 
@FXML private TextField nameField, rentedOnField, dueBackField; 

/** String for NameField **/ 
String name, rentedOn, dueBack; 

/** Game ComboBox ID's **/ 
@FXML private ObservableList<GameType> cbGameOptions; 
@FXML private ComboBox<GameType> cbGame; 

/** Console ComboBox ID's **/ 
@FXML private ObservableList<PlayerType> cbConsoleOptions; 
@FXML private ComboBox<PlayerType> cbConsole; 

/** GameType object **/ 
private GameType game; 

/** PlayerType Object **/ 
private PlayerType console; 

/** Button ID's **/ 
@FXML Button cancel, addToCart; 

/** Counter for calculating total **/ 
int gameCounter; 

/** Stage for closing GUI **/ 
private Stage currentStage; 




@Override 
public void initialize(URL location, ResourceBundle resources) { 

    /** Select Console **/ 
    cbConsoleOptions = FXCollections.observableArrayList(); 
    for (PlayerType p : PlayerType.values()) { cbConsoleOptions.addAll(p); } 
    cbConsole.getItems().addAll(cbConsoleOptions); 

    /** Select Game **/ 
    cbGameOptions = FXCollections.observableArrayList(); 
    for (GameType g : GameType.values()){ cbGameOptions.addAll(g); } 
    cbGame.getItems().addAll(cbGameOptions); 

} 

public String getName(){ 
    name = nameField.getText(); 

    try { 

     String[] firstLast = name.split(" "); 
     String firstName = firstLast[0]; 
     String lastName = firstLast[1]; 

    } catch (Exception e){ 
     e.printStackTrace(); 
    } 

    return name; 
} 

public void getGame() { 
    GameType gameChoice = cbGame.getSelectionModel().getSelectedItem(); 
} 

public void getConsole() { 
    PlayerType player = cbConsole.getSelectionModel().getSelectedItem(); 
} 

public String getRentedOn() throws ParseException { 

    rentedOn = rentedOnField.getText(); 


    DateFormat format = new SimpleDateFormat("dd/MM/yyyy"); 
    Date rentedOnDate = format.parse(rentedOn); 

    Calendar cal = Calendar.getInstance(); 
    cal.setLenient(false); 
    cal.setTime(rentedOnDate); 

    try { 

     cal.getTime(); 

    } catch (Exception e) { 
     System.exit(0); 
    } 

    return rentedOn; 

} 

public String getDueBack() throws ParseException { 

    dueBack = dueBackField.getText(); 


    DateFormat format = new SimpleDateFormat("dd/MM/yyyy"); 
    Date dueBackDate = format.parse(dueBack); 

    Calendar cal = Calendar.getInstance(); 
    cal.setLenient(false); 
    cal.setTime(dueBackDate); 

    try { 

     cal.getTime(); 

    } catch (Exception e) { 
     System.exit(0); 
    } 

    return dueBack; 

} 

/************************************* 
* This is the method to call the other 
* String methods so their output can be 
* put into my main GUI 
* 
* Current problem: game.toString() and console.toString() throw an InvocationTargetException 
* @return 
* @throws ParseException 
*************************************/ 

public String storePurchaseData() throws ParseException { 
    gameCounter++;     //Problem    //Problem 
    String toList = getName() + " " + game.toString() + " " + console.toString() + " " + 
      getRentedOn() + " " + getDueBack(); 

    return toList; //Returns "null null null" 
} 


@FXML 
public void handleCancelButtonAction (ActionEvent event) { 
    currentStage = (Stage) cancel.getScene().getWindow(); 
    currentStage.close(); 
} 

@FXML 
public void addToCartButton (ActionEvent event) throws ParseException { 
    System.out.println(storePurchaseData()); 
    currentStage = (Stage) cancel.getScene().getWindow(); 
    currentStage.close(); 
}} 

列挙型ゲームタイプとPlayerType(コンソール選択)のクラス:

public enum PlayerType { 
Xbox360("Xbox 360"), 
PS4("Playstation 4"), 
XBoxOne("Xbox One"), 
WiiU("Wii - U"), 
PS3("Playstation 3"), 
Wii("Nintendo Wii"); 

private String console; 

PlayerType(String console) { this.console = console; } 

public String PlayerType() { return console; } 

@Override public String toString() { return console; }} 

ゲームタイプ:

public enum GameType { 
THE_WITCHER("The Witcher 3"), 
CALL_OF_DUTY_AW("Call of Duty: Advanced Warfare"), 
CALL_DUTY_BLOP3("Call of Duty: Black Ops 3"), 
CALL_OF_DUTY_IW("Call of Duty: Infinite Warfare"), 
THE_ELDER_SCROLLS("The Elder Scrolls IV: Skyrim"); 

private String game; 

GameType(String game) { 
    this.game = game; 
} 

public String GameType() { return game; } 

@Override public String toString() { return game; }} 

GUI Snapshot

選択された値を返します

答えて

0

唯一の方法は、getName()

あなたが何かに

public void getGame() //Void return type 
{ 
    GameType gameChoice = cbGame.getSelectionModel().getSelectedItem(); 
} 

を返さない使用されている方法の残りの部分であるこのすべてがのみアクセス可能ですGameTypeオブジェクトを作成しているんその方法の内側にある。

あなたはString使用

public String getGame() 
{ 
    return cbGame.getSelectionModel().getSelectedItem().toString(); 
} 
+0

としてそれをしたい場合は代わりにそれはそのように

public GameType getGame() //Instead of void, the type you are trying to get { return cbGame.getSelectionModel().getSelectedItem(); } 

でなければなりません、あなたがして

GameType selectedGame = getGame(); 

により結果にアクセスすることができますあなたはどのように知っているだろうそれを文字列に変換するには?私は場所に文字列の値を持っていますが、タイプGameTypeとして返す場合、それは明らかに正しく動作しません。 –

+0

@JarredParr、更新された回答を参照してください。さもなければ、 'GameType' enumに' toString() 'メソッドが実装されています... – Jonah

関連する問題