mysql - javafx scenebuilder中的分页与tableview

标签 mysql javafx netbeans pagination tableview

我的表中有太多字段,因此我将表分为两个第一个表和第二个表......我的问题是我想在两个表中放置一个分页,这样如果我决定将我的表移动按我的分页记录到下一个,我所有的表都会一起移动。
我尝试了它,甚至给了我的两张 table 不同的 id,但是如果我尝试将分页放在两张 table 上没有任何显示,但是如果我尝试将它只放在一张 table 上,它现在可以正常工作,我尝试给出相同的 id到两个表,但它仍然只影响一个是我的代码中有错误。(我是分页类的新手,所以我对它有一点问题。我不知道如何将滚动 Pane 应用于我的表所以我需要通过这个方法来让我的两张表充满一个人的记录需要移动到另一个请帮助)

请这里是我的代码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>

我的 Controller 代码
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;


    }    

}

最佳答案

使用相同的fx:id从不工作。一个 TableView字段只能包含对单个 TableView 的引用.此外,将 TableView s 旁边的 PaginationAnchorPane是错的。 Pagination添加由它返回的节点 pageFactory作为它的后代之一,这会导致您的一个节点与 AnchorPane 分离并依附于 Pagination控制。您可以通过放置 TableView 来解决此问题s 在一个共同的 parent 中,例如一个 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();
}

关于mysql - javafx scenebuilder中的分页与tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50760365/

相关文章:

Java Swing 按钮颜色

php - 如何更灵活地将许多列传递给bind_result()?

php - 使用mysql和php计算2点之间的距离

mysql - 根据查询将数据插入到表中,该查询使用其他表中的数据而不是需要插入的数据

java - Thread.exit() 行 :not available error in integrating javafx into swing using jfxpanel

php - 从搜索中排除文件夹,但仍考虑在 Netbeans 7 (php) 的导航中

c# - Entity Framework 数据库提供程序兼容性错误

JavaFX 应用程序无法使用 native exe 包启动

sorting - JavaFX 按日期对 TableView 列进行排序(dd/mm/yyyy 格式)

java - Java 和 Swing 中的跨平台按钮排序