Include FXML file in TableView Cell - javafx

Is it possible to set a whole FXML File with its corresponding controller as a single cell of a TableView? I really get no way to do so.
I want to have a TableView with only eight cells, and each of these cells is a LineChart and has its own FXML file and controller. For example, one of these cells should be this FXML file:
<LineChart xmlns:fx="http://javafx.com/fxml/1" fx:controller="control.Channel1Controller"
fx:id="lineChart1" animated="false" title="Channel 1"
onMouseClicked="#handleClick"
onScroll="#handleScroll"
onContextMenuRequested="#popUpChartContextMenu">
<xAxis>
<NumberAxis fx:id="timeAxis" lowerBound="0" upperBound="3000" tickUnit="50"
label="Time (ms)"/>
</xAxis>
<yAxis>
<NumberAxis fx:id="bandWidthAxis" side="LEFT" lowerBound="0" upperBound="1000" tickUnit="50"
label="Band Width (μs)"/>
</yAxis>
</LineChart>

Related

I have set a linechart in FXML with scene builder, how can I use it? I shoud add code in controller or main class? [duplicate]

This question already has answers here:
Dynamic Bar chart in Java FX
(2 answers)
Closed 8 months ago.
<LineChart fx:id="linechart_10mins" layoutY="-78.0" prefHeight="317.0" prefWidth="600.0" AnchorPane.bottomAnchor="54.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis side="LEFT" />
</yAxis>
</LineChart>
How can I use this linechart in my code?
Initialize linechart with #FXML annotation in your controller, and use your lineChart there for adding and showing data

TreeView in FXML

How can I create a TreeView in FXML including all the branches and components in it? I can't find documentation about it. There is just code about how to populate it but I want to do it in FXML so y can have the design apart of the logic.
There's no specific documentation for FXML (beyond the Introduction to FXML document): but there is no need for any. An element beginning with an uppercase character is an instruction to instantiate the class of that name, so
<TreeView></TreeView>
will instantiate a TreeView. Attributes correspond to properties, so if you needed you could do
<TreeView editable="true"></TreeView>
Nested elements beginning with lower case also correspond to properties, and will be set to the enclosed FXML structure. So you can create a tree view with code like
<TreeView fx:id = "treeView">
<root>
<TreeItem fx:id="rootItem" value="Root" expanded="true">
<children>
<TreeItem fx:id="child1" value="Child 1" expanded="true">
<children>
<TreeItem fx:id="child11" value="Child 1.1"></TreeItem>
<TreeItem fx:id="child12" value="Child 1.2"></TreeItem>
</children>
</TreeItem>
<TreeItem fx:id="child2" value="Child 2">
<children>
<TreeItem fx:id="child21" value="Child 2.1"></TreeItem>
<TreeItem fx:id="child22" value="Child 2.2"></TreeItem>
</children>
</TreeItem>
</children>
</TreeItem>
</root>
</TreeView>
You can, of course, see all the properties in the API documentation.

How can I add items to a ComboBox (or other Control) using FXML?

Recently, I discovered that <ComboBox>—and other controls—can have an <items> element underneath them .
How can I populate, or add items right to a control in the FXML markup?
(One use case for this might be to make the FXML semi-functional as a mockup to show to stakeholders.)
Research proves that it's done with a combination of the fx:value and fx:factory attributes. These seem to have been added in JavaFX 8 JavaFX 2.
Below, I'll cite the mechanisms and then give some examples.
🦶🔫 Warning:
Note, as #fabian does, that though this works well in the short term for something like a prototype or mockup, adding items directly to the FXML breaks the separation between model and view—and that may likely be an undesired result in the long term.
The Mechanisms
fx:value
The fx:value attribute can be used to initialize an instance of a type that does not have a default constructor but provides a static valueOf(String) method. For example, java.lang.String as well as each of the primitive wrapper types define a valueOf() method and can be constructed in FXML as follows:
<String fx:value="Hello, World!"/>
<Double fx:value="1.0"/>
<Boolean fx:value="false"/>
Custom classes that define a static valueOf(String) method can also be constructed this way.
Source: JavaFX 2 Introduction to FXML
fx:factory
The fx:factory attribute is another means of creating objects whose classes do not have a default constructor. The value of the attribute is the name of a static, no-arg factory method for producing class instances. For example, the following markup creates an instance of an observable array list, populated with three string values:
<FXCollections fx:factory="observableArrayList">
<String fx:value="A"/>
<String fx:value="B"/>
<String fx:value="C"/>
</FXCollections>
Source: JavaFX 2 Introduction to FXML
Some Examples:
ComboBox
<ComboBox value="One">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="Three"/>
<String fx:value="Two"/>
<String fx:value="One"/>
</FXCollections>
</items>
</ComboBox>
CheckComboBox
The ControlsFX Controls are a little different:
<CheckComboBox>
<items>
<String fx:value="One"/>
<String fx:value="Two"/>
<String fx:value="Three"/>
</items>
</CheckComboBox>
TableView
TableView gets a little more complicated because it needs CellValueFactorys to know which part of the Person to show in each column.
<TableView prefHeight="200.0" prefWidth="200.0">
<columns>
<TableColumn text="Name">
<cellValueFactory>
<PropertyValueFactory property="name" />
</cellValueFactory>
</TableColumn>
<TableColumn text="Comment">
<cellValueFactory>
<PropertyValueFactory property="comment" />
</cellValueFactory>
</TableColumn>
</columns>
<items>
<FXCollections fx:factory="observableArrayList">
<Person name="Jacob" comment="Hey!"/>
<Person name="Isabella" comment="Dude, we're in FXML!"/>
<Person name="Ethan" comment="No way!"/>
</FXCollections>
</items>
</TableView>

What is the Function used for FXML comboBox in JavaFX

I have defined the combo Box in javaFX FXML file now i want to define it's function to get the value of Combo Box and use it in my code.
The FXML file is
<ComboBox fx:id="select_pc" promptText="Select PC">
<HBox.margin>
<Insets left="20.0" top="35.0" />
</HBox.margin>
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="ForkLift" />
<String fx:value="Gates" />
</FXCollections>
</items>
</ComboBox>
now can anybody tell me how to write a function to take the value of combo Box and use it in the String form

Stretch Tableview to content size

I have a TableView inside a JavaFX ScrollPane, together with some other Views parallel to it. Something like this here:
<ScrollPane maxHeight="Infinity" maxWidth="Infinity" VBox.vgrow="ALWAYS">
<GridPane maxHeight="Infinity" maxWidth="Infinity" prefWidth="1100">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="Infinity" minWidth="0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="Infinity" minWidth="0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="Infinity" minHeight="10.0" vgrow="ALWAYS" />
</rowConstraints>
<VBox GridPane.columnIndex="1" GridPane.rowSpan="2147483647">
<PieChart fx:id="amountPerState" legendVisible="false" minHeight="300" minWidth="300" />
<PieChart fx:id="amountPerComponent" legendVisible="false" minHeight="300" minWidth="300" />
</VBox>
<TableView fx:id="componentTable" maxHeight="Infinity" maxWidth="Infinity" GridPane.rowIndex="6">
<columns>
...
</columns>
</TableView>
</GridPane>
</ScrollPane>
Now this double scrolling from the ScrollPane and the TableView is ackward. When I try to scroll inside the TableView the ScrollPane also scrolls. I would love to have the TableView go as high as necessary for it's content, so that there is only the scrolling of the ScrollPane. Any Idea how I can do that? Or other ideas?
Edit: For extra clarification, I assume the TableView should have prefered size so high that all it's content is shown without any scrolling necessary. Then there are probably no scroll bars, or I can just not show them with the policy. This is how I think you would achieve this, but I don't know how to figure out the tables content hight to make it it's preferred size.
I think it will be a bit worse in performance, since TableView has this row reusing, but I know that there are not to many rows in the TableView, so that there will be no performance problems.
Edit: Both the charts and the table are to big for the windows, so they all need to be in the ScrollPane

Resources