Set SelectionModel for TableView in FXML - javafx

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...

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());

Fit size to loaded FXML

I was trying to resolve my problem by many ways. But nothing works.
I have a MainApplicationWindowController
#FXML
private AnchorPane mainApplicationWindow;
#FXML
private AnchorPane workAnchorPane;
#FXML
private AnchorPane workAnchorPaneContent;
And corresponding fxml
<SplitPane dividerPositions="0.22690763052208834" layoutX="55.0" layoutY="46.0"
prefHeight="160.0" prefWidth="200.0" styleClass="gt-splitpane"
AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0"
AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane id="splitPaneHorizontal" maxWidth="250.0" minWidth="250.0"
prefWidth="250.0">
<children>
<TableView fx:id="gtFamilyMemberTable" layoutX="20.0" layoutY="12.0"
onMouseClicked="#showInfoMember" prefHeight="200.0" prefWidth="200.0"
AnchorPane.bottomAnchor="40.0" AnchorPane.leftAnchor="10.0"
AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="5.0">
<columns>
<TableColumn fx:id="simNameColumn" maxWidth="110.0" minWidth="110.0"
prefWidth="-1.0" text="%simName"/>
<TableColumn fx:id="simSurnameColumn" maxWidth="110.0" minWidth="110.0"
prefWidth="-1.0" text="%simSurname"/>
</columns>
<styleClass>
<String fx:value="firstTypeTable"/>
<String fx:value="tableMembersAndRelations"/>
</styleClass>
</TableView>
<TableView fx:id="gtFamilyRelationTable" layoutX="20.0" layoutY="12.0"
onMouseClicked="#showInfoRelation" prefHeight="200.0"
prefWidth="200.0" AnchorPane.bottomAnchor="40.0"
AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0"
AnchorPane.topAnchor="5.0">
<columns>
<TableColumn fx:id="relationSimLeftColumn" maxWidth="85.0"
minWidth="85.0" prefWidth="-1.0" text="%relation_sim"/>
<TableColumn fx:id="relationTypeColumn" maxWidth="50.0" minWidth="50.0"
prefWidth="-1.0" text="%relation_type"/>
<TableColumn fx:id="relationSimRightColumn" maxWidth="85.0"
minWidth="85.0" prefWidth="-1.0" text="%relation_sim"/>
</columns>
<styleClass>
<String fx:value="firstTypeTable"/>
<String fx:value="tableMembersAndRelations"/>
</styleClass>
</TableView>
<ToggleButton fx:id="buttonShowMemberTable" layoutX="23.0"
mnemonicParsing="false" text="ToggleButton"
AnchorPane.bottomAnchor="10.0"/>
<ToggleButton fx:id="buttonShowRelationTable" layoutX="125.0"
mnemonicParsing="false" text="ToggleButton"
AnchorPane.bottomAnchor="10.0"/>
</children>
</AnchorPane>
<AnchorPane fx:id="workAnchorPane">
<children>
<AnchorPane fx:id="workAnchorPaneContent" styleClass="workingPane"
AnchorPane.bottomAnchor="40.0"
AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="20.0"
AnchorPane.topAnchor="5.0">
</AnchorPane>
</children>
</AnchorPane>
</items>
</SplitPane>
In workAnchorPaneContent I load another FXML using function from my SceenManager.class :
public FXMLPaneController loadFxml(FXMLPaneController controller, AnchorPane anchor, String fxml) {
FXMLLoader loader = new FXMLLoader(getClass().getResource(fxml), this.context.getBundle());
try {
anchor.getChildren().clear();
anchor.getChildren().addAll((AnchorPane) loader.load());
controller = loader.getController();
controller.setManager(this);
controller.setContext(this.context);
} catch (Exception ex) {
LOG.error(ex.getClass() + " - " + ex.getMessage());
LOG.error(ex.getCause());
ex.printStackTrace();
}
return controller;
}
The problem is that after load FXML in workAnchorPaneContent the size is not fitted to size of workAnchorPane despite setting
AnchorPane.bottomAnchor="40.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="5.0"
#Edit
Jest I was trying to bind size properties. But it didn't works for AnchorPane inside second controller.

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>

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

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>

Resources