0

I'm new to JavaFX, so please forgive the noobishness.

So I'm making a Picture Viewer. This is my first time designing a GUI with the MVC paradigm, so I'm not very sure what I'm doing.

What I'm Trying To Accomplish:

It's just a normal picture viewer. Like your standard Windows program that opens when you want to look at a picture.

When you reszie the Picture Viewer window, the image automatically fits either with width or with height of the Image. If there is any amount of zoom - it becomes a viewfinder.

What I Did:

I'm using ScenBuilder to design my views. I currently have 2 views:

  1. the Root.fxml view, which is a BorderPane with just a MenuBar on top with a MenuItem "Open" that triggers a FileChooser and sets the MainApp.java currentImage Image property to whatever the user chose.

  2. Nested inside of (root.setCenter) it I have ImageViewer.fxml view, which is an AnchorPane that only contains an ImageView.

Unfortunately, the AnchorPane+ImageView combination can't achieve the effect I'm trying to achieve.

Fortunately, over here I found a custom ImageViewPane class that does exactly what I want: resizes the Pane based on the window size. So I want to use it insead of the AnchorPane. (Here's ImageViewPane.java)

The problem is that SceneBuilder accepts only FXML for custom classes, and I have no idea how to go about converting it. What do I need to do?

Current ImageViewer.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="JImageViewer.view.ImageViewerController">
   <children>
      <ImageView fx:id="imageView" layoutX="107.0" layoutY="90.0" pickOnBounds="true" preserveRatio="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
   </children>
</AnchorPane>
Community
  • 1
  • 1
Pt. Terk
  • 462
  • 4
  • 13
  • There are two things I think you need here. First your `ImageViewPane` doesn't have a default constructor. To be able to use it in FXML, you need to [annotate the constructor parameter](http://stackoverflow.com/questions/26823157/what-is-the-purpose-of-namedarg-annotation-in-javafx-8/26824088#26824088)(I don't know if SceneBuilder will recognize this though). Second, to use a custom component in SceneBuilder, you need to bundle it in a jar file and follow [this guide](http://stackoverflow.com/questions/29444698/how-to-create-an-fxml-file-for-an-already-created-new-component-in-java-than-add). – James_D Nov 23 '15 at 15:49
  • @James_D: Look again. The default constructor is the 5th member. But I don't know if residing in the default package causes any problems... – fabian Nov 23 '15 at 17:27
  • @fabian Oh yes: so it is. So the annotation is not necessary. Default packages definitely cause a problem with SceneBuilder (and FXML in general, I think): presumably the OP would be putting this in a package anyway... – James_D Nov 23 '15 at 17:28
  • @James_D not sure what you mean by the "default package" problem and which package I'll be putting it in. Do you mean that I need to put the generated FXML into the src folder of my application along with all the other .fxml views? – Pt. Terk Nov 24 '15 at 04:53
  • @James_D So I converted `ImageViewPane.java` into a jar by executing the `jar cf ImageViewPane.jar ImageViewPane.java` command, I then imported the jar into SceneBuilder using the same steps as in the guide you linked, but when the import dialog appears there are 0 items available for import. Would you kindly attempt to import `ImageViewPane` yourself? – Pt. Terk Nov 24 '15 at 05:01

0 Answers0