我可以使用文本值来选择Toggle()吗?



我正在使用文本文件将信息加载到javafx gui。 有没有办法在那里使用文本值来选择切换组中的单选按钮。

我认为"切换组.selectedValue(切换值("是我需要的功能,但它不需要字符串。有没有办法间接将字符串转换为切换值?

以下内容不起作用,因为"selectToggle(("采用切换而不是文本值,并且隐式和显式"(切换("转换似乎都不起作用。

tgrpSex.selectToggle(read.nextLine());

这应该是可重现的:

package programmingassignment1;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextArea;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.*;
//import javafx.scene.layout.StackPane;
//import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import java.io.*; //input/output
import java.util.Scanner;
//import java.util.*; //scanner, user input
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
//import javafx.scene.shape.Rectangle;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
public class Address extends Application {

RadioButton rbMale = new RadioButton("Male");
RadioButton rbFemale = new RadioButton("Female");
ToggleGroup tgrpSex = new ToggleGroup();

GridPane rootPane = new GridPane();

@Override
public void start(Stage primaryStage){
//Setting an action for the Open Contact  button       
Button btOpenContact = new Button("Open Contact");
File file = new File("AddressBook.txt");
btOpenContact.setOnAction(event -> {
try {
openContact(file);
} catch (Exception e) {
e.printStackTrace();
}
});
//Setting an action for the Save button        
Button btSave = new Button("Save");
btSave.setOnAction(
new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent e){
try{saveContact(file);}
catch(Exception f){f.getMessage();}
}});

//associate radio buttons with a toggle group
rbMale.setToggleGroup(tgrpSex);
rbFemale.setToggleGroup(tgrpSex);
rbMale.setOnAction(e -> {
if(rbMale.isSelected()){int maleContact = 1;}
});
rbFemale.setOnAction(e -> {
if(rbFemale.isSelected()){int maleContact = 0;}
});
rootPane.add(new Label("Sex"), 3, 1);
rootPane.add(rbFemale, 3, 2);
rootPane.add(rbMale, 3, 3);       
rootPane.add(btOpenContact, 1, 13);
Scene scene = new Scene(rootPane, 1000, 500);
primaryStage.setTitle("Address Book");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
public void saveContact(File file) throws FileNotFoundException, Exception{ //declaration
//this code might cause a FileNotFoundException
//if it does it creates an exception object of the above type
try{
//PrintWriter output = new PrintWriter (file);
PrintStream output = new PrintStream(file);
output.println(tfContactFirst.getText());
output.println(tfContactLast.getText());
output.println(tfSpouseFirst.getText());
output.println(tfSpouseLast.getText());
output.println(cboWorkHome.getValue());
output.println(tfStreet.getText());
output.println(tfCity.getText());
output.println(tfState.getText());
output.println(tfZip.getText());
output.close();
}
//what do do with exception
//here the catch clause with create another exception
//that is passed the result of the getMessage() method from the original exception
catch(FileNotFoundException e){
throw new Exception(e.getMessage());
}
}
//read same text file you save too
public void openContact(File file) throws FileNotFoundException, Exception{
try{
Scanner read = new Scanner(file);     
while(read.hasNextLine()){
//how do I save the imageFileName
tfContactFirst.setText(read.nextLine());
tfContactLast.setText(read.nextLine());
tgrpSex.selectToggle(read.nextLine());
tfSpouseFirst.setText(read.nextLine());
tfSpouseLast.setText(read.nextLine());
//tfSpouseGender.setText(read.nextLine());
cboWorkHome.setValue(read.nextLine());
tfStreet.setText(read.nextLine());
tfCity.setText(read.nextLine());
tfState.setText(read.nextLine());
tfZip.setText(read.nextLine());
//taNotes.setText(read.nextLine());
}
}
catch(FileNotFoundException e){
throw new Exception(e.getMessage());
}
}
}

无结果 <- 语法错误

对不起。我得到了答案。我不知道切换类型对象是什么。我查找了selectToggle((的示例,并了解到您可以将单选按钮对象传递给它。所以我把它放在一个if-then语句中。if(read.nextLine().equals("Male")){tgrpSex.selectToggle(rbMale);} else{tgrpSex.selectToggle(rbFemale);}

最新更新