javafx scenebuilder中的mysql分页和tableview

dwthyt8l  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(341)

我的表中有太多的字段,因此我将表分为两个第一表和第二表…我的问题是我想在两个表中都放置一个分页,这样如果我决定通过按分页将充满记录的表移到下一个表,那么我的所有表都将一起移动。我甚至尝试给我的两个表赋予不同的id,但是如果我尝试在两个表上设置分页,什么都不会显示,,但是如果我试着只把它放在一个表上,它可以正常工作,现在我已经尝试给两个表相同的id,但它仍然只影响一个是我的代码中有一个错误。(我是一个新的分页类的东西,所以我有一个小问题,它。我不知道如何应用scrollpane到我的表,所以我需要通过这个方法在其他地方去使我的两张table上满是一个人记录需要移到另一个人记录(请帮助)
这是我的密码 enter code here 场景fxml代码

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

<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="419.0" prefWidth="1402.0" styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="hotel.management.system.AliennavigationController">
    <stylesheets>
        <URL value="@aliennavigation.css" />
    </stylesheets>
   <children>
      <Pagination fx:id="pagination" layoutX="-1.0" layoutY="213.0" prefHeight="208.0" prefWidth="1388.0" />
      <TableView fx:id="paginationTableview" layoutX="2.0" layoutY="7.0" prefHeight="166.0" prefWidth="1388.0" style="-fx-background-color: blue;" styleClass="tbTable-row-cell-white">
         <columns>
            <TableColumn fx:id="nameofhotel" prefWidth="84.0" text="nameofhotel" />
            <TableColumn fx:id="surname" prefWidth="66.0" text="surname" />
            <TableColumn fx:id="othernames" prefWidth="81.0" text="othernames" />
            <TableColumn fx:id="nationality" prefWidth="79.0" text="nationality" />
            <TableColumn fx:id="telephone" prefWidth="90.0" text="telephone" />
            <TableColumn fx:id="profession" prefWidth="95.0" text="profession" />
            <TableColumn fx:id="maritalstatus" prefWidth="85.0" text="maritalstatus" />
            <TableColumn fx:id="nameofspouse" prefWidth="101.0" text="nameofspouse" />
            <TableColumn fx:id="numberofchildren" prefWidth="108.0" text="numberofchildren" />
            <TableColumn fx:id="modeoftravel" prefWidth="87.0" text="modeoftravel" />
            <TableColumn fx:id="purposeofvisit" prefWidth="99.0" text="purposeofvisit" />
            <TableColumn fx:id="countryofresidence" prefWidth="115.0" text="countryofresidence" />
            <TableColumn fx:id="numberofnight" prefWidth="94.0" text="numberofnight" />
            <TableColumn fx:id="rate" prefWidth="47.0" text="rate" />
            <TableColumn fx:id="discount" minWidth="6.0" prefWidth="54.0" text="discount" />
            <TableColumn fx:id="total" prefWidth="45.0" text="total" />
         </columns>
      </TableView>
      <TableView fx:id="paginationTableview" layoutX="2.0" layoutY="214.0" prefHeight="160.0" prefWidth="1388.0" style="-fx-background-color: blue;" styleClass="tbTable-row-cell-white">
         <columns>
            <TableColumn fx:id="dateofarrival" prefWidth="79.0" text="dateofarrival" />
            <TableColumn fx:id="dateofdepature" prefWidth="103.0" text="dateofdepature" />
            <TableColumn fx:id="issuedat" prefWidth="77.0" text="issuedat" />
            <TableColumn fx:id="no" prefWidth="71.0" text="no" />
            <TableColumn fx:id="arrivedherefrom" prefWidth="105.0" text="arrivedherefrom" />
            <TableColumn fx:id="fullname" prefWidth="79.0" text="fullname" />
            <TableColumn fx:id="purposeofvisitt" prefWidth="92.0" text="purposeofvisit" />
            <TableColumn fx:id="durationofstay" prefWidth="90.0" text="durationofstay" />
            <TableColumn fx:id="destination" prefWidth="83.0" text="destination" />
            <TableColumn fx:id="hostaddress" prefWidth="93.0" text="hostaddress" />
            <TableColumn fx:id="emailaddress" prefWidth="99.0" text="emailaddress" />
            <TableColumn fx:id="dateofdepaturee" prefWidth="103.0" text="dateofdepature" />
            <TableColumn fx:id="sex" prefWidth="74.0" text="sex" />
         </columns>
      </TableView>
   </children>
</AnchorPane>

我的控制器代码

package hotel.management.system;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.Pagination;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javax.swing.JOptionPane;
/**
 * FXML Controller class
 *
 * @author PHILSERVER
 */
public class AliennavigationController implements Initializable {

    int itemPerPage = 5;
    int from = 0, to = 0;

    @FXML
    private Pagination pagination;

    @FXML
    private TableView<TableSetterGetter1> paginationTableview;

    @FXML
    private TableColumn<TableSetterGetter1, String> nameofhotel, surname, othernames, nationality, telephone, profession, maritalstatus, nameofspouse, numberofchildren, modeoftravel, purposeofvisit, countryofresidence, numberofnight, rate, discount, total, dateofarrival, dateofdepature, issuedat, no, arrivedherefrom, fullname, purposeofvisitt, durationofstay, destination, hostaddress, emailaddress, dateofdepaturee, sex;

    @Override
    public void initialize(URL url, ResourceBundle rb) {

     int count = 0;
         try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            JOptionPane.showMessageDialog(null, "Unable to register class " + e.getMessage());
        }

        try {
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost/level2", "root", "addison");
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("select count(*) from foreignersinformation");
            rs.first();
            count = rs.getInt(1);
            rs.close();
            stmt.close();

            con.close();
        } catch (Exception e) {
            e.printStackTrace();

        }

       nameofhotel.setCellValueFactory(new PropertyValueFactory<>("nameofhotel"));
       surname.setCellValueFactory(new PropertyValueFactory<>("surname"));
       othernames.setCellValueFactory(new PropertyValueFactory<>("othernames"));
       nationality.setCellValueFactory(new PropertyValueFactory<>("nationality"));
       telephone.setCellValueFactory(new PropertyValueFactory<>("telephone"));
       profession.setCellValueFactory(new PropertyValueFactory<>("profession"));
       maritalstatus.setCellValueFactory(new PropertyValueFactory<>("maritalStatus"));
       nameofspouse.setCellValueFactory(new PropertyValueFactory<>("nameofspouse"));
       numberofchildren.setCellValueFactory(new PropertyValueFactory<>("numberofchildren"));
       modeoftravel.setCellValueFactory(new PropertyValueFactory<>("modeoftravel"));
       purposeofvisit.setCellValueFactory(new PropertyValueFactory<>("purposeofvisit"));
       countryofresidence.setCellValueFactory(new PropertyValueFactory<>("countryofresidence"));
       numberofnight.setCellValueFactory(new PropertyValueFactory<>("numberofnight"));
       rate.setCellValueFactory(new PropertyValueFactory<>("rate"));
       discount.setCellValueFactory(new PropertyValueFactory<>("discount"));
       total.setCellValueFactory(new PropertyValueFactory<>("total"));
       dateofarrival.setCellValueFactory(new PropertyValueFactory<>("dateofarrival"));
       dateofdepature.setCellValueFactory(new PropertyValueFactory<>("dateofdepature"));
       issuedat.setCellValueFactory(new PropertyValueFactory<>("issuedat"));
       no.setCellValueFactory(new PropertyValueFactory<>("no"));
       arrivedherefrom.setCellValueFactory(new PropertyValueFactory<>("arrivedherefrom"));
       fullname.setCellValueFactory(new PropertyValueFactory<>("fullname"));
       purposeofvisitt.setCellValueFactory(new PropertyValueFactory<>("purposeofvisitt"));
       durationofstay.setCellValueFactory(new PropertyValueFactory<>("durationofstay"));
       destination.setCellValueFactory(new PropertyValueFactory<>("deatination"));
       hostaddress.setCellValueFactory(new PropertyValueFactory<>("hostaddress"));
       emailaddress.setCellValueFactory(new PropertyValueFactory<>("emailaddress"));
       dateofdepaturee.setCellValueFactory(new PropertyValueFactory<>("dateofdepaturee"));
       sex.setCellValueFactory(new PropertyValueFactory<>("sex"));

        int pageCount = (count / itemPerPage) + 1;
        pagination.setPageCount(pageCount);

        pagination.setPageFactory(this::createPage);
    }

    public List<TableSetterGetter1> getTableData() {

        List<TableSetterGetter1> data = new ArrayList<>();
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            JOptionPane.showMessageDialog(null, "Unable to register class " + e.getMessage());
        }

        try {
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost/level2", "root", "addison");
            Statement stmt = con.createStatement();

            ResultSet rs = stmt.executeQuery("select * from foreignersinformation limit " + from + "," + to);

            while (rs.next()) {
                data.add(new TableSetterGetter1("" + rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9), rs.getString(10), rs.getString(11), rs.getString(12), rs.getString(13), rs.getString(14), rs.getString(15), rs.getString(16), rs.getString(17), rs.getString(18), rs.getString(19), rs.getString(20), rs.getString(21), rs.getString(22), rs.getString(23), rs.getString(24), rs.getString(25), rs.getString(26), rs.getString(27), rs.getString(28), rs.getString(29)));
            }

            con.close();
        } catch (Exception e) {
            e.printStackTrace();

        }

        return data;

    }

    private Node createPage(int pageIndex) {
        from = pageIndex * itemPerPage;
        to = itemPerPage;
        paginationTableview.setItems(FXCollections.observableArrayList(getTableData()));
        return paginationTableview;

    }    

}
9rygscc1

9rygscc11#

使用相同的 fx:id 从来没用过。一 TableView 字段只能包含对单个 TableView . 此外,放置 TableView 它在table旁边 PaginationAnchorPane 这是错误的。 Pagination 添加它的 pageFactory 作为它的子代之一,这将导致上个节点从 AnchorPane 并依附于 Pagination 控制。你可以把这些 TableView 在一个共同的父母,例如 VBox 不要将此节点附加到场景:

<children>
    <Pagination fx:id="pagination" layoutX="-1.0" layoutY="213.0" prefHeight="208.0" prefWidth="1388.0" />
    <fx:define>
        <VBox fx:id="paginationContent">
            <children>
                <TableView fx:id="paginationTableview" layoutX="2.0" layoutY="7.0" prefHeight="166.0" prefWidth="1388.0" style="-fx-background-color: blue;" styleClass="tbTable-row-cell-white">
                    ...
                </TableView>
                <TableView fx:id="paginationTableview2" layoutX="2.0" layoutY="214.0" prefHeight="160.0" prefWidth="1388.0" style="-fx-background-color: blue;" styleClass="tbTable-row-cell-white">
                      ...
                </TableView>
            </children>
        </VBox>
    </fx:define>
</children>
@FXML
private TableView<TableSetterGetter1> paginationTableview, paginationTableview2;

@FXML
private VBox paginationContent;

...

private Node createPage(int pageIndex) {
    from = pageIndex * itemPerPage;
    to = itemPerPage;
    List<TableSetterGetter1> newData = getTableData();
    paginationTableview.getItems().setAll(newData);
    paginationTableview2.getItems().setAll(newData); // you could also use a common data list for both and only call setAll once
    return paginationContent;
}
int pageCount = (count / itemPerPage) + 1;
pagination.setPageCount(pageCount);

当db表中的行数可以被 itemPerPage ,这将导致空页。你应该检查一下,如果 count 可被整除 itemPerPage 添加前 1 :

int pageCount = (count / itemPerPage);
if (count % itemPerPage != 0) {
    pageCount++;
}
pagination.setPageCount(pageCount);

下面的示例使用java代码创建分页,但是您应该能够使用fxml再现类似的结果:

public static class Row {

    private final String v1;
    private final String v2;

    public Row(String v1, String v2) {
        this.v1 = v1;
        this.v2 = v2;
    }

    public String getV1() {
        return v1;
    }

    public String getV2() {
        return v2;
    }

}

private static TableView<Row> createTable(ObservableList<Row> data, String property) {
    TableView<Row> table = new TableView<>(data);
    TableColumn<Row, String> column = new TableColumn<>(property);
    column.setCellValueFactory(new PropertyValueFactory<>(property));

    table.getColumns().add(column);
    return table;
}

@Override
public void start(Stage primaryStage) {
    final int itemsPerPage = 5;
    final ObservableList<Row> data = FXCollections.observableArrayList();
    final TableView<Row> table1 = createTable(data, "v1");
    final TableView<Row> table2 = createTable(data, "v2");
    final VBox paginationContent = new VBox(table1, table2);
    Pagination pagination = new Pagination(5);
    pagination.setPageFactory(index -> {
        // replace items in shared list with items for page
        Row[] newItems = new Row[itemsPerPage];
        final int offset = itemsPerPage * index;
        for (int i = 0; i < itemsPerPage; i++) {
            int rowIndex = offset + i;
            newItems[i] = new Row(Character.toString((char) ('0' + rowIndex)), "index = " + rowIndex);
        }
        data.setAll(newItems);

        return paginationContent;
    });

    Scene scene = new Scene(pagination);

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

相关问题