How to define nested columns in JavaFX within the fxml file? - javafx

I would like produce the following table using fxml:
So, I wonder how can I nest the columns Primary and Secondary inside Email Address?

After checking some code in this link, I was able to figure it out.
<TableView fx:id="tableView" GridPane.columnIndex="0" GridPane.rowIndex="1">
<columns>
<TableColumn fx:id="firstNameColumn" text="First Name" prefWidth="100"/>
<TableColumn text="Last Name" prefWidth="100"/>
<TableColumn text="Email Address" prefWidth="300">
<columns>
<TableColumn text ="Primary" prefWidth="150"/>
<TableColumn text ="Secondary" prefWidth="150"/>
</columns>
</TableColumn>
</columns>
</TableView>

Related

Usage of USE_COMPUTED_SIZE in TableColumn

Is it possible to use USE_COMPUTED_SIZE for the prefered width of a TableColumn? I have tried but my column then disappear.
Maybe that it is nonsense?
<TableView id="my-table" fx:id="tagTable" editable="true" stylesheets="#My_Theme.css">
<columns>
<TableColumn fx:id="typeColumn" editable="false" maxWidth="-Infinity" minWidth="-Infinity" prefWidth="-1.0" resizable="false" sortable="false" text="Type" />
<TableColumn fx:id="contentColumn" editable="false" maxWidth="-Infinity" minWidth="-Infinity" prefWidth="-1.0" resizable="false" text="CONTENT" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
<placeholder>
<Label styleClass="label-dark" text="Nothing to display">
<padding>
<Insets bottom="200.0" />
</padding>
</Label>
</placeholder>
</TableView>
How I do it, which might not be "the" way you should or want to do it is:
I set all the TableColumns to have the Min and Pref- Width set to USE_COMPUTED_SIZE while the Max Width have the default value of 5000. (I tried setting Max Width to MAX_VALUE but that just ends up with the first column taking up the entire table, and I haven't looked up why that is yet since it works and that it good enough for me :) ).
Also, for the TableView I have the Column Resize Policy set to "constrained-resize" just as you have.
In the image below you can see the results I get, which is 5 evenly sized columns:
I hope that this might be of some help, and that others may comment if there is a better way or a way in which you can set the Max Width to MAX_VALUE.
Edit: I'm using Java 8 (1.8.181).

How can i add serial no to my current list which i need to add observable list in javafx so that displays in table view

This is my fxml file code which displays some search criteria and then table view
<Pane xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.medplus.posoffline.gui.ReturnsPickListController">
<children>
<Pane prefHeight="600.0" prefWidth="800.0">
<children>
<Label layoutX="5.0" layoutY="15.0" text="RequestId" />
<TextField fx:id="requestId" layoutX="75.0" layoutY="15.0" prefWidth="140.0" />
<Label layoutX="245.0" layoutY="15.0" text="Status" />
<ComboBox fx:id="returnStatusComb" layoutX="295.0" layoutY="15.0" prefWidth="140.0">
</ComboBox>
<Label layoutX="470.0" layoutY="15.0" text="Type" />
<ComboBox fx:id="returnTypeComb" layoutX="510.0" layoutY="15.0" prefWidth="140.0">
</ComboBox>
<Button layoutX="598.0" layoutY="60.0" mnemonicParsing="false" onAction="#searcReturnDetailsfroPickList" text="Search" />
<TableView fx:id="tableView" layoutY="92.0" prefHeight="500.0" prefWidth="800.0">
<columns>
<TableColumn fx:id="requestIds" minWidth="160.0" prefWidth="75.0" text="ReqId" />
<TableColumn fx:id="status" minWidth="160.0" prefWidth="75.0" text="Status" />
<TableColumn fx:id="type" minWidth="160.0" prefWidth="75.0" text="Type" />
<TableColumn fx:id="dateCreated" minWidth="160.0" prefWidth="75.0" text="DateCreated" />
<TableColumn fx:id="showDetailsBtn" minWidth="160.0" prefWidth="75.0" text="Action" />
</columns>
</TableView>
</children>
</Pane>
</children>
</Pane>
if(UtilValidate.isNotEmpty(returnRequests)) {
observableProductList.addAll(returnRequests);
requestIds.setCellValueFactory(new PropertyValueFactory<ReturnRequest, Long>("requestId"));
status.setCellValueFactory(new PropertyValueFactory<ReturnRequest, ReturnRequestType>("status"));
type.setCellValueFactory(new PropertyValueFactory<ReturnRequest, ReturnRequestStatus>("type"));
dateCreated.setCellValueFactory(new PropertyValueFactory<ReturnRequest, Date>("dateCreated"));
tableView.setItems(observableProductList);
}else {
alert.setTitle("Info");
alert.setContentText("No Data found");
alert.showAndWait();
}
clearData();
I want to add serial numbers to list in order to show how many records got displayed in my table.
Now I'm able to display just data in table perfectly I just to know how many records got display by showing serial number does any method in javafx can display according to their index in list?
or any other alternative solution let me know please
Thanks in advance
If you simply want to display the index in the items list of the TableView in a TableCell, I recommend using a custom TableCell implementation for this purpose:
public static <T> Callback<TableColumn<T, Void>, TableCell<T, Void>> indexCellFactory() {
return t -> new TableCell<T, Void>() {
#Override
public void updateIndex(int i) {
super.updateIndex(i);
setText(isEmpty() ? "" : Integer.toString(i));
}
};
}
TableColumn<MyItem, Void> indexColumn = new TableColumn<>("Row index");
indexColumn.setCellFactory(indexCellFactory());

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

JavaFX Turn an FXML tag to an Object for the controller class

im having issues with my code, i am a very new guy to javafx and im not quite sure of what im doing at all(I do ofc, just not enough)
I need to find the way to add data to my table (TableView is fxml) with my Controller Class, i have no idea on how to do this, suggest the best way.. i thought of maybe if i could create an object of that TableView based on the fxml table view ...
Help guys...
<TableView fx:id="tableView" prefHeight="200.0" prefWidth="200.0" StackPane.alignment="CENTER">
<columns>
<TableColumn editable="false" maxWidth="90.0" prefWidth="78.0" text="Fecha" />
<TableColumn editable="false" maxWidth="90.0" text="Hora" />
<TableColumn editable="false" maxWidth="90.0" prefWidth="79.0" text="Folio" />
<TableColumn editable="false" maxWidth="90.0" prefWidth="77.0" text="Estacion" />
<TableColumn editable="false" maxWidth="190.0" prefWidth="160.0" text="Circuito" />
<TableColumn editable="false" maxWidth="190.0" prefWidth="160.0" text="Receta" />
<TableColumn editable="false" maxWidth="190.0" prefWidth="160.0" text="Usuario" />
</columns>
<StackPane.margin>
<Insets bottom="8.0" left="8.0" top="8.0" />
</StackPane.margin>
</TableView>

Set SelectionModel for TableView in FXML

I want to set the SelectionModel of the TableView from the FXML, but I can not find how to do this. I already tried the following:
1.Just set it as a property of the TableView:
<TableView selectionModel="MULTIPLE">
2.Set the property the same as the ListView works (see: https://community.oracle.com/thread/2315611?start=0&tstart=0):
<TableView multiSelect="true">
3.Set the property in a different way:
<TableView>
<selectionModel>
<TableView fx:constant="MULTIPLE" />
</selectionModel>
</TableView>
4.Another version:
<TableView>
<selectionModel>
<SelectionModel fx:constant="MULTIPLE" />
</selectionModel>
</TableView>
5.Selection model (different):
<TableView>
<selectionModel>
<SelectionModel selectionModel="MULTIPLE" />
</selectionModel>
</TableView>
None of this works.
Any help is greatly appreciated!
Should it be possible on FXML this should be the way:
<TableView fx:id="table" prefHeight="200.0" prefWidth="200.0" >
<columns>
<TableColumn prefWidth="75.0" text="C1" />
</columns>
<selectionModel>
<SelectionMode fx:constant="MULTIPLE"/>
</selectionModel>
</TableView>
Unfortunately, when you run it you get an exception:
java.lang.IllegalArgumentException: Unable to coerce SINGLE to class javafx.scene.control.TableView$TableViewSelectionModel.
at com.sun.javafx.fxml.BeanAdapter.coerce(BeanAdapter.java:495)
This is happening because the bean adapter tries reflexively to find in the class javafx.scene.control.TableView$TableViewSelectionModel the valueOf of javafx.scene.control.SelectionMode.MULTIPLE, but it doesn't find it.
There's an unresolved JIRA ticket for this here.
The only working solution I've found, based on that report, is using scripting capabilities:
...
<?language javascript?>
<TableView fx:id="table" prefHeight="200.0" prefWidth="200.0" >
<columns >
<TableColumn fx:id="col" prefWidth="75.0" text="C1" />
</columns>
</TableView>
<fx:script>
table.getSelectionModel().setSelectionMode(javafx.scene.control.SelectionMode.MULTIPLE);
</fx:script>
Which is the same as doing it by code...

Resources