JavaFX,如何填写表格

时间:2016-02-14 22:18:35

标签: java javafx

我在MS SQL server中有一个包含4列的表:idchannelmovietime。四个可能的行:HBO|Furious 7|12:15HBO|Wimbledon|11:25FOX|The Simpsons|7:00FOX|Lucifer|9:01。我有一个名为guide的课程:

public class guide {
private String channel;
private String movie;
private LocalTime time;
(...)

一个名为gui的课程:

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.util.Callback;
import org.joda.time.LocalTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class gui extends Application {

    Stage stage;
    Scene sceneMain;

    int nrOfChannels;
    List<String> channelsOnMain;
    @Override
    public void start(Stage primaryStage) throws Exception {

        TableView<guide> tableMain = new TableView<>(loadMain());

        List<TableColumn<guide, String>> colChannelList = new ArrayList<>();
        for (int i = 0; i < nrOfChannels; i++) {
            colChannelList.add(new TableColumn<guide, String>(channelsOnMain.get(i)));
        }

        for(int i = 0; i < colChannelList.size(); i++){
            colChannelList.get(i).setCellValueFactory(new PropertyValueFactory<guide, String>("movie"));
        }

        for(int i = 0; i < colChannelList.size(); i++)
            tableMain.getColumns().add(colChannelList.get(i));


        BorderPane paneMain = new BorderPane(tableMain);
        sceneMain = new Scene(paneMain);

        stage = primaryStage;
        stage.setScene(sceneMain);
        stage.setTitle("TVguide");
        stage.show();
    }

    public ObservableList<guide> loadMain(){
        ObservableList<guide> mainList = FXCollections.observableArrayList();
        int numberOfChann = 0;
        try {
            handleDB dbMain = new handleDB();
            Connection myConnection = dbMain.connect_with_DB();
            String query = "SELECT * FROM main";
            ResultSet mainFromDB = dbMain.getFromSQL(myConnection, query);
            String tempChannel = "";
            channelsOnMain = new ArrayList<>();
            while (mainFromDB.next()) {
                if(mainFromDB.getString(3).toString() != null && !mainFromDB.getString(3).toString().isEmpty()) {
                    if(!mainFromDB.getString(2).equals(tempChannel)){
                        numberOfChann++;
                        tempChannel = mainFromDB.getString(2);
                        channelsOnMain.add(tempChannel);
                    }
                    mainList.add(new guide(mainFromDB.getString(2), mainFromDB.getString(3), new LocalTime(mainFromDB.getString(4))));//2-channel,3-movie,4-time
                }
            }
        } catch(Exception ex){
            ex.printStackTrace();
        }
        nrOfChannels = numberOfChann;
        return mainList;
    }
}

问题是ObservableList<guide>方法返回了loadMain(),所以

colChannelList.get(i).setCellValueFactory(new PropertyValueFactory<guide, String>("movie"));

它将每部电影放在每一列中,这是合乎逻辑的,但不是我的观点。是否可以改变这个

for(int i = 0; i < colChannelList.size(); i++){
    colChannelList.get(i).setCellValueFactory(new PropertyValueFactory<guide, String>("movie"));
}

为了拥有属于具有特定colChannelList.get(i)的对象的movie channel列属性? 简而言之,我希望在HBOFurious 7Wimbledon以及FOX列中有The SimpsonsLucifer的表格,而不是列fe HBO每部4部电影都有可能吗?

0 个答案:

没有答案