JavaFX DirectoryChooser - display files - javafx

Is it possible to display files available in the folder when using DirectoryChooser in JavaFX?
directoryChooser.showDialog - This the the dialog for DirectoryChosser in JavaFX.
This question is asked before & i have gone through this
JavaFX - display files in the DirectoryChooser
I was able to achieve this using JFileChooser but the UI is completely different
JFileChooser select directory but show files
JFileChooser code:
final JFileChooser chooser = new JFileChooser() {
public void approveSelection() {
if (getSelectedFile().isFile()) {
return;
} else{
super.approveSelection();
stubFolder.setText(getCurrentDirectory().getAbsolutePath());
}
}
};
chooser.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES );
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
System.out.println("getSelectedFile() : " + chooser.getSelectedFile());
} else {
System.out.println("No Selection ");
}

Related

JavaFX-Apache pdfbox view PDDocument in pane

I'm using apache pdfbox in my javaFx application where im reading a pdf document now i want to display a org.apache.pdfbox.pdmodel.PDDocument inside a pane in my FXML. So far i tried with org.apache.pdfbox.PDFReader but it is using it's own Jframe. I want to show it inside a pane.
Here what i have done so far
public class CustomPDFReader extends PDFReader {
public CustomPDFReader(BillModel bm) {
super();
showAllPages(bm.getAllPages());
setVisible(true);
}
private void showAllPages(List<PDPage> pagesList) {
try {
Field documentPanel = getClass().getSuperclass().getDeclaredField("documentPanel");
documentPanel.setAccessible(true);
JPanel panel = (JPanel) documentPanel.get(this);
GridLayout layout = new GridLayout(0, 1);
panel.setLayout(layout);
for(PDPage page : pagesList) {
PageWrapper wrapper = new PageWrapper(this);
wrapper.displayPage(page);
panel.add(wrapper.getPanel());
}
pack();
} catch(Exception e) {
e.printStackTrace();
}
}
}

Screenshot saving error JavaFX

I am trying to save a screenshot of the current scene using javaFX.
saveMenuItem.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
WritableImage image = scene.snapshot(new SnapshotParameters(), null);
// TODO: probably use a file chooser here
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Save Image");
File file = fileChooser.showSaveDialog(primaryStage);
if(file != null)
{
try {
ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file);
}
catch (IOException e) {
System.out.println("Couldn't Save.");
}
}
}
});
But my compiler NetBeans IDE 8.1 is giving an error:
incompatible types: SnapshotParameters cannot be converted to Callback<SnapshotResult, Void>
Can someone tell me what I am doing wrong?
And your compiler is right. A Scene just does not have a method like the one you are trying to call. Just use
WritableImage image = scene.snapshot(null);

Open JavaFX virtual keyboard programmatically

I have partially solved the following problem: JavaFX WebView / WebEngine show on-screen-keyboard automatically for each text input
I stucked at the 6th point because I would like to use the built in JavaFX virtual keyboard but I can not find any reference how can trigger the displaying of it.
Do you know any solution for this? If it is possible I do not want to use 3rd party library.
I am going to answer my question because I found a solution.
First of all I added an event listener for all input tags on webpage, after page loaded:
private void addEventListenersToDOM() {
webview.getEngine().getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
#Override
public void changed(ObservableValue<? extends State> ov, State oldState, State newState) {
if (newState == State.SUCCEEDED) {
JSObject win = (JSObject) webview.getEngine().executeScript("window");
win.setMember("javaFXVirtualKeyboard", new JavaFXVirtualKeyboard());
String script =
"var inputsList = document.getElementsByTagName('input');"
+ "for (var index = 0; index < inputsList.length; ++index) { "
+ "inputsList[index].addEventListener('focus', function() { javaFXVirtualKeyboard.show() }, false); "
+ "inputsList[index].addEventListener('focusout', function() { javaFXVirtualKeyboard.hide() }, false); "
+ "}";
webview.getEngine().executeScript(script);
}
}
});
}
And the key point, how I triggering the keyboard displaying and hiding:
public class JavaFXVirtualKeyboard {
public void show() {
FXVK.init(webview);
FXVK.attach(webview);
}
public void hide() {
FXVK.detach();
}
}
One note: FXVK class is not an API so we get a warning message in all cases but it works without any bug.
Discouraged access: The type 'FXVK' is not API (restriction on required library 'C:\Program Files\Java\jre1.8.0_91\lib\ext\jfxrt.jar')

Deleting a file(s) through FileChooser?

I have a JavaFX app using JRE1.8.0 u40. I converted my Swing JFileChooser Open and Save to the newer JavaFX FileChooser Open and Save, Windows7 style Dialog box. But I have not found an equivalent JavaFX FileChooser method to replace the JFileChooser method I'm using for deleting a file(s) as shown below:
public static void deleteFile() throws IOException {
JFileChooser fileDialog = new JFileChooser("C:\\ProgramData\\L1 Art Files\\");
File[] selectedFiles;
fileDialog.setSelectedFiles(null);
// Set frame properties
fileDialog.setDialogTitle("Delete Pixel Art File(s)");
//fileDialog.setLayout(new FlowLayout());
fileDialog.setSize(400, 400);
fileDialog.setVisible(true);
fileDialog.setMultiSelectionEnabled(true); // Allow multiple selection
fileDialog.setVisible(true);
int option = fileDialog.showDialog(null, "Delete");
if (option != JFileChooser.APPROVE_OPTION)
return; //user canceled the
selectedFiles = fileDialog.getSelectedFiles();
if (selectedFiles != null) { //ask the user to replace this file
int response = JOptionPane.showConfirmDialog(null, "Are you sure want to delete this file?",
"Confirm Delete",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
if (response != JOptionPane.YES_OPTION) return;
}
for (File f : selectedFiles) {
Files.delete(f.toPath());
}
Is there a similar solution to the above for JavaFX using FileChooser or do I use the showOpenDialog(null) with setTitle("Delete Pixel Art File")?
You can easily perform that task using javafx like below :
#FXML
private void onDeleteAction(ActionEvent event) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Your_title_here");
List<File> selectedFiles = fileChooser.showOpenMultipleDialog(null);
if (selectedFiles != null) {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Confirmation Dialog");
alert.setHeaderText("Warning !");
alert.setContentText("Are you sure you want to delete these files ?");
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK) {
for (File selectedFile : selectedFiles) {
selectedFile.delete();
}
}
} else {
System.out.println("Error Selection");
}
}
The above code was very helpful and works best with the other suggestion of checking for null and adding throws IOException to the deleteFile() method.

Family Tree project javafx

I have searched ALL the web for an answer and gave up. So my last hope is to find an answer here!!!
I have started to make a Family Tree project using JAVAFX (NOT FXML) and I am stocked in the middle!!!
I have the functionality written in JAVA and can not implement it on JAVAFX. For example the "ADD BUTTON" or "EDIT BUTTON" or ....
Here is the link to all the project: The Codes
Basically this is the part that i get confused, To use the java method in javafx:
Button AddButton = new Button("Add");
Button DeleteButton = new Button("Delete");
Button EditButton = new Button("Edit");
/**
* Adding a person to the family tree
*/
public void addPerson(Person aPerson) {
boolean found = false;
for (Person p : family) {
if (p.compareTo(aPerson) == 1) {
found = true;
}
}
if (!found) {
family.add(aPerson);
System.out.println(aPerson.getName() + " has been added!");
} else {
System.out.println("Error 301 - Person already in the family tree.");
}
}
Any help will be truly appreciated !!!!!
Cheers
Just register a handler with the button:
addButton.setOnAction(e -> {
Person personToAdd = ... ;
addPerson(personToAdd);
});
See the tutorial.

Resources