I am trying to download a TableView
in JavaFx as an pdf document. I am using iText 7.2.5 . When I am clicking the button to download the TableView
, it is asking me to select the folder then it is asking to write the name for the pdf document then after it; when I press ok to download the document, it downloads the pdf file that is unable to show any content even a blank page. When I open the pdf file with chrome it shows this, and I also tried to open with Adobe Acrobat, but situation is the same with pdf file.
Error Failed to load PDF document.
First, it was giving an exception for slf4j; then I added these jar files in classpath: slf4j-simple-2.0.6.jar , slf4j-api-2.0.6-javadoc.jar , slf4j-api-2.0.6.jar . It was giving warning for provider not found or something like that; that was solved after adding slf4j-simple-2.0.6.jar.
I saw a similar question here java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory problem.
Answer of this question is: It turns out that it missed the following 3 jars : slf4j.api, slf4j-log4j12, and log4j
It works now after adding the above 3 jars.
These are all the files at org/slf4j
integration/ - -
jcl-over-slf4j/ - -
jcl104-over-slf4j/ - -
jul-to-slf4j/ - -
log4j-over-slf4j/ - -
nlog4j/ - -
osgi-over-slf4j/ - -
slf4j-android/ - -
slf4j-api/ - -
slf4j-archetype/ - -
slf4j-converter/ - -
slf4j-ext/ - -
slf4j-jcl/ - -
slf4j-jdk-platform-logging/ - -
slf4j-jdk14/ - -
slf4j-log4j12/ - -
slf4j-log4j13/ - -
slf4j-migrator/ - -
slf4j-nop/ - -
slf4j-parent/ - -
slf4j-reload4j/ - -
slf4j-simple/ - -
slf4j-site/ - -
slf4j-skin/ - -
taglib/ - -
log4j-over-slf4j/ ---> is it the file log4j that is mentioned in reference answer to the question.
I am unable to get slf4j-log4j12.
because org/slf4j/slf4j-log4j12/2.0.6 contains no jar file but this
slf4j-log4j12-2.0.6.pom 2022-12-12 19:14 873
slf4j-log4j12-2.0.6.pom.asc 2022-12-12 19:14 317
slf4j-log4j12-2.0.6.pom.md5 2022-12-12 19:14 32
slf4j-log4j12-2.0.6.pom.sha1
Here is the code on download button:
@FXML
public void ButtonDownload(ActionEvent event) {
DirectoryChooser directoryChooser = new DirectoryChooser();
File selectedDirectory = directoryChooser.showDialog(TableName.getScene().getWindow());
if (selectedDirectory == null) {
return;
}
TextInputDialog dialog = new TextInputDialog("table");
dialog.setTitle("Enter File Name");
dialog.setHeaderText("Enter the name for the PDF file:");
dialog.setContentText("File name:");
Optional<String> result = dialog.showAndWait();
if (result.isPresent()) {
try {
String fileName = result.get();
if (!fileName.endsWith(".pdf")) {
fileName = fileName + ".pdf";
}
String filePath = selectedDirectory.getAbsolutePath() + "/" + fileName;
OutputStream file = new FileOutputStream(filePath);
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(filePath));
Document document = new Document(pdfDoc);
Table pdfTable = new Table(TableName.getColumns().size());
for (TableColumn<?, ?> column : TableName.getColumns()) {
pdfTable.addCell(column.getText());
}
for (Object item : TableName.getItems()) {
for (TableColumn<?, ?> column : TableName.getColumns()) {
/*this line is line:254*/
pdfTable.addCell(column.getCellData((int) item).toString());
}
}
document.add(pdfTable);
document.close();
} catch (Exception e) {
System.out.println("Exception while downloading pdf tableView: "+e);
e.printStackTrace();
}
}
}
This is the StackTrace:
Exception while downloading pdf tableView: java.lang.ClassCastException: class application.(className --> that is the controller of the fxml file where the download button is placed) cannot be cast to class java.lang.Integer (application.className(Controller) is in unnamed module of loader 'app'; java.lang.Integer is in module java.base of loader 'bootstrap')
java.lang.ClassCastException: class application.className(Controller) cannot be cast to class java.lang.Integer (application.className(Controller) is in unnamed module of loader 'app'; java.lang.Integer is in module java.base of loader 'bootstrap')
at application.className(Controller).DownloadMethod(className(Controller).java:254)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:77)
at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at javafx.base@19.0.2.1/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml@19.0.2.1/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:84)
at javafx.fxml@19.0.2.1/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1852)
at javafx.fxml@19.0.2.1/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1724)
at javafx.base@19.0.2.1/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base@19.0.2.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base@19.0.2.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base@19.0.2.1/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base@19.0.2.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base@19.0.2.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@19.0.2.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base@19.0.2.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@19.0.2.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base@19.0.2.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@19.0.2.1/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base@19.0.2.1/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base@19.0.2.1/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics@19.0.2.1/javafx.scene.Node.fireEvent(Node.java:8923)
at javafx.controls@19.0.2.1/javafx.scene.control.Button.fire(Button.java:203)
at javafx.controls@19.0.2.1/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:207)
at javafx.controls@19.0.2.1/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base@19.0.2.1/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
at javafx.base@19.0.2.1/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base@19.0.2.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base@19.0.2.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base@19.0.2.1/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base@19.0.2.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base@19.0.2.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@19.0.2.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base@19.0.2.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@19.0.2.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base@19.0.2.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@19.0.2.1/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base@19.0.2.1/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base@19.0.2.1/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics@19.0.2.1/javafx.scene.Scene$MouseHandler.process(Scene.java:3894)
at javafx.graphics@19.0.2.1/javafx.scene.Scene.processMouseEvent(Scene.java:1887)
at javafx.graphics@19.0.2.1/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2620)
at javafx.graphics@19.0.2.1/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
at javafx.graphics@19.0.2.1/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics@19.0.2.1/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
at javafx.graphics@19.0.2.1/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
at javafx.graphics@19.0.2.1/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
at javafx.graphics@19.0.2.1/com.sun.glass.ui.View.handleMouseEvent(View.java:551)
at javafx.graphics@19.0.2.1/com.sun.glass.ui.View.notifyMouse(View.java:937)
at javafx.graphics@19.0.2.1/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics@19.0.2.1/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
at java.base/java.lang.Thread.run(Thread.java:833)
EDIT: 254 original code
pdfTable.addCell(column.getCellData(item).toString());
but then It asked to cast argument item to int like this
pdfTable.addCell(column.getCellData((int) item).toString());
Kindly help out to solve this.