I know the following problem is a bit of a luxury problem:
I would like to keep the initialize of my FXML Controller as clean as possible, and therefore I would like to set the placeholder of my TableView in the FXML file (as I think it is the best place to keep it, because it is just a property and in my case also a constant). I already tried to set it in the FXML file like this:
<TableView placeholder="Some text">
This obviously does not work, because the placeholder property expects a Node. That is why I set the placeholder like this in the initialize of the FXML controller:
Label placeholder = new Label();
placeholder.setText("Some text");
tableview.setPlaceholder(placeholder);
This works, but as I said, I would like to manage this from the FXML file only. Some my question is:
How can I set the placeholder from within the FXML file?
Note: please let me know if this i even possible, because when it is not, I will fill a feature request (with a low priority of course!).
Quite simple, just the usual FXML Syntax:
<BorderPane xmlns:fx="http://javafx.com/fxml/1">
<center>
<TableView>
<placeholder>
<Label text="some text"/>
</placeholder>
</TableView>
</center>
Note: Not everything is a primitive value (can be expressed inline) and therefore needs its own element.
I already found the answer using this question: Styling a JavaFX 2 button using FXML only - How to add an image to a button?
The graphic tag triggered the idea to do it like this:
<TableView>
<placeholder><Label text="Some Text"></Label></placeholder>
</TableView>
And luckily it works! I hope I helped some of you too. Also, sorry for asking this question too quickly.
Related
Maybe a really newbie's question....
I'm starting learning JavaFX in a FMXL Application using the Scene Builder, by reading this tutorials:
http://docs.oracle.com/javase/8/javafx/get-started-tutorial/fxml_tutorial.htm
So once i applied some changes, an issue with this 2 IDs came up... I might have missed or confused something about them...
Can anyone tell me in which cases they are used one or another?
id you use to set a CSS ID to your Component, for example <Text id="welcome-text" .../> and in your stylesheet you have something like #welcome-text { font-size: 16pt; } so this will be applied to your Text.
fx:id you use if you want to work with your Components in your Controller class, where you annotate them with #FXML Text myWelcomeText.
The fx:id is the identity associated to component in fxml to build a controller, and the id is used for css.
I took a look at an FXML document generated using the JavaFX Scene Builder. You access controls from Java Controller with the fx:id. (edit) I stand corrected, the id does matter.
You can apply css from the FXML document like this:
<Slider id="css_id" fx:id="myslider" styleClass="style_name" .../>
(Replace slider with any control)
And Java controller interaction:
#FXML
Slider myslider;
In JavaFX id is used to set a CSS ID to a component. And fx:id is used for accessing that component in code (i.e. in a controller class). fx:id works like a components name.
Maybe a really newbie's question....
I'm starting learning JavaFX in a FMXL Application using the Scene Builder, by reading this tutorials:
http://docs.oracle.com/javase/8/javafx/get-started-tutorial/fxml_tutorial.htm
So once i applied some changes, an issue with this 2 IDs came up... I might have missed or confused something about them...
Can anyone tell me in which cases they are used one or another?
id you use to set a CSS ID to your Component, for example <Text id="welcome-text" .../> and in your stylesheet you have something like #welcome-text { font-size: 16pt; } so this will be applied to your Text.
fx:id you use if you want to work with your Components in your Controller class, where you annotate them with #FXML Text myWelcomeText.
The fx:id is the identity associated to component in fxml to build a controller, and the id is used for css.
I took a look at an FXML document generated using the JavaFX Scene Builder. You access controls from Java Controller with the fx:id. (edit) I stand corrected, the id does matter.
You can apply css from the FXML document like this:
<Slider id="css_id" fx:id="myslider" styleClass="style_name" .../>
(Replace slider with any control)
And Java controller interaction:
#FXML
Slider myslider;
In JavaFX id is used to set a CSS ID to a component. And fx:id is used for accessing that component in code (i.e. in a controller class). fx:id works like a components name.
I'm using JavaFX Scene Builder 2.0. version and I would like to import a FXML file under TabPane controller. My idea is that I will have one kind of container FXML file with TabPane and separate FXML files for things that will come inside of tabs. When I try to import FXML file where I have things that I would like to add under one tab, Scene Builder gives error message "Failed to import TabContents.fxml under TabPane". Why it is not possible to import anything under the TabPane? If I import same file directly under root StackPane everything works.
EDIT: While the below works, it is best to add some sort of layout pane (like an AnchorPane) to each tab in your TabPane, select the AnchorPane pane in the hierarchy view on the left and then import your other fxml document into that, as opposed to importing directly into a TabPane (Perhaps if the root node of the document is a Tab it would work?).
Looks like a bug, for now:
Cut your entire document hierarchy
Add an empty stack pane
Include your other FXML file
Paste your document hierarchy into the stack pane
Drag your FXML file into the appropriate place in your hierarchy
Cut your new hierarchy (with your file included)
Delete the now defunct stack pane
Paste your hierarchy back in
And voila, working as intended!
Three simple steps
Add a StackPane to the content area of your Tab, ensuring that it occupies 100% of the area.
Go to the Hierarchy view and select that StackPane you just added.
Include your TabContents.fxml. It should automatically be added to the created StackPane and should display as desired.
Also, to answer your additional question, importing it directly into the TabPane will render it as a Graphic rather than a Node.
just add a new TabPane where you want and then edit your FXML code of the TabPane just added as following:
<TabPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="5000.0" prefWidth="5000.0" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab closable="false" text="MyTab1">
<content>
<fx:include source="MyTab1.fxml" />
</content>
</Tab>
<Tab closable="false" text="MyTab2">
<content>
<fx:include source="MyTab2.fxml" />
</content>
</Tab>
</tabs>
</TabPane>
adding as many tab you need just adding more 'Tab' tags.
'MyTab1.fxml' and 'MyTab2.fxml' are just fxml files contained in the same folder of the fxml file of the TabPane.
it works for me selecting first the root node in the scene. Then I import de FXML and it is imported hanging from this root node. As the last step, I move the imported FXML to the place I want in the scene.
I realised that the FXML import can also fail when you have another FXML template included in the one you want to import. Something like:
<fx:include source="top-menu-bar.fxml" />
Looks like SceneBuilder doesn't know where is top-menu-bar.fxml. The solution can be to work on the template separately and then adding those includes manually to make it works.
It works for me:
<AnchorPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" >
<children>
<fx:include fx:id="includeID" source="includeSource.fxml" />
</children>
</AnchorPane>
The include file must be include in the children of a pane.
How to get elements or nodes from FXML file using Java, I know the way by using initialization or by setting controller class in FxmL . But I need to do it without any controller. I want to access the nodes inside the fxml file using.
My FXML COde:
HBox fx:id="hbx" id="hbx" alignment="CENTER_RIGHT" prefHeight="100.0"
prefWidth="200.0" BorderPane.alignment="CENTER"
My java Code
System.out.println(par.lookupAll("hbx"));
See my Code above, could you give me a hint?
After loading the FXML file, you can use Node#lookup():
Node node = fxmlParentPane.lookup("#nodeId");
This is probably a very simple question, but at this time I have myself so confused I can't see the answer. Simply put, I have a window that contains a content control. I'm using Caliburn.Micro's conventions to "locate" the view.
The window looks like this:
<Window x:Class="Views.MainWindowView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox/>
<ContentControl x:Name="MyViewModel" Height="Auto" Background="Blue"/>
</Grid>
</Window>
The view itself is successfully found, and the screen displays as I expected. However, MyViewModel needs to make a service call to get information based on what is typed into the text box.
So, what I can't seem to figure out is how one would pass that information from the text box to the view model. I've thought of several options, but they all seem to be too much work which makes me think that I'm missing something simple.
Thanks a lot
Like you said there are a number of things you can do:
You could expose a property on MyViewModel and set it within
MainWindowView.
You could use the EventAgregator, publish an event from the
MainWindowView and subscribe to that event from MyViewModel.
Using MEF you could inject a shared resource between the two
ViewModels, set it in MainWindowViewModel, and be able to access it
from MyViewModel.