我的桌子中有太多的字段,因为我把表分成了两个第一个表和第二个表...我的问题是我想在两个表中放一个分页,这样如果我决定移动我的通过按下我的分页记录到下一个的记录表,我的所有表将一起移动。 我尝试它甚至给了我的两个表不同的ID,但如果我试图把两个表上的分页没有任何显示,但如果我试图把它只在一个表上它工作正常现在我尝试给同样的id对于这两个表仍然只影响一个是我的代码中有一个错误。(我是一个新的分页类型的东西,所以我有一点问题。我不知道如何将scrollpane应用到我的表所以我需要在其他方面通过这个方法让我的两个表满一个人记录需要移动到另一个请帮助)
请在这里找到我的代码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;
}
}
答案 0 :(得分:0)
使用相同的fx:id
永远不会有效。 TableView
字段只能包含对单个TableView
的引用。此外,将TableView
放在Pagination
的{{1}}旁边是错误的。 AnchorPane
将其Pagination
返回的节点添加为其中一个后代,这会导致您的节点与pageFactory
分离并附加到AnchorPane
控件。您可以通过将这些Pagination
置于公共父级中来解决此问题,例如: a 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;
}
当db表中的行数可以被int pageCount = (count / itemPerPage) + 1;
pagination.setPageCount(pageCount);
整除时,这将导致空页面。在添加itemPerPage
之前,您应该检查count
是否可被itemPerPage
整除:
1
以下示例使用java代码创建分页,但您应该能够使用fxml重现类似的结果:
int pageCount = (count / itemPerPage);
if (count % itemPerPage != 0) {
pageCount++;
}
pagination.setPageCount(pageCount);