在每个切换按钮javafx的hashmap中保存textarea值

时间:2016-12-24 22:47:40

标签: java javafx hashmap toggle

当我单击切换按钮并输入文本然后按相同的切换按钮 - (开/关)将textarea文本保存在带有按钮ID的hashmap中,但是当我按下切换按钮并向其添加文本时textarea并按下其他切换按钮而不是按下相同的值,该值不会添加到hashmap并且textarea文本会丢失。

这是我的代码,请咨询

Node componentsPane;
int x = 14, y = 0;
ToggleGroup group = new ToggleGroup();
Integer id = 1;
String temp;
String tempmessageText = null;

public void initialize() throws SQLException {

    componentsPane = splitPane.getItems().get(1);

    final Statement stmt = null;
    String m = "";

    final String query = "select username from chatuser ";
    final Connection conn = null;

    final Connection _connection = DriverManager.getConnection("jdbc:postgresql://IP/postgres", "postgres",
            "password");
    @SuppressWarnings("resource")
    final PreparedStatement _stmt = _connection.prepareStatement(query);
    final ResultSet res = _stmt.executeQuery();
    while (res.next()) {
        m = res.getString("username");
        final ToggleButton btn = new ToggleButton();
        btn.setLayoutX(x);
        btn.setLayoutY(y + 180);
        btn.setText(m);
        btn.setId(id.toString());
        btn.setToggleGroup(group);
        splitPaneLeft.getChildren().add(btn);
        btn.setOnAction(new EventHandler<ActionEvent>() {
            HashMap<Integer, String> hmap = new HashMap<Integer, String>();

            @Override
            public void handle(final ActionEvent event) {
                System.out.println(btn.getId());

                if (btn.isSelected()) {

                    temp = btn.getId();
                    System.out.println("yes");

                    splitPane.getItems().remove(componentsPane);

                    splitPane.getItems().add(1, componentsPane);

                    smsBox.setText(hmap.get(Integer.parseInt(temp)));

                    Main.getPrimaryStage().setMaxWidth(840);
                    Main.getPrimaryStage().setWidth(810);
                    Main.getPrimaryStage().setMinWidth(400);

                } else {
                    System.out.println("no");

                    splitPane.getItems().remove(componentsPane);

                    Main.getPrimaryStage().setResizable(false);
                    Main.getPrimaryStage().setMaxWidth(400);
                    System.out.println(smsBox.getText());
                    hmap.put(Integer.parseInt(btn.getId()), smsBox.getText());
                }

            }
        });
        y += 35;
        id++;

    }

}

boolean k;

@FXML
private void func() {
    System.out.println(group.getSelectedToggle());

    if (!group.getSelectedToggle().equals(null)) {

        splitPane.getItems().add(1, componentsPane);
        splitPane.setDividerPosition(1, 0.1);

        Main.getPrimaryStage().setMaxWidth(840);
        Main.getPrimaryStage().setWidth(810);
        Main.getPrimaryStage().setMinWidth(400);

    }

    else {
        System.out.println("false");
        System.out.println(splitPane.getItems());

        System.out.println(Main.getPrimaryStage().getHeight());

    }
}

1 个答案:

答案 0 :(得分:0)

当同一组中的另一个onAction被选中时,ToggleButton未被选中的Toggle未被触发。

此外,将数据存储在Map似乎是不必要的。您为每个按钮创建一个EventHandler<ActionEvent>,永远不会修改按钮的id,只会写入与按钮ID相对应的按键。一个字段会做同样的伎俩。

但我建议您在ChangeListener添加ToggleGroup,这样您就可以获得未选中的Toggle和在{1}中选择的Toggle单一事件。此外,将id添加为userData允许您保留类型而不是转换为字符串并从字符串解析......

@Override
public void start(Stage primaryStage) {
    List<String> strings = Arrays.asList("a", "b", "c");
    ToggleGroup group = new ToggleGroup();

    TextArea textArea = new TextArea();
    textArea.setEditable(false);
    VBox root = new VBox(textArea);

    int id = 0;

    Map<Integer, String> data = new HashMap<>();

    for (String m : strings) {
        ToggleButton btn = new ToggleButton(m);
        btn.setUserData(id++);
        btn.setToggleGroup(group);
        root.getChildren().add(btn);
    }

    // listen to selection of a different toggle to store data for the old toggle /
    // display stored data for the new toggle
    group.selectedToggleProperty().addListener((observable, oldValue, newValue) -> {
        textArea.setEditable(newValue != null);
        if (oldValue != null) {
            data.put((Integer) oldValue.getUserData(), textArea.getText());
        }
        if (newValue != null) {
            textArea.setText(data.get(newValue.getUserData()));
        }
    });

    Scene scene = new Scene(root);

    primaryStage.setScene(scene);
    primaryStage.show();
}