@James_Duh 将两个文本字段添加到您的第二个窗格(paneRpt)。然后在 Scene Builder 中将 GridPane (gpRpt) 包装在 ScrollPane 中,并向 GridPane 列零添加一个新列,该列只需要 PerfWidth 25。我已经重写了您的 onBuildRpt 方法,因此 GridPane 反映了 CSV 文件中的所有数据。GridPane 现在是可滚动的,并且每条记录都有一个行号。这是有趣的开始,您现在可以选择开始行号和结束行号进行打印。我无法发出换页,因此打印仍然是一个多步骤过程。实际的打印设计由打印按钮调用的新方法 printRpt 管理。我在下面发布了相关代码。
@FXML
private void onBuildRpt(ActionEvent e) throws IOException {
if(list.size()==0) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Information");
alert.setHeaderText("");
alert.setContentText("No Data for Report\n"+
"Select File then Alt + A to Add New Data");
alert.showAndWait();
return;
}
int row = 0;
togp = new ArrayList<>();
pane.setVisible(false);
paneRpt.setVisible(true);
gpRpt.setVisible(true);
gpRpt.getChildren().clear();
//gpRpt.gridLinesVisibleProperty().set(true);
//gpRpt.setLayoutX(20);
//gpRpt.setLayoutY(10);
int R = 2;
File file = new File("C:/A_Hospital/Patients.csv");
Path dirP = Paths.get(String.valueOf(file));
InputStream in = Files.newInputStream(dirP);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
scan = new Scanner(reader);
scan.useDelimiter("\\s*,\\s*");
while (scan.hasNext()){
String hospnum = scan.next();
String fname = scan.next();
String lname = scan.next();
String roomnum = scan.next();
togp.add(hospnum);
togp.add(fname);
togp.add(lname);
togp.add(roomnum);
}
scan.close();
for(int p = 0; p < togp.size(); p++){
row = row + 1;
Text txt0 = new Text(""+row);
//row number
GridPane.setHalignment(txt0, HPos.CENTER);
txt0.setFont(Font.font("Times New Roman", FontWeight.BLACK,
FontPosture.REGULAR, 14));`
gpRpt.add(txt0, 0, R);
Text txt1 = new Text(togp.get(p));
//hospnum
GridPane.setHalignment(txt1, HPos.CENTER);
txt1.setFont(Font.font("Times New Roman", FontWeight.BLACK,
FontPosture.REGULAR, 14));`
gpRpt.add(txt1, 1, R);
Text txt2 = new Text(togp.get(p=p+1));
//fname
GridPane.setHalignment(txt2, HPos.LEFT);
txt2.setFont(Font.font("Times New Roman", FontWeight.BLACK,
FontPosture.REGULAR, 14));`
gpRpt.add(txt2 , 2, R);
Text txt3 = new Text(togp.get(p=p+1));
//lname
GridPane.setHalignment(txt3, HPos.LEFT);
txt3.setFont(Font.font("Times New Roman", FontWeight.BLACK,
FontPosture.REGULAR, 14));`
gpRpt.add(txt3, 3, R);
Text txt4 = new Text(togp.get(p=p+1));
//roomnum
GridPane.setHalignment(txt4, HPos.LEFT);
txt4.setFont(Font.font("Times New Roman", FontWeight.BLACK,
`FontPosture.REGULAR, 14));`
gpRpt.add(txt4, 4, R);
Text txtS0 = new Text(" ");
txtS0.prefHeight(5);
Text txtS1 = new Text(" ");
txtS1.prefHeight(5);
Text txtS2 = new Text(" ");
txtS2.prefHeight(5);
Text txtS3 = new Text(" ");
txtS3.prefHeight(5);
GridPane.setFillHeight(txtS0, true);
gpRpt.add(txtS0, 0, R+1);
GridPane.setFillHeight(txtS1, true);
gpRpt.add(txtS1, 1, R+1);
GridPane.setFillHeight(txtS2, true);
gpRpt.add(txtS2, 2, R+1);
GridPane.setFillHeight(txtS3, true);
gpRpt.add(txtS3, 3, R+1);
R = R + 2;
}
txfStartRow.requestFocus();
}
@FXML
private void printRpt() {
//This method just clears the gpRpt and
//reloads it with selected row number range
if(txfStartRow.getText().isEmpty() || txfEndRow.getText().isEmpty()) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Information");
alert.setHeaderText("");
alert.setContentText("No Data Entered\n"+
"Enter Start Row Number and End Row Number");
alert.showAndWait();
if(txfStartRow.getText().isEmpty()) {
txfStartRow.requestFocus();
return;
}
if(txfEndRow.getText().isEmpty()) {
txfEndRow.requestFocus();
return;
}
return;
}
String regX1 = ("[0-9\\^W]*");
String W1 = String.valueOf(txfStartRow.getText().trim());
if(!W1.matches(regX1) || Integer.valueOf(W1) > togp.size()/4) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Information");
alert.setHeaderText("");
alert.setContentText("Numeric Values Only\n"+
"\nAcceptable Rows 1 to "+ togp.size()/4 +" Can Be Entered");
alert.showAndWait();
txfStartRow.requestFocus();
return;
}
String regX2 = ("[0-9\\^W]*");
String W2 = String.valueOf(txfEndRow.getText().trim());
if(!W2.matches(regX2) || Integer.valueOf(W2) > togp.size()/4) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Information");
alert.setHeaderText("");
alert.setContentText("Numeric Values Only\n"+
"\nAcceptable Rows 1 to "+ togp.size()/4 +" Can Be Entered");
alert.showAndWait();
txfEndRow.requestFocus();
return;
}
if(Integer.valueOf(txfStartRow.getText()) <= 0 ||
Integer.valueOf(txfEndRow.getText()) <=0) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Information");
alert.setHeaderText("");
alert.setContentText("Data Entry Error\n"+
"Start & End Rows Can NOT be zero");
alert.showAndWait();
txfStartRow.setText("");
txfEndRow.setText("");
txfStartRow.requestFocus();
return;
}
if(Integer.valueOf(txfEndRow.getText()) - Integer.valueOf
(txfStartRow.getText()) > 19) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Information");
alert.setHeaderText("");
alert.setContentText("Printing Issue\n"+
"Max Report Size 20 Rows");
alert.showAndWait();
//txfStartRow.setText("");
//txfEndRow.setText("");
txfStartRow.requestFocus();
return;
}
if(Integer.valueOf(txfStartRow.getText()) >
Integer.valueOf(txfEndRow.getText())) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Information");
alert.setHeaderText("");
alert.setContentText("Data Entry Error\n"+
"Start Row Greater than End Row");
alert.showAndWait();
//txfStartRow.setText("");
//txfEndRow.setText("");
txfStartRow.requestFocus();
return;
}
//Fancy Math for Row Calculation to find togp index
//This should work no matter the size of the togp ArrayList
int SR = Integer.valueOf(txfStartRow.getText());
SR = SR * 3 + SR - 4;
int ER = Integer.valueOf(txfEndRow.getText());
ER = ER * 3 + ER - 3;
gpRpt.getChildren().clear();
int row = 0;
int R = 2;
Text txtH0 = new Text("Row");
GridPane.setHalignment(txtH0, HPos.CENTER);
txtH0.setFont(Font.font("Times New Roman", FontWeight.BLACK,
FontPosture.REGULAR, 14));
gpRpt.add(txtH0, 0, 0);
Text txtH1 = new Text("Hospital ID");
GridPane.setHalignment(txtH1, HPos.CENTER);
txtH1.setFont(Font.font("Times New Roman", FontWeight.BLACK,
FontPosture.REGULAR, 14));
gpRpt.add(txtH1, 1, 0);
Text txtH2 = new Text("First Name");
GridPane.setHalignment(txtH2, HPos.LEFT);
txtH2.setFont(Font.font("Times New Roman", FontWeight.BLACK,
FontPosture.REGULAR, 14));
gpRpt.add(txtH2, 2, 0);
Text txtH3 = new Text("Last Name");
GridPane.setHalignment(txtH3, HPos.LEFT);
txtH3.setFont(Font.font("Times New Roman", FontWeight.BLACK,
FontPosture.REGULAR, 14));
gpRpt.add(txtH3, 3, 0);
Text txtH4 = new Text("Room");
GridPane.setHalignment(txtH4, HPos.LEFT);
txtH4.setFont(Font.font("Times New Roman", FontWeight.BLACK,
FontPosture.REGULAR, 14));`
gpRpt.add(txtH4, 4, 0);
for(int p = SR; p < ER; p++){
row = row + 1;
Text txt0 = new Text(""+row);
//row #
GridPane.setHalignment(txt0, HPos.CENTER);
txt0.setFont(Font.font("Times New Roman", FontWeight.BLACK,
FontPosture.REGULAR, 14));
gpRpt.add(txt0, 0, R);
Text txt1 = new Text(togp.get(p));
//hospnum
GridPane.setHalignment(txt1, HPos.CENTER);
txt1.setFont(Font.font("Times New Roman", FontWeight.BLACK,
FontPosture.REGULAR, 14));
gpRpt.add(txt1, 1, R);
Text txt2 = new Text(togp.get(p=p+1));
//fname
GridPane.setHalignment(txt2, HPos.LEFT);
txt2.setFont(Font.font("Times New Roman", FontWeight.BLACK,
FontPosture.REGULAR, 14));
gpRpt.add(txt2 , 2, R);
Text txt3 = new Text(togp.get(p=p+1));
//lname
GridPane.setHalignment(txt3, HPos.LEFT);
txt3.setFont(Font.font("Times New Roman", FontWeight.BLACK, FontPosture.REGULAR, 14));
gpRpt.add(txt3, 3, R);
Text txt4 = new Text(togp.get(p=p+1));
//roomnum
GridPane.setHalignment(txt4, HPos.LEFT);
txt4.setFont(Font.font("Times New Roman", FontWeight.BLACK,
FontPosture.REGULAR, 14));
gpRpt.add(txt4, 4, R);
Text txtS0 = new Text(" ");
txtS0.prefHeight(5);
Text txtS1 = new Text(" ");
txtS1.prefHeight(5);
Text txtS2 = new Text(" ");
txtS2.prefHeight(5);
Text txtS3 = new Text(" ");
txtS3.prefHeight(5);
GridPane.setFillHeight(txtS0, true);
gpRpt.add(txtS0, 0, R+1);
GridPane.setFillHeight(txtS1, true);
gpRpt.add(txtS1, 1, R+1);
GridPane.setFillHeight(txtS2, true);
gpRpt.add(txtS2, 2, R+1);
GridPane.setFillHeight(txtS3, true);
gpRpt.add(txtS3, 3, R+1);
R = R + 2;
}
onPrint();
}
public void onPrint(){
//gpRpt.gridLinesVisibleProperty().set(true);
gpRpt.setStyle("-fx-background-color:transparent");
print(gpRpt, selectedPrinter);
}