java


How to get the current item after the FXML initialization?


I tryed to get the current view of my application and set the item of a menu depending of the view, my fxml looks something like that:
<StackPane fx:id="rootNode" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="628.0" prefWidth="1185.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.AccountRegistrationController">
<stylesheets>
<URL value="#../css/jfoenix-components.css" />
<URL value="#../css/main.css" />
</stylesheets>
<children>
<BorderPane prefHeight="740.0" prefWidth="1185.0">
<left>
<!-- this is my menu-->
<fx:include source="Menu.fxml"/>
</left>
<top>
</top>
<center>
</center>
<left>
</left>
</BorderPane>
</children>
And my menu looks like this:
<VBox fx:id="menu" maxWidth="240.0" prefWidth="240.0" minWidth="240.0" fx:controller="controller.MenuController" xmlns:fx="http://javafx.com/fxml/1" styleClass="menu">
<children>
<GridPane>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="14.0" minWidth="14.0" prefWidth="14.0"/>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="180.0" minWidth="180.0" prefWidth="180.0"/>
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" vgrow="SOMETIMES"/>
</rowConstraints>
<children>
<JFXButton fx:id="mnFactories" alignment="TOP_LEFT" prefHeight="35.0" prefWidth="180.0"
styleClass="menu-item" text="Fábricas" GridPane.columnIndex="1" onAction="#goTo"/>
<FontAwesomeIconView glyphName="INDUSTRY" glyphStyle="-fx-fill: rgb(93, 135, 156)" size="14px"/>
</children>
</GridPane>
<GridPane>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="14.0" minWidth="14.0" prefWidth="14.0"/>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="180.0" minWidth="180.0" prefWidth="180.0"/>
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" vgrow="SOMETIMES"/>
</rowConstraints>
<children>
<JFXButton fx:id="mnProducts" alignment="TOP_LEFT" prefHeight="35.0" prefWidth="180.0"
styleClass="menu-item" text="Productos" GridPane.columnIndex="1" onAction="#goTo"/>
<FontAwesomeIconView glyphName="SUITCASE" glyphStyle="-fx-fill: rgb(93, 135, 156)" size="14px"/>
</children>
</GridPane>
<GridPane>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="14.0" minWidth="14.0" prefWidth="14.0"/>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="180.0" minWidth="180.0" prefWidth="180.0"/>
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" vgrow="SOMETIMES"/>
</rowConstraints>
<children>
<JFXButton fx:id="mnTags" alignment="TOP_LEFT" prefHeight="35.0" prefWidth="180.0"
styleClass="menu-item" text="Tags" GridPane.columnIndex="1" onAction="#goTo"/>
<FontAwesomeIconView glyphName="TAG" glyphStyle="-fx-fill: rgb(93, 135, 156)" size="14px"/>
</children>
</GridPane>
<GridPane>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="14.0" minWidth="14.0" prefWidth="14.0"/>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="180.0" minWidth="180.0" prefWidth="180.0"/>
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" vgrow="SOMETIMES"/>
</rowConstraints>
<children>
<JFXButton fx:id="mnUsers" alignment="TOP_LEFT" prefHeight="35.0" prefWidth="180.0"
styleClass="menu-item" text="Usuarios" GridPane.columnIndex="1" onAction="#goTo"/>
<FontAwesomeIconView glyphName="USERS" glyphStyle="-fx-fill: rgb(93, 135, 156)" size="14px"/>
</children>
</GridPane>
</children>
So now when I clicked for example in "Usuarios" option the class of this item change the class to active-menu-item.
First I manage my fxml views with this functions:
#FXML
public void goTo (ActionEvent evt) {
updateActiveItem(((JFXButton)evt.getSource()));
}
private void updateActiveItem (JFXButton item) {
if (item.getStyleClass().contains("active-menu-item")) {
// Es la página actualmente mostrada
} else {
setView(item.getText(), item);
}
}
public boolean setView(String option, Node node) {
try {
if (Main.getSceneObjects() == null) {
System.out.println("No se encontraron los elementos necesarios para cargar la vista");
return false;
} else {
Parent root = (Parent) Main.getSceneObjects()[0];
Stage stage = (Stage) node.getScene().getWindow() ;
switch (option.toLowerCase()) {
case "productos":
root = FXMLLoader.load(getClass().getResource("../view/fxml/products.fxml"));
break;
case "tags":
root = FXMLLoader.load(getClass().getResource("../view/fxml/tags.fxml"));
break;
case "usuarios":
root = FXMLLoader.load(getClass().getResource("../view/fxml/accountRegistration.fxml"));
break;
default:
System.out.println("La opción seleccionada no existe");
break;
}
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
setCurrentItem(node);
return true;
}
} catch (IOException ex) {
return false;
}
}
And finally set the current option active with this function:
public void setCurrentItem (Node node) {
for (Node child : menu.getChildren()) {
int index = 0;
for (Node _child : ((GridPane)child).getChildren()) {
if (index == 0) {
if (_child.getStyleClass().contains("active-menu-item")) {
_child.getStyleClass().remove("active-menu-item");
_child.getStyleClass().add("menu-item");
}
} else {
if (_child.getClass().toString().contains("class de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView")) {
if (((FontAwesomeIconView)_child).getGlyphStyle().contains("-fx-fill: #FFF")) {
((FontAwesomeIconView)_child).setGlyphStyle("-fx-fill: rgb(93, 135, 156)");
}
}
}
index ++;
}
}
node.getStyleClass().add("active-menu-item");
for (Node currentChild : ((GridPane)node.getParent()).getChildren()) {
System.out.println("ENTROO en el for del boton");
if (currentChild.getStyleClass().contains("glyph-icon")) {
((FontAwesomeIconView)currentChild).setGlyphStyle("-fx-fill: #FFF");
}
}
}
But when I set the scene, the current active option of the menu no change, what happened?, I need to do something else or this cant work because I use two controllers in every fxml?

Related Links

Whenever I run the android login application it just crashes an a series of error messages in log
measure short distance android GPS
What does android:progressTintMode, android:secondaryProgressTintMode and android:progressDrawable do?
Exception in thread “main” java.lang.NoSuchMethodError: com.fasterxml.jackson.core.JsonFactory.requiresPropertyOrdering()Z
Enter multiple numbers in console
Getting file name of file being recieved on the server
Unsure about the best loop to use
Improve performance of reversing array
My Jsoup code extract elements within the first <td> tag of table only
In Java Windows Application using PdfBox Proguard zaps me
Consuming Spring Hateoas Restservice with RestTemplate
MASON Get Moore Neighbors
Java “no such element exception”
Trying to refer to a final ImageView does not work
Connect to tor with java - TorLib
Why won't this sorting work

Categories

HOME
view
dot
objectgears
react-router
mean-stack
paw-app
blueprintjs
fancybox
elasticsearch-hadoop
vifm
dax
quicklook
export-to-csv
circular-dependency
windows-server-2012
ups
graphlab
saxon
shared-hosting
viewport
predix
custom-wordpress-pages
errorlevel
tapestry
tibco-mdm
crosstab
poltergeist
wpfdatagrid
intel-pin
fopen
web-api-testing
typo3-6.2.x
saas
log4js-node
large-file-upload
javascriptcore
google-cloud-endpoints-v2
google-qpx-express-api
textmate
suricata
unspecified
cookiecutter-django
eclipse-scout
ncalc
botbuilder
btrace
babel-core
atomicity
jedis
theming
paxos
clean-architecture
ionicons
colorama
akka-cluster
or-tools
setuptools
angular-cache
underscore.js-templating
moveit
ford-fulkerson
freedesktop.org
iad
vhd
python-3.2
atk4
citrus-pay
iis-arr
drawbitmap
riak-cs
processmodel
ami
key-management
javax.mail
npapi
kineticjs
modalpopup
javafx-webengine
flask-cors
responsive-slides
has-many-through
uitouch
expected-exception
resty-gwt
imdbpy
html5-notifications
multiprocessor
mysqltuner
farseer
web2py-modules
ruby-datamapper
bulkloader
qt-faststart
xamlparseexception
interface-orientation
sharp-repository
kyotocabinet
specification-pattern
first-responder
postgresql-performance
semantic-diff
assembly-loading
work-stealing
qt-jambi
blackberry-playbook
xmlspy
appender
propagation
imac
gallio
gamequery
.nettiers
noscript
text-coloring
dbisam
paul-graham
3gp

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html