管理控制器内的数据

时间:2016-02-12 02:37:14

标签: javafx tableview

当我尝试在GUI的控制器类中管理数据时,我遇到了问题。

1)我发现在将数据检索到TableView时遇到了麻烦。

2)我试图研究JavaFX应用程序的一般设计,但我还没有真正得到创建这类东西的“标准”方法。我是否遵循标准模式,我有控制器和主要?我是否应该只使用一个控制器来使这更容易(但代码更麻烦?)

主要

public class TrackPlayerMain extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        BorderPane root = new BorderPane();
        FXMLLoader trackPlayerLoader = new FXMLLoader(getClass().getResource("TrackPlayer.fxml"));
        root.setCenter(trackPlayerLoader.load());
        TrackPlayerController trackPlayerController = trackPlayerLoader.getController();

        DataModel model = new DataModel();
        trackPlayerController.initData(model);

        Scene scene = new Scene(root, 600, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

数据

public class DataModel {
    private final ObservableList<Track> trackList = FXCollections.observableArrayList(track -> 
        new Observable[] {
            track.trackTitleProperty(), 
            track.trackArtistProperty(),
            track.trackGenreProperty()});
    private final ObjectProperty<Track> currentTrack = new SimpleObjectProperty<>(null);

    public ObjectProperty<Track> currentTrackProperty() {
        return currentTrack;
    }
    public final Track getCurrentTrack() {
        return currentTrackProperty().get();
    }
    public final void setCurrentTrack(Track track) {
        currentTrackProperty().set(track);
    }
    public ObservableList<Track> getTrackList() {
        return trackList;
    }
    public void loadData() { // Database as parameter?
        trackList.setAll(
                new Track("Track1", "Artist1", "Genre1"),
                new Track("Track2", "Artist2", "Genre2"),
                new Track("Track3", "Artist3", "Genre3"),
                new Track("Track4", "Artist4", "Genre4")
        );
    }
    public void saveData() { } // Database as parameter?
}

轨道

public class Track {
    public Track(String trackTitle, String trackArtist, String trackGenre) {
        setTrackTitle(trackTitle);
        setTrackArtist(trackArtist);
        setTrackGenre(trackGenre);
    }
    // -----------------------------------------------------
    private final StringProperty trackTitle = new SimpleStringProperty();

    public final StringProperty trackTitleProperty() {
        return this.trackTitle;
    }
    public final String getTrackTitle() {
        return this.trackTitleProperty().get();
    }
    public final void setTrackTitle(final String trackTitle) {
        this.trackTitleProperty().set(trackTitle);
    }
    // -----------------------------------------------------
    private final StringProperty trackArtist = new SimpleStringProperty();

    public final StringProperty trackArtistProperty() {
        return this.trackArtist;
    }
    public final String getTrackArtist() {
        return this.trackArtistProperty().get();
    }
    public final void setTrackArtist(final String trackArtist) {
        this.trackArtistProperty().set(trackArtist);
    }   
    // -----------------------------------------------------
    private final StringProperty trackGenre = new SimpleStringProperty();

    public final StringProperty trackGenreProperty() {
        return this.trackGenre;
    }
    public final String getTrackGenre() {
        return this.trackGenreProperty().get();
    }
    public final void setTrackGenre(final String trackGenre) {
        this.trackGenreProperty().set(trackGenre);
    }
    // -----------------------------------------------------
}

跟踪播放器控制器

public class TrackPlayerController implements Initializable {
    // -----------------------------------------------------
    @FXML private Button remove;
    @FXML private Button removeAll;
    @FXML private Button search;
    // -----------------------------------------------------
    @FXML private TableView<Track> playingTable;
    @FXML private TableColumn<Track, String> playingTitleCol;
    @FXML private TableColumn<Track, String> playingArtistCol;
    @FXML private TableColumn<Track, String> playingGenreCol;
    // -----------------------------------------------------
    @FXML private Button play;
    @FXML private Button pause;
    @FXML private Button rw;
    @FXML private Button ff;
    @FXML private Button reset;
    // -----------------------------------------------------
    @FXML private Label runTime;
    // -----------------------------------------------------
    private DataModel model;

    @Override
    public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
        playingTitleCol.setCellValueFactory(new PropertyValueFactory<>("titleName"));
        playingArtistCol.setCellValueFactory(new PropertyValueFactory<>("artistName"));
        playingGenreCol.setCellValueFactory(new PropertyValueFactory<>("genreName"));
    }
    @FXML private void play(ActionEvent e) {
        System.out.println("Play pressed");
    }
    @FXML private void pause(ActionEvent e) {
        System.out.println("Pause");
    }
    @FXML private void remove(ActionEvent e) {
    }
    @FXML private void removeAll(ActionEvent e) {
    }
    @FXML private void search(ActionEvent e) throws IOException{
    }

    void initData(DataModel model) {
        if(this.model != null) {
            throw new IllegalStateException("Data has already been initialized");
        }
        this.model = model;
        playingTable.setItems(model.getTrackList());
    }
}

0 个答案:

没有答案