甚至可以像这样初始化数据吗? JavaFX ComboBox

时间:2017-03-10 16:19:55

标签: java javafx

我遵循MVP模式,因此Presenter可以访问视图,模型和视图到演示者。 查看< - >演示者 - >模型。

我的问题是,我无法填充ComboBox。我是一个业余爱好者,这个人在这里打破我。我想我有一个根本问题。

主要课程:

grep -f-

我试图用对象创建一个ComboBox。 人员类:

package test;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class Main extends Application
{

private Presenter presenter;
private Model model;
private ViewController view;

@Override
public void start(Stage primaryStage) throws Exception
{
    presenter = initData();
    testSampleData();

    model.showList(); // prints out: [Susi 22, Peter 30, Tom 54]

    AnchorPane paneMain = (AnchorPane) FXMLLoader.load(getClass().getResource("View.fxml"));
    Scene scene = new Scene(paneMain);

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

public void testSampleData()
{
    model.getList().add(new Person("Susi", 22));
    model.getList().add(new Person("Peter", 30));
    model.getList().add(new Person("Tom", 54));
}

private Presenter initData()
{
    presenter = new Presenter();
    model = new Model();
    view = new ViewController();

    presenter.setModel(model);
    presenter.setView(view);
    view.setPresenter(presenter);

    return presenter;
}

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

}

数据存储在模型类中:

package test;
public class Person 
{
private String name;
private int age;

public Person(String name, int alter)
{
    this.name = name;
    this.age = alter;
}

public String toString()
{
    return name + " " +  age;
}
}

主持人是"桥梁"在视图和模型之间

package test;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

public class Model
{
private ObservableList<Person> list = FXCollections.observableArrayList();

public void showList()
{
    System.out.println(this.list.toString());
}

public ObservableList<Person> getList()
{
    return list;
}

public void initTest()
{
    list.add(new Person("Susi", 22));
    list.add(new Person("Peter", 30));
    list.add(new Person("Tom", 54));
}

}

以下是观点:

package test;

import javafx.collections.ObservableList;

public class Presenter
{
private Model myModel;
private ViewController myViewController;

public void setModel(Model model)
{
    this.myModel = model;
}

public void setView(ViewController view)
{
    this.myViewController = view;
}

public ObservableList<Person> getList()
{
    return myModel.getList();
}

}

最后但并非最不重要的是,fxml:

package test;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;

public class ViewController 
{
Presenter presenter;

@FXML
private Label myLabel;

@FXML
private ComboBox<Person> myComboBox;

@FXML       
private TextArea myTextArea;

private ObservableList<Person> list;

@FXML
private void handleComboBoxAction() {
  Person selectedPerson = myComboBox.getSelectionModel().getSelectedItem();
  myTextArea.appendText("Selected Person: " + selectedPerson + "\n");
}

public ViewController()
{
//        myComboBox.setItems(presenter.getList());  // -> nullpointer
}

public void initialize()
{
    list = FXCollections.observableArrayList();

//        list = presenter.getList(); // -> this is my goal 

    list.add(new Person("Tom", 54));  // this here works, but I want to get the data from the model class :(
//        myComboBox.setItems(list);

}

public void setPresenter(Presenter presenter)
{
    this.presenter = presenter;
}
}

也许我在这里发了很多代码,但我认为这是必要的。

编辑:

我在我的主编中编辑了FXML部分:

        <?xml version="1.0" encoding="UTF-8"?>

    <?import javafx.geometry.*?>
    <?import javafx.scene.control.*?>
    <?import java.lang.*?>
    <?import javafx.scene.layout.*?>
    <?import javafx.scene.layout.AnchorPane?>

    <AnchorPane minHeight="300.0" minWidth="300.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="test.ViewController">
       <children>
          <BorderPane prefHeight="300.0" prefWidth="300.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
             <center>
                <ComboBox fx:id="myComboBox" onAction="#handleComboBoxAction" prefWidth="150.0" BorderPane.alignment="CENTER" />
             </center>
             <right>
                <TextArea fx:id="myTextArea" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
             </right>
          </BorderPane>
       </children>
    </AnchorPane>

现在NAMS和James_D的建议有效!

0 个答案:

没有答案