努力用SQLite数据填充JavaFX TableView

时间:2017-03-21 10:12:10

标签: sqlite javafx tableview

我正在尝试将SQLite数据库中的数据转换为JavaFX TableView对象,并且已经添加了行,但实际数据没有显示。我到处寻找尝试并理解如何做到这一点,但对Java来说相对较新。这是代码,如果有人可以看看和帮助。诚然,代码有点乱,因为我已经尝试了各种各样的方法来使它工作。提前谢谢......

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.stage.Stage;
import javafx.event.EventHandler;
import javafx.stage.WindowEvent;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TextField;
import javafx.scene.control.TitledPane;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.control.TableView;
import javafx.collections.ObservableList;
import javafx.collections.FXCollections;

public class MasterController {
    Stage stage = new Stage();
    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private TitledPane titledPane;

    @FXML
    private BorderPane borderPane;

    @FXML
    private TableView tableViewId;

    @FXML
    private MenuBar mainMenuId;

    @FXML
    private MenuItem closeId;

    @FXML
    private MenuItem deleteId;

    @FXML
    private MenuItem helpId;

    @FXML
    private Label searchLabelId;

    @FXML
    private TextField searchId;

    @FXML
    private Button goId;

    @FXML
    private TableColumn<Music, String> artistColId;

    @FXML
    private TableColumn<Music, String> titleColId;

    @FXML
    private TableColumn<Music, String> albumColId;

    @FXML
    private TableColumn<Music, String> genreColId;

    @FXML
    void closeClicked(ActionEvent event) {

    }

    @FXML
    void deleteClicked(ActionEvent event) {

    }

    @FXML
    void goClicked(MouseEvent event) {

    }

    @FXML
    void helpClicked(ActionEvent event) {

    }

    @FXML
    void initialize() {
        assert titledPane != null : "fx:id=\"titledPane\" was not injected: check your FXML file 'Untitled'.";
        assert borderPane != null : "fx:id=\"borderPane\" was not injected: check your FXML file 'Untitled'.";
        assert mainMenuId != null : "fx:id=\"mainMenuId\" was not injected: check your FXML file 'Untitled'.";
        assert closeId != null : "fx:id=\"closeId\" was not injected: check your FXML file 'Untitled'.";
        assert deleteId != null : "fx:id=\"deleteId\" was not injected: check your FXML file 'Untitled'.";
        assert helpId != null : "fx:id=\"helpId\" was not injected: check your FXML file 'Untitled'.";
        assert searchLabelId != null : "fx:id=\"searchLabelId\" was not injected: check your FXML file 'Untitled'.";
        assert searchId != null : "fx:id=\"searchId\" was not injected: check your FXML file 'Untitled'.";
        assert goId != null : "fx:id=\"goId\" was not injected: check your FXML file 'Untitled'.";
        assert tableViewId != null : "fx:id=\"tableViewId\" was not injected: check your FXML file 'Untitled'.";
        assert artistColId != null : "fx:id=\"artistColId\" was not injected: check your FXML file 'Untitled'.";
        assert titleColId != null : "fx:id=\"titleColId\" was not injected: check your FXML file 'Untitled'.";
        assert albumColId != null : "fx:id=\"albumColId\" was not injected: check your FXML file 'Untitled'.";
        assert genreColId != null : "fx:id=\"genreColId\" was not injected: check your FXML file 'Untitled'.";

    }

        public void prepareStageEvents(Stage stage)
    {
        System.out.println("Preparing stage events...");

        this.stage = stage;
        ObservableList<Music> targetData = FXCollections.observableArrayList();
        System.out.println(targetData);
        tableViewId.setItems(Music.showAll());
        stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
                public void handle(WindowEvent we) {
                    System.out.println("Close button was clicked!");
                    Application.terminate();
                }
            });
    }   
}

这是音乐类代码

import java.util.List;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import javafx.scene.control.TableView;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

public class Music{
    public int trackId;
    public String title;
    public String artist;
    public String album;
    public String genre;

    public Music(int trackId, String title, String artist, String album, String genre){
        this.trackId = trackId;
        this.title = title;
        this.artist = artist;
        this.album = album;
        this.genre = genre;
    }

    @Override public String toString()
    {
        return trackId + "\t" + title + "\t" + artist + "\t" + album + "\t" + genre;
    }

    public static ObservableList<Music> showAll(){
        PreparedStatement statement = Application.database.newStatement("select tracks.TrackId, tracks.Title, artist.ArtistName, album.albumTitle, album.albumGenre from tracks inner join artist on tracks.artistId = artist.artistId left outer join album on tracks.albumId = album.albumId");
        ObservableList<Music> musicData = FXCollections.observableArrayList();
        if (statement != null)      // Assuming the statement correctly initated...
        {
            ResultSet results = Application.database.runQuery(statement);       // ...run the query!

            if (results != null)        // If some results are returned from the query...
            {
                try {                               // ...add each one to the list.
                    while (results.next()) {                                               
                        musicData.add(new Music(results.getInt("TrackId"), results.getString("Title"), results.getString("ArtistName"), results.getString("albumTitle"), results.getString("albumGenre")));
                    }
                }
                catch (SQLException resultsexception)       // Catch any error processing the results.
                {
                    System.out.println("Database result processing error: " + resultsexception.getMessage());
                }
            }
        }
        return musicData;
    }
}

0 个答案:

没有答案