What is the Function used for FXML comboBox in JavaFX - 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

Related

Random characters instead of String input in Combobox Items?

I am trying to create a choicebox in my application using javafx/scenebuilder in IntelliJ but so far am not able to get Strings to show as menu items - in the example screenshot below I have just tried to create an option with the letter C? The code I have used is below. Any ideas what I am doing wrong? I thought it might be a font issue but I can't find a way to change the font of the menu items.
<AnchorPane prefHeight="45.0" prefWidth="127.5">
<children>
<Text fill="WHITE" strokeType="OUTSIDE" strokeWidth="0.0" text="I" AnchorPane.leftAnchor="11.0" AnchorPane.topAnchor="4.0">
<font>
<Font name="Courier Bold" size="40.0" />
</font>
</Text>
<ChoiceBox fx:id="keyChoice" prefWidth="79.0" style="-fx-background-color: #FBC5B8;" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="8.0">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="C" />
</FXCollections>
</items>
</ChoiceBox>
</children>
</AnchorPane>
So this was a font issue as suspected, couldn't find a way to change it in FXML so created a CSS file which works fine.
.choice-box .menu-item .label {
-fx-font-family: "monospace";
}
.choice-box {
-fx-font-family: "monospace";
}

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

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>

Reuse the same FXML declaration for menu bar and context menu

I have declared a menu bar in FXML with a bunch menu items (containing graphics, onClick method links etc...).
Now I'm creating a context menu for a table, and I'd like to put in there all the menu items for the "Edit" menu of the menu bar.
Is there a DRY way of doing so in FXML?
I don't like the idea of copying all the FXML declarations of the menu items, and having to maintain both sets of items.
I know I could reuse the items if I declared them in Java code, but I'd like to keep all my layout in FXML.
Here is the FXML for the edit menu, that I don't want to duplicate:
<Menu text="_Edit">
<MenuItem onAction="#copyRaw" text="Copy _raw log">
<accelerator>
<KeyCodeCombination alt="UP" code="C" control="DOWN" meta="UP" shift="UP" shortcut="UP" />
</accelerator>
<graphic>
<Glyph fontFamily="FontAwesome" icon="copy" />
</graphic>
</MenuItem>
<MenuItem onAction="#copyPretty" text="Copy with _columns">
<accelerator>
<KeyCodeCombination alt="UP" code="C" control="DOWN" meta="UP" shift="DOWN" shortcut="UP" />
</accelerator>
<graphic>
<Glyph fontFamily="FontAwesome" icon="copy" />
</graphic>
</MenuItem>
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem onAction="#selectAll" text="Select _All">
<accelerator>
<KeyCodeCombination alt="UP" code="A" control="DOWN" meta="UP" shift="UP" shortcut="UP" />
</accelerator>
</MenuItem>
<MenuItem mnemonicParsing="false" onAction="#unselectAll" text="Unselect All" />
</Menu>
I'm not sure that my idea is best but u should to do it like this:
You can create a package in resources with most used elements as Buttons,Tables,Labels etc. for each assign an fx:id and include that in another fxmls.
Example:
PACKAGE: resource/package/name/utils/Label.fxml
<?import javafx.scene.control.Label?>
<?import javafx.geometry.Insets?>
<Label xmlns:fx="http://javafx.com/fxml"
fx:id="label"
style="-fx-background-color: red;
-fx-font: bold;
-fx-font-size: 30px;"
text="Hello world">
<padding>
<Insets top="5" left="5" right="5" bottom="5"/>
</padding>
</Label>
<!--Add all your nodes there-->
PACKAGE: resource/package/name/Main.fxml
<GridPane xmlns:fx="http://javafx.com/fxml" alignment="TOP_CENTER" hgap="10" vgap="10">
<fx:include source="Label.fxml"/> <!--There will be displayed all elements from Label.fxml-->
</GridPane>
In your case you just need to set fx:id for your Menu item and import in another fxml.
Good luck.

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