簡體   English   中英

從Java fx中的其他類更新標簽

[英]updating labels from other classes in java fx

對於JavaFX非常新的東西,並且在控制器的工作方式方面缺乏一點知識,但是事情就這樣了。

我的問題很簡單。 我需要在運行時更新屏幕上的Label

此問題已在以下站點上得到解決:

Java FX更改標簽文本

Java FX更改標簽文本2

傳遞參數

此外,這些鏈接描述的是同一事物,但執行方式不同嗎?

但是我的程序有些不同。

該程序的流程如下:

主舞台有幾個Objects ,這些Objects擴展了內部帶有Label Pane 可以右鍵單擊這些Objects ,這將打開一個上下文菜單。 上下文菜單中的一個選項將打開一個帶有RadioButtons的新窗口。

這個想法是選擇一個RadioButtons並使用該字符串在主舞台上改寫Label

但是,我的代碼第一次只能運行一次。 屏幕上未顯示所有后續更改。 我什至可以將已更改的Label輸出到Console ,它顯示正確的值,但是從不在Stage上更新Label

屏幕上具有標簽的類:

import javafx.scene.control.Label;
import javafx.scene.layout.Pane;

public class CoursePane extends Pane {

    private Label courseID;

    public CoursePane(Label courseID) {

        this.courseID = courseID;
    }

    public String getCourseID() {

        return courseID.getText();
    }

    public Label getCourseLabel() {

        return courseID;
    }

    public void setCourseID(String ID) {

        courseID.setText(ID);   
    }
}

調用菜單的上下文菜單類:

public class CourseContext {


    static String fxmlfile;
    private static Object paneSrc; //the CoursePane that was clicked on

    public static void start(CoursePane pane, String courseSrc) {

        //Context Menu
        ContextMenu contextMenu = new ContextMenu();

        //MenuItems
        MenuItem item4 = new MenuItem("option");

        //add items to context menu
        contextMenu.getItems().addAll(item4);

        pane.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if (event.isSecondaryButtonDown()) {

                    //the coursePane that was right clicked on
                    paneSrc = event.getSource().toString();

                    contextMenu.show(pane, event.getScreenX(), event.getScreenY());

                    item4.setOnAction(new EventHandler<ActionEvent>() {

                        @Override
                        public void handle(ActionEvent event) {

                            try {

                                FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("my fxml file for the radio Buttons"));

                                Parent root= loader.load();

                                ElectiveController electiveController = loader.getController();

                                electiveController.start( "pass the coursePane that was right clicked on" );

                                Scene scene = new Scene(root);

                                Stage stage = new Stage();

                                stage.setScene(scene);

                                stage.setTitle("Set Elective");

                                stage.show();

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

最后,應該將具有Label值的類設置為:

public class ElectiveController {

    @FXML
    private Button setButton;

    private RadioButton chk;

    //the pane that was right clicked on
    private static String courseSource;

    public void start(Course courseSrc) { //courseSrc: the Pane you right clicked on

        courseSource = courseSrc.getCoursenamenumber().getValue();

    }//end start

    //sets the course pane with the selected elective radio button
    @FXML
    private void setElective() {

        chk = (RadioButton)humElectiveGroup.getSelectedToggle();

        //This is supposed to set the value for the coursePane Object to show on the screen!
        MainStage.getCoursePanes().get(courseSource).setCourseID(chk.getText());

        Stage stage = (Stage) setButton.getScene().getWindow();

        stage.close();
    }
}

我研究了依賴項注入,嘗試了綁定和傳遞參數,但是得到了相同的結果。 我知道這很簡單,我們將不勝感激! 謝謝。

這是如何連接不同零件的技巧
-可以將其復制粘貼到單個文件中並調用。
-請注意,這並不代表或模擬您的應用程序。 旨在演示該問題的解決方案(非常簡單)

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

//main class
public class UpdateViewByMenu extends Application {

    private Controller controller;

    @Override
    public void start(Stage stage) throws Exception {

        BorderPane root = new BorderPane();
        controller = new Controller();
        root.setTop(controller.getMenu());
        root.setBottom(controller.getView());
        Scene scene = new Scene(root, 350,200);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) { launch(args);}
}

//controller which "wires" view to model
class Controller {

    private Model model;
    private View view;
    private TopMenu menu;

    public Controller() {
        model = new Model();
        view = new View();
        menu = new TopMenu();
        //wire up menu to model : menu changes update model
        menu.getMenuTextProperty().addListener(
                e-> model.setCourseID(menu.getMenuTextProperty().get()));
        //wire model to view: change in model update view
        view. geLabelTextProerty().bind(model.getCourseIDProperty());
        //set initial value to show
        menu.getMenuTextProperty().set("Not set");
    }

    Model     getModel() {return model;}
    Pane      getView()  { return view;}
    MenuBar  getMenu()  { return menu; }
}

//model which represent the data, in this case label info
class Model{

    SimpleStringProperty courseIdProperty;
    Model(){
        courseIdProperty = new SimpleStringProperty();
    }

    StringProperty getCourseIDProperty() {

        return courseIdProperty;
    }

    void setCourseID(String id) {

        courseIdProperty.set(id);
    }
}

//represents main view, in this case a container for a label
class View extends HBox {

    private Label courseID;

    View() {
        courseID = new Label();
        getChildren().add(courseID);
    }

    StringProperty geLabelTextProerty() {

        return courseID.textProperty();
    }
}

//menu
class TopMenu extends MenuBar{

    SimpleStringProperty menuTextProperty;

    TopMenu() {
        menuTextProperty = new SimpleStringProperty();
        Menu menu = new Menu("Select id");
        MenuItem item1 =  getMenuItem("10021");
        MenuItem item2 =  getMenuItem("10022");
        MenuItem item3 =  getMenuItem("10023");
        MenuItem item4 =  getMenuItem("10024");
        menu.getItems().addAll(item1, item2, item3, item4);
        getMenus().add(menu);
    }

    MenuItem getMenuItem(String text) {

        MenuItem item =  new MenuItem(text);
        item.setOnAction(e -> menuTextProperty.set(item.textProperty().get()));
        return item;
    }

    StringProperty getMenuTextProperty() {

        return menuTextProperty;
    }
}

請隨時根據需要進行澄清。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM