JavaFX中的TableView分页

时间:2017-01-04 16:33:35

标签: java javafx pagination scenebuilder

我正在为一个程序开发JavaFX接口,我正在尝试为表添加分页。

这是我的控制器代码的一部分(我在添加时省略的是我界面中某些按钮的句柄)

public class AllController {

private RaportPDF wrpdf;

private final static int rowsPerPage = 10;

public void updateSarcina(Observable<Sarcina> observable) 
{
    SarcinaService service = (SarcinaService)observable;
    smodel.setAll(service.getAllSarcinas());
}
public void updatePost(Observable<Post> observable) 
{
    PostService service = (PostService)observable;
    pmodel.setAll(service.getAllPosts());
}
public void updateFisa(Observable<Elementfisa> observable) 
{
    FisaService service = (FisaService)observable;
    fmodel.setAll(service.getAllFisa());
}

public Observer<Sarcina> getSarcinaObserver() 
{
    return new Observer<Sarcina>() 
    {
        @Override
        public void update(Observable<Sarcina> observable) 
        {
            updateSarcina(observable);
        }
    };
}
public Observer<Post> getPostObserver() 
{
    return new Observer<Post>() 
    {
        @Override
        public void update(Observable<Post> observable) 
        {
            updatePost(observable);
        }
    };
}
public Observer<Elementfisa> getFisaObserver() 
{
    return new Observer<Elementfisa>() 
    {
        @Override
        public void update(Observable<Elementfisa> observable) 
        {
            updateFisa(observable);
        }
    };
}


@SuppressWarnings("rawtypes")
@FXML private TableView allTable;

private TableColumn<Sarcina, String> sFirstColumn;
private TableColumn<Sarcina, String> sSecondColumn;
private TableColumn<Post, String> pFirstColumn;
private TableColumn<Post, String> pSecondColumn;
private TableColumn<Elementfisa, String> fFirstColumn;
private TableColumn<Elementfisa, String> fSecondColumn;

@FXML private ComboBox<String> ComboObject;

@FXML private Label firstLabel;
@FXML private Label secondLabel;
@FXML private Label thirdLabel;

@FXML private TextField firstTextField;
@FXML private TextField secondTextField;
@FXML private TextField thirdTextField;
@FXML private TextField filterTextField;

@FXML private RadioButton radioButtonFirst;
@FXML private RadioButton radioButtonSecond;
@FXML private Button addButton;
@FXML private Button updateButton;
@FXML private Button deleteButton;
@FXML private Button clearFieldsButton;
@FXML private Button raportButton;
@FXML private Pagination pagination ;
SarcinaService sservice;
PostService pservice;
FisaService fservice;

ObservableList<Sarcina> smodel;
ObservableList<Post> pmodel;
ObservableList<Elementfisa> fmodel;

private String currentComboBoxString;

private Boolean isSelectedFC;
private Boolean isSelectedSC;

ToggleGroup toggleRadioGroup = new ToggleGroup();

public AllController() 
{

}
private int intValidate(String e) 
{
    for(int i = 0; i < e.length(); i++) 
    {
        if(i == 0 && e.charAt(i) == '-') 
        {
            if(e.length() == 1) 
            {
                showErrorMessage("Numar invalid !");
                return -1;
            }
            else continue;
        }
        if(Character.digit(e.charAt(i), 10) < 0) 
        {
            showErrorMessage("Numar invalid !");
            return -1;
        }
    }
    return Integer.parseInt(e);
}
@SuppressWarnings("unchecked")
private void fillItemsOnTable(boolean justColumns) 
{
    @SuppressWarnings("rawtypes")
    ObservableList localModel = null;
    if (currentComboBoxString.equals("Sarcina")) 
    {
        allTable.getColumns().clear();
        allTable.getColumns().add(sFirstColumn);
        allTable.getColumns().add(sSecondColumn);
        localModel = this.smodel;
    }
    else if (currentComboBoxString.equals("Post")) 
    {
        allTable.getColumns().clear();
        allTable.getColumns().add(pFirstColumn);
        allTable.getColumns().add(pSecondColumn);
        localModel = this.pmodel;
    }
    else if (currentComboBoxString.equals("Fisa")) 
    {
        allTable.getColumns().clear();
        allTable.getColumns().add(fFirstColumn);
        allTable.getColumns().add(fSecondColumn);
        localModel = this.fmodel;
    }
    if (!justColumns)
        allTable.setItems(localModel);
}

@FXML public void handleRaport()
{
    if (isSelectedFC) 
    {
        ObservableList<Sarcina> model = FXCollections.observableArrayList(fservice.filterRapoarte());
        ComboObject.setValue("Sarcina");
        this.fillItemsOnTable(true);
        allTable.setItems(model);
        wrpdf.addPdf(model);
    }


}

public void setService(SarcinaService sservice, PostService pservice, FisaService fservice) 
{
    this.sservice = sservice;
    this.pservice = pservice;
    this.fservice = fservice;

    this.smodel = FXCollections.observableArrayList(sservice.getAllSarcinas());
    this.pmodel = FXCollections.observableArrayList(pservice.getAllPosts());
    this.fmodel = FXCollections.observableArrayList(fservice.getAllFisa());

    this.fillItemsOnTable(false);
}
@FXML private void onActionComboBox(ActionEvent event) 
{
    String current = ComboObject.getSelectionModel().getSelectedItem();
    if (current.compareTo(currentComboBoxString) != 0) 
    {
        currentComboBoxString = current;
        if (current.equals("Sarcina")) 
        {
            secondLabel.setText("Desc: ");
            radioButtonSecond.setText("By Desc");
            thirdLabel.setVisible(false);
            radioButtonFirst.setVisible(false);
            thirdTextField.setVisible(false);
        }
        else if (current.equals("Post")) 
        {
            secondLabel.setText("Name: ");
            thirdLabel.setText("Type: ");
            radioButtonFirst.setText("By Name");
            radioButtonSecond.setText("By Type");
            thirdLabel.setVisible(true);
            radioButtonFirst.setVisible(true);
            thirdTextField.setVisible(true);
        }
        else if (current.equals("Fisa")) 
        {
            secondLabel.setText("Sarcina ID: ");
            thirdLabel.setText("Post ID: ");
            radioButtonFirst.setText("By Sarcina");
            radioButtonSecond.setText("By Post");
            thirdLabel.setVisible(true);
            radioButtonFirst.setVisible(true);
            thirdTextField.setVisible(true);
        }
        this.fillItemsOnTable(false);
    }
}

//pagination;

public int itemsPerPage() {
    return 1;
}

public int rowsPerPage() {
    return 5;
}

@SuppressWarnings("unchecked")
private Node createPage(int pageIndex) 
{
    int fromIndex = pageIndex * rowsPerPage;
    int toIndex = Math.min(fromIndex + rowsPerPage, data.size());
    allTable.setItems(FXCollections.observableArrayList(data.subList(fromIndex, toIndex)));

    return new BorderPane(allTable);
}

//pagination


@SuppressWarnings("unchecked")
@FXML private void initialize() 
{
    ComboObject.getItems().addAll(
            "Sarcina",
            "Post",
            "Fisa"
        );
    currentComboBoxString = "Sarcina";
    ComboObject.setValue("Sarcina");

    sFirstColumn = new TableColumn<>("ID");
    sSecondColumn = new TableColumn<>("Desc");
    pFirstColumn = new TableColumn<>("Name");
    pSecondColumn = new TableColumn<>("Type");
    fFirstColumn = new TableColumn<>("Sarcina");
    fSecondColumn = new TableColumn<>("Post");

    sSecondColumn.setCellValueFactory(new PropertyValueFactory<Sarcina, String>("Desc"));
    sFirstColumn.setCellValueFactory(new  PropertyValueFactory<Sarcina, String>("Id"));     
    pFirstColumn.setCellValueFactory(new PropertyValueFactory<Post, String>("Nume"));
    pSecondColumn.setCellValueFactory(new PropertyValueFactory<Post, String>("Tip"));



    fFirstColumn.setCellValueFactory(new Callback<CellDataFeatures<Elementfisa, String>, ObservableValue<String>>()
    {
         public ObservableValue<String> call(CellDataFeatures<Elementfisa, String> p) 
         {
             if (p.getValue() != null && p.getValue().getSarcina() != null) 
             {
                  return new SimpleStringProperty(p.getValue().getSarcina().getDesc());
             } else 
             {
                 return new SimpleStringProperty("Empty");
             }
         }
      });
    fSecondColumn.setCellValueFactory(new Callback<CellDataFeatures<Elementfisa, String>, ObservableValue<String>>() 
    {
         public ObservableValue<String> call(CellDataFeatures<Elementfisa, String> p) 
         {
             if (p.getValue() != null && p.getValue().getPost() != null) 
             {
                  return new SimpleStringProperty(p.getValue().getPost().getNume());
             } else 
             {
                 return new SimpleStringProperty("Empty");
             }
         }
      });
    isSelectedFC = true;
    isSelectedSC = false;
    radioButtonFirst.setToggleGroup(toggleRadioGroup);
    radioButtonSecond.setToggleGroup(toggleRadioGroup);
    radioButtonFirst.setSelected(true);

    allTable.getSelectionModel().selectedItemProperty().addListener((obs, oldSelection, newSelection) -> {
        if (newSelection != null) 
        {
            if (currentComboBoxString.equals("Sarcina")) 
            {
                firstTextField.setText(((Sarcina) newSelection).getId().toString());
                thirdTextField.setText("N/A");
                secondTextField.setText(((Sarcina) newSelection).getDesc());
            }
            else if (currentComboBoxString.equals("Post")) 
            {
                firstTextField.setText(((Post) newSelection).getId().toString());
                secondTextField.setText(((Post) newSelection).getNume());
                thirdTextField.setText(((Post) newSelection).getTip());
            }
            else if (currentComboBoxString.equals("Fisa")) 
            {
                firstTextField.setText(((Elementfisa) newSelection).getId().toString());
                secondTextField.setText(((Elementfisa) newSelection).getSarcina().getId().toString());
                thirdTextField.setText(((Elementfisa) newSelection).getPost().getId().toString());
            }
        }
    });
}

private void clearFields() 
{
    firstTextField.setText("");
    secondTextField.setText("");
    thirdTextField.setText("");
}

这是我和#34; main&#34;的代码。类:

public class PrimaryLayoutMain extends Application {

AnchorPane apview;
BorderPane rootLayout;
Stage primaryStage;

SarcinaXMLRepo srep;
SarcinaService sservice;
PostXMLRepo prep;
PostService pservice;
ElementFisaRepo frep;
FisaService fservice;

FXMLLoader loader;

@Override
public void start(Stage primaryStage) {
    srep = new SarcinaXMLRepo(new ValidatorSarcina(), "SarcinaXMLRepo.xml");
    sservice = new SarcinaService(srep);
    prep = new PostXMLRepo(new ValidatorPost(), "PostXMLRepo.xml");
    pservice = new PostService(prep);
    frep = new ElementFisaRepo(new ValidatorEF());
    fservice = new FisaService(frep);

    this.primaryStage = primaryStage;
    this.primaryStage.setTitle("Management de sarcini.");

    loader = new FXMLLoader();

    initRootLayout();        
    initView();       
}

private void initView() {
    try {
        String pathToFxml = "src/fisapost/view/PrimaryLayout.fxml";
        URL fxmlUrl = new File(pathToFxml).toURI().toURL();
        loader.setLocation(fxmlUrl);
        apview = (AnchorPane) loader.load();
        rootLayout.setCenter(apview);

        AllController viewCtrl = loader.getController();
        viewCtrl.setService(sservice, pservice, fservice);
        sservice.addObserver(viewCtrl.getSarcinaObserver());
        pservice.addObserver(viewCtrl.getPostObserver());
        fservice.addObserver(viewCtrl.getFisaObserver());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void initRootLayout() {
    try {

        FXMLLoader loader = new FXMLLoader();
        String pathToFxml = "src/fisapost/view/RootLayout.fxml";
        URL fxmlUrl = new File(pathToFxml).toURI().toURL();
        loader.setLocation(fxmlUrl);
        rootLayout = (BorderPane) loader.load();


        Scene scene = new Scene(rootLayout);
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}

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

}

我发现this solution非常有用,但我无法在我的程序中正确实现它。首先,我的主类和控制器类是分开的,我不会在代码的开头手动初始化带有元素的数据数组(我的ObservableList数组的数据是从文件中读取的)。enter image description here

运行代码后的界面如下所示。如何为每个表添加分页,因为我还有一个ComboBox在我选择不同的值时更改表?我还在整个界面中使用了SceneBuilder。

0 个答案:

没有答案