Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » e(fx)clipse » FXML - dynamic tab creation
FXML - dynamic tab creation [message #1734707] Fri, 10 June 2016 13:06 Go to next message
Daniel Clarke is currently offline Daniel ClarkeFriend
Messages: 21
Registered: March 2016
Junior Member
Hi,

I've only just started looking at Java FX/FXML and MVC... so apologies if this is a stupid question...

I have created an FXML document which defines a TabPane and a Tab within it. This has a table within it which I populate from data in my model - all is good.

<TabPane fx:id="myTabPane" >
	<tabs>
		<Tab text="WANT THIS TO BE POPULATED BASED ON DATA IN MY MODEL!">
			<content>
				<GridPane alignment="CENTER" hgap="10.0" vgap="10.0" >
					<padding>
						<Insets  bottom="10.0" left="10.0" right="10.0" top="10.0" />
					</padding>
					<Label style="-fx-font: NORMAL 20 Tahoma;" text="People" GridPane.columnIndex="0" GridPane.rowIndex="0">
					</Label>
					<TableView fx:id="peopleTableView" GridPane.columnIndex="0" GridPane.rowIndex="1">
						<columns>
							<TableColumn text="First Name">
								<cellValueFactory>
									<PropertyValueFactory property="firstName" />
								</cellValueFactory>
							</TableColumn>
							<TableColumn text="Surname">
								<cellValueFactory>
									<PropertyValueFactory property="surname" />
								</cellValueFactory>
							</TableColumn>
						</columns>
					</TableView>
				</GridPane>
			</content>
		</Tab>
	</tabs>
</TabPane>



Now what I want to do is create a number of these tabs (the number depending upon how many of a certain element exist in my model). Then I want to populate the table on each tab with with data associated with that model element.

Is this possible to do using FXML? Or do I have to do this programmatically? So in FXML I can only get to the stage of defining my tabPane, the rest of the content of the tab I have to do in Java Code? (using myTabPane.getTabs.add() and then have to create a Tab object and the layout/table within Java code?)

Regards,
Dan
Re: FXML - dynamic tab creation [message #1734714 is a reply to message #1734707] Fri, 10 June 2016 13:55 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6349
Registered: July 2009
Senior Member
Well first of all yes FXML is static so you can't get any dynamics into
it and when ever something is dynamic you need to do this in an
imperative language like Java.

In JavaFX when used with FXML this is typically the controller who is
implemented for example in Java.

Still there's no need to make up the UI in Code because you can load
FXML dynamically at will so if you structure your FXMLs differently you
are good to go.

main.fxml (MainController)
page.fxml (PageController)


main.fxml:
----------
<TabPane fx:id="myTabPane">

<TabPane>




page.fxml
---------
<GridPane alignment="CENTER" hgap="10.0" vgap="10.0" >
<!-- ... -->
</GridPane>



MainController.java
--------------------

class MainController implements Initializable {

@FXML
TabPane tb;

public initialize(....) {
for( int i = 0; i < 10; i++ ) {
Tab t = new Tab("Tab " + i);
t.setContent(FXMLLoader.load(page.fxml));
tb.getTabs().add(t);
}
}

}


Tom

On 10.06.16 15:06, Daniel Clarke wrote:
> Hi,
>
> I've only just started looking at Java FX/FXML and MVC... so apologies
> if this is a stupid question...
>
> I have created an FXML document which defines a TabPane and a Tab within
> it. This has a table within it which I populate from data in my model -
> all is good.
>
>
> <TabPane fx:id="myTabPane" >
> <tabs>
> <Tab text="WANT THIS TO BE POPULATED BASED ON DATA IN MY MODEL!">
> <content>
> <GridPane alignment="CENTER" hgap="10.0" vgap="10.0" >
> <padding>
> <Insets bottom="10.0" left="10.0" right="10.0"
> top="10.0" />
> </padding>
> <Label style="-fx-font: NORMAL 20 Tahoma;"
> text="People" GridPane.columnIndex="0" GridPane.rowIndex="0">
> </Label>
> <TableView fx:id="peopleTableView"
> GridPane.columnIndex="0" GridPane.rowIndex="1">
> <columns>
> <TableColumn text="First Name">
> <cellValueFactory>
> <PropertyValueFactory
> property="firstName" />
> </cellValueFactory>
> </TableColumn>
> <TableColumn text="Surname">
> <cellValueFactory>
> <PropertyValueFactory
> property="surname" />
> </cellValueFactory>
> </TableColumn>
> </columns>
> </TableView>
> </GridPane>
> </content>
> </Tab>
> </tabs>
> </TabPane>
>
>
>
> Now what I want to do is create a number of these tabs (the number
> depending upon how many of a certain element exist in my model). Then I
> want to populate the table on each tab with with data associated with
> that model element.
>
> Is this possible to do using FXML? Or do I have to do this
> programmatically? So in FXML I can only get to the stage of defining my
> tabPane, the rest of the content of the tab I have to do in Java Code?
> (using myTabPane.getTabs.add() and then have to create a Tab object and
> the layout/table within Java code?)
>
> Regards,
> Dan
Re: FXML - dynamic tab creation [message #1735108 is a reply to message #1734714] Wed, 15 June 2016 15:11 Go to previous message
Daniel Clarke is currently offline Daniel ClarkeFriend
Messages: 21
Registered: March 2016
Junior Member
Perfect! Exactly what I was after.

Thanks a lot!
Previous Topic:Move to github?
Next Topic:Access restriction on JavaFX classes in Eclipse Neon, e(fx)clipse 2.4.0
Goto Forum:
  


Current Time: Tue Aug 22 15:03:01 GMT 2017

Powered by FUDForum. Page generated in 0.03159 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software