JavaFX elements aligning right before animating - java

I'm developing a small GUI using JavaFX for an assignment. Having never used JavaFX before, most of what I've used or learned I've research myself. Most of my application runs smoothly, animations included.
I have a small user input section:
I'm trying to allow it to swipe left prior to loading the next scene, to give the user the impression of sequence. I achieved this using a ParallelTransition populated by TranslationTransitions in which I load all of these Nodes.
However, just slightly before it animates, it appears to align everything right.
I have a feeling this is something to do with the container (AnchorPane) in which I'm placing these objects, or some undefined attribute which I'm not setting. As I'm learning this all on the fly, it's a bit difficult to narrow down. Can anyone give me an idea as to what is causing this and how I can retain the alignment? Each Node moves in the following way:
translateTransition.setFromX(node.getLayoutX());
translateTransition.setToX(node.getLayoutX()-500.00);
If I've left out any pertinent information, please let me know!

So, my issue turned out to be the way I'm laying out my objects. Placing them in an arbitrary AnchorPane with only specific attributes to locate those items means that, upon animation, it will group those objects and animate them accordingly. It was the incorrect way to layout those nodes.
Instead, I've placed them in a VBox which has two advantages.
I can align the contents of that VBox centre, so I need not specify the exact layout attributes of the elements.
I can animate using the single VBox, as opposed to the all the elements in a ParallelTransition. As a result, a single TranslateTransition on the VBox node will suffice!

Related

Android Studio: Group elements together

I am fairly new to android studio and I am trying to make a car rental app for a university project.
I designed an app which has a horizontal slider containing blocks of different cars however I am unsure on how I would do this, I understand how the slider works and how to add elements to it.
I have a linear layout within the horizontal slider which will contain all of the blocks but I am unsure how to make a block of elements. Within the block it should have two buttons, an image and some text. Here is a picture of the design.
As you can see the available blocks are surrounded by a square. this is what I want, a small container which surrounds my data. Sort of like a div box in html.
Is there a way to do this?
Please just point me in the correct direction!
Many thanks!
It seems like cardviews can handle most of the grouping you want to do. I would reccomend looking into them because they incorporate a lot of material design components right out of the box.
https://developer.android.com/guide/topics/ui/layout/cardview

How can i launch a new pane in java FX immediately to the right of the previous pane within the same window/stage?

My goal is to have a program with 3 panes. A mulitfactor Auth. The first pane will have the user type in a passphrase, while the second pain will allow the user to pick a image from a drop down list. But I want the 3rd pane to launch just to the right of the 2nd pane after the use selects a image in the same "Main" stage.
Not looking for someone to code a program just point me in the right direction to what im trying to do. My searching skills are failing, either im not explaining it right or theres another word for this.
Edit:
This is my idea of how i want it to work. Now that i look at it using a border pane probably makes since, But im still stuck with, How can I launch each section of the border at a different time, i.e when something is clicked.
I would go about it by having 3 panes side by side and just blank for the first FXML file you load in. I would then have another FXML file with the same layout that contains what you want to show up in those panes.
Then with that, you can have the controller on request (like when a user hits submit or however you are wanting these to show up) grab the content inside of the pane on the second FXML file by ID and load it into the pane.
I've done something similar with changing anchor panes and keeping the toolbar from the original so I can add more on this when I get home and should be able to supply some code that is modified to fit your issue.
Edit 1: Sorry I was in a hurry to submit that dive I had to go but I am on mobile now so I can edit but not able to add a lot, just felt I needed to say, there are different options for what you can use to do this which is why I just said a pane instead of anything specific. Just wanted to submit something so you can start looking in the right direction till I am able to update.
Edit 2: Alright now that I am home I tried this out and was able to get this working. Here is how I did it.
So I had two FXML files. One with the 3 areas that you have your items, however, only the box that you want to show when it starts is shown. Each area is enclosed by an AnchorPane. I used the AnchorPane as a container so I can swap out what is inside of it. I then had a second FXML file that had all of the boxes you want to show all of which enclosed in AnchorPanes. Here are pictures explaining what I mean.
I have the first pane named initial.fxml and the second named grabfrom.fxml. For the pane names, I just have it as pane1, pane2, and pane3. Lastly, the methods I have are show2() and show3() and call them from the FXML when the respective buttons are clicked inside of the AnchorePanes.
With initial, I just load that up as normal from the start method in my main class and that is all that is needed to be done with that. We only had it so we could display something that does not have the boxes showing before needed.
Now for the important part
With what I have in show2(), which is called when the button inside of the first pane (which is there from the start) is pressed.
public void show2() throws IOException{
AnchorPane toSetPane2=(AnchorPane) FXMLLoader.load(getClass().getResource("grabfrom.fxml"));
toSetPane2=(AnchorPane) toSetPane2.lookup("#pane2");
pane2.getChildren().setAll(toSetPane2);
}
What this is doing is loading the grabfrom.fxml into a temp var that we cast to an anchor pane. (Do note that this works since as you can see in the screenshot the whole FXML file is an anchor pane. If you're not using it that way you can take out the casting and cast to something you are using or not even cast depending on what it is.)
It then set the var we just made to just the AnchorPane we need, which is the second one since that's the one we are adding. It does this with the .lookup("#ID"); method to get just the pane we need.
Lastly, it sets everything inside of the current pane2 to toSetPane2.
This could all be compressed down into one line, however, I have left it as is for easier reading.
You should be able to use this method of loading in a portion of your application for loading in the third one and for that matter any other parts you want to in any situation.
Edit 3:
Also as #Swatarianess had said, there are stackpanes, this method will work with anything that you can set an ID to so they would work just as well. I used AnchorPanes because I have done a fair bit with them and had some code I could recycle whilst making a test for it so it was easier. All you would do if you were using those though is just cast to a StackPane instead of an AnchorPane like this:
public void show2() throws IOException{
StackPane toSetPane2=(StackPane) FXMLLoader.load(getClass().getResource("grabfrom.fxml"));
toSetPane2=(StackPane) toSetPane2.lookup("#pane2");
pane2.getChildren().setAll(toSetPane2);
}
The transition between panes could be done with a stackpane.

Superimposed node in javafx

Maybe the title of my question is not good but actually I don't know how to name it.
So I will try to explain my problem really clearly.
So I want to know if its possible to make like a "ErrorBar" inside my scene. I understand by this to show this element over other node inside the scene for a short time.
I have a BorderPane and I want when I have an error in my application (like webservice error) to show the error to the user. And for that I want to move a gridpane from the right to the left of the screen without change my scene (like put this object in the top of the borderpane).
I don't have any example with a software who do it but I think some stuff in Javascript can do something like this. Just show a green or red bar in the top of the website event it's somthing already show.
If someone know how to do that it will be great.
Actually I use Java 8 latest version.
Feel free to ask more if you want to know more about my question.
Thanks in advance.
Fische
I think you can just use a StackPane which allows you to work with "layers". Indeed in the StackPane, each Node you add to it becomes a "layer" and appears above previously added Nodes. Meaning that the latest Node will always be on top.
Knowing that, you can add your error message to the StackPane and then, if you use a transition to move it, at the end of the transition you automatically remove the message from the StackPane.
Hope it helps.

Choosing appropriate layout for this specific situation

I'm trying to figure out how to manage this layout in order for it to work. I have some ideas, but rehauling the whole thing is quite a bit of work to do.
This is how it looks like (in JTextAreas: "component name (parent (parent))"):
I have explaind the structure at the end of the question, if you feel the need to know.
This GUI is supposed to be very dynamic. You should be able to add and remove chapters, pages, questions and answers.
The GUI in the image above is made using nested JPanels (up to six layers on the thickest parts!) which most don't have thier size specified so they can adjust to the changes in the document. However, a lot of time is consumed (about a second per page) when drawing the document because the program keeps recalculating sizes of all the JPanels until they fit. So, unless I can specify the initial size (MigLayout) of a component, this method won't cut it for me.
Only alternative I have come up with is trying to put it all in one layer using MigLayout, which is doable, but I don't know how well does it work with the dynamic part of the whole thing. Removing and readding all the components (document could have over a hundred pages!) doesn't really seem as an option. Since most of the components are nested one onto another and are to move as one, this makes this solution even more difficult.
Also, all widths are fixed, while all of the heights within a page are flexible.
I really don't know how to go about this. Should I modify one of the existing ideas to work, or are there maybe libraries which are used in this type of situations? Is there another way?
Any ideas?
Also, as promised, this is the structure explained:
So, the thing important here is the JPanel inside a tab. It contains the DOCUMENT.
Document itself is made up out of random number of CHAPTERS. Each CHAPTER contains random number of PAGES. PAGES have MARINGS and CONTENT. On the image, pink and red parts are the MARGNIS, while everything within is CONTENT(green). CONTENT contains a single TITLE(blue). TITLE is made out ofa single JTextArea. After the TITLE, CONTENT can contain a random number of QUESTION(orange). QUESTION contains a JLabel(number) and JTextArea in one row, and below is a it's ANSWER PANEL. ANSWER PANEL contains up to five ANSWERS(yellow). Each ANSWER has a JCheckBox, JLabel (letter) and a JTextArea all in the same row.
Here I have some things marked out:
You seem to have the design you need. Break down each section and apply the required layout to achieve that section. Each section should be a self contained component.
So to my mind, start by modelling the data. You need a Document model, which contains a list of Chapters, which contains a list of Pages, which is made up of a list of Titles, which is is made up of a list of questions.
I would then provide a view for each level of the model. This will allow you to concentrate on the individual needs of each view, in isolation and reuse the code logic. It also means if you need to make changes, they will be more easy to make and reflected through the entire program
You seem to have the right idea for the Document/Chapters, being laid out within tabs.
I'd follow through. Each Page would be a self contained component, possibly using something like a GridLayout.
Each Content section would be its own component, consisting of the title editor and then the questions.
Here I'd use a BorderLyout, placing the title editor at the north position and the question panel in the center. You could then use something like a GridLayout for the questions pane.
As for the margins, you can achieve hese through the use EmptyBorders

Scene inside a Scene or Custom Control with JavaFX

After having used Swing for the past 3 years for School and such, I decided to give JavaFX a try and play around with it. With the new Scene Builder and NetBeans 7.2, it's been a blast so far.
The next step I'd like to do is convert a small project of mine into JavaFX. One of the issue however is that project uses a MVC Architecture. Basically I have a JFrame with a Toolbar, a JMenuBar, plus an empty JPanel.
The JPanel is used to display a View which is a JPanel built the using GUI Builder of NetBeans. This allows me to navigate to different view and each view is an independent component.
My question is as followed: Is there any ways to display a Scene inside a Scene? Or achieve similar result as a JPanel inside a JPanel, or a User Control inside a User Control (for the .Net folks)
Basically, I'd like to display another scene in the Gray area of the picture.
I had the same reaction when I came in: you want a Node (which feels wrong on a first glance). The Node is basically the root component that can contain itself (much like a JPanel) and though you can change out scenes on your stage if you're looking for the "root component" it will extend Node for the MVC basics.
http://docs.oracle.com/javafx/2/api/javafx/scene/Node.html
I'm sure you could get fancy and swap out scenes on your stage (which is totally viable) but from my Swing background the Node behaved more naturally. Caviat: I find I write a bit more framework and abstractions to make it work for me (wrapped classes with Groups etc...) but it definitely does the trick.
I hope to find a better way, for my "Module" based project I initially used Scenes, but found it completely restricting. The Nodes allow for much more dynamic content.
Best of luck.
edit: reading the doc after answering I really should say "Parent" but the direct subclasses of Node are all useful as containers)

Categories

Resources