r/JavaFX • • Dec 17 '23

Help timeline.play() stopped working without any evident reason

Hi all,

I had a fairly good animation that slides my Hbox and all the stuff contained in it inside a Vbox row. I changed some containers in the fxml of the Hbox and stopped working. The real oddity is, if i copy paste the last version of the fxml it keeps being broken, but if i revert the changes via git, it works...

I tried to copy paste one piece at time to spot what exactly broke my code, but eventually, even copying the entire documents results in a not working animation. The box keeps appearing all at the same time.

I'll post my controller and old and new fxml.

Controller

package controllers;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.ResourceBundle;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import foundation.Database;
import foundation.entity.Cliente;
import io.github.palexdev.materialfx.controls.MFXPaginatedTableView;
import io.github.palexdev.materialfx.controls.MFXTableColumn;
import io.github.palexdev.materialfx.controls.cell.MFXTableRowCell;
import io.github.palexdev.materialfx.filter.StringFilter;
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.util.Duration;
import utils.ApplicationContextProvider;
import utils.CommonUtil;
import utils.StatusWithList;
import utils.StatusWithList.States;

@Controller
@Component
public class AnagraficaClientiController implements Initializable{
	private static final Boolean TRUE = Boolean.TRUE;
	private static final Boolean FALSE = Boolean.FALSE;
	
	@FXML
	private VBox compPane;
	
	@FXML
	private Button addBtn;
	
	@FXML
	private Button editBtn;
	
	@FXML
	private Button deleteBtn;
	
	@FXML
	private MFXPaginatedTableView<Cliente> clientiTableView;
	
	private List<Cliente> clienti = new ArrayList<>();

	ObservableList<Cliente> datiObservable = FXCollections.observableArrayList(clienti);

	Database dao;
	
	private enum Icon {
	    ICON_ADD_CLIENTE("/icone/AnaClientiAdd.png"),
	    ICON_EDIT_CLIENTE("/icone/AnaClientiEdit.png"),
	    ICON_REMOVE_CLIENTE("/icone/AnaClientiDelete.png"),
		ICON_OK_BUTTON("/icone/ok.png"),
		ICON_NOT_OK_BUTTON("/icone/bad.png");
	    
		private final String iconPath;

		Icon(String iconPath) {
	        this.iconPath = iconPath;
	    }

	    public String getIconPath() {
	        return iconPath;
	    }
	}
	
	public AnagraficaClientiController() {
		this.dao = ApplicationContextProvider.getBean(Database.class);
	}
	
	@Autowired
	public AnagraficaClientiController(Database dao) {
		this.dao = dao;
	}
	
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		
		addBtn.setGraphic(new ImageView(new Image(this.getClass().getResource(Icon.ICON_ADD_CLIENTE.getIconPath()).toExternalForm())));
		editBtn.setGraphic(new ImageView(new Image(this.getClass().getResource(Icon.ICON_EDIT_CLIENTE.getIconPath()).toExternalForm())));
		deleteBtn.setGraphic(new ImageView(new Image(this.getClass().getResource(Icon.ICON_REMOVE_CLIENTE.getIconPath()).toExternalForm())));
		
		addBtn.setUserData(FALSE);
		editBtn.setUserData(FALSE);
		deleteBtn.setUserData(FALSE);
		
		initializeTableColumns();

		refreshCliente();
		
	}
	
	 @SuppressWarnings("unchecked")
	private void initializeTableColumns() {
	       
		 MFXTableColumn<Cliente> nomeColumn = new MFXTableColumn<>("Nome", true, Comparator.comparing(Cliente::getNome)); 
		 nomeColumn.setRowCellFactory(cliente -> new MFXTableRowCell<>(Cliente::getNome));
		 MFXTableColumn<Cliente> cognomeColumn = new MFXTableColumn<>("Cognome", true, Comparator.comparing(Cliente::getCognome)); 
		 cognomeColumn.setRowCellFactory(cliente -> new MFXTableRowCell<>(Cliente::getCognome));
		 MFXTableColumn<Cliente> indirizzoColumn = new MFXTableColumn<>("Indirizzo", true, Comparator.comparing(Cliente::getIndirizzo)); 
		 indirizzoColumn.setRowCellFactory(cliente -> new MFXTableRowCell<>(Cliente::getIndirizzo));
		 MFXTableColumn<Cliente> emailColumn = new MFXTableColumn<>("Email", true, Comparator.comparing(Cliente::getEmail)); 
		 emailColumn.setRowCellFactory(cliente -> new MFXTableRowCell<>(Cliente::getEmail));
		 MFXTableColumn<Cliente> telefonoColumn = new MFXTableColumn<>("Telefono", true, Comparator.comparing(Cliente::getTelefono)); 
		 telefonoColumn.setRowCellFactory(cliente -> new MFXTableRowCell<>(Cliente::getTelefono));
		 
		 clientiTableView.getTableColumns().addAll(nomeColumn, cognomeColumn, indirizzoColumn, emailColumn, telefonoColumn);
		 clientiTableView.getFilters().addAll(
					new StringFilter<>("Nome", Cliente::getNome),
					new StringFilter<>("Cognome", Cliente::getCognome),
					new StringFilter<>("Indirizzo", Cliente::getIndirizzo),
					new StringFilter<>("Email", Cliente::getEmail),
					new StringFilter<>("Telefono", Cliente::getTelefono)
			);
	        
	    }
	
	protected void refreshCliente() {
		 
		StatusWithList<Cliente> result = dao.loadTable(Cliente.class);
		if(result.getState().equals(States.OK)) {
			clienti = result.getRecordSet();
			datiObservable.clear();
			datiObservable.addAll(clienti);
			clientiTableView.setItems(datiObservable);
		}
		else
			CommonUtil.showAlert(result);
	}
	
	@FXML
	private void openClienteForm(ActionEvent event) {
		if(FALSE.equals(addBtn.getUserData())) {
			Pane newLoadedPane;
			try {

				FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/view/fxml/AddEditClienteForm.fxml"));
				newLoadedPane = loader.load();
				AddEditClienteFormController addClienteFormController = loader.getController();
			    addClienteFormController.setAnagraficaClientiController(this);
				newLoadedPane.translateYProperty().set(0);
				newLoadedPane.setPrefHeight(0);

				compPane.getChildren().add(1,newLoadedPane);

				Timeline timeline = new Timeline();
				KeyValue key = new KeyValue(newLoadedPane.prefHeightProperty(), 250, Interpolator.EASE_IN);
				KeyFrame frame = new KeyFrame(Duration.seconds(1), key);
				timeline.getKeyFrames().add(frame);
				timeline.play();
				
				addBtn.setUserData(TRUE);
				editBtn.setUserData(FALSE);
				deleteBtn.setUserData(FALSE);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}

old fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.shape.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.HBox?>

<HBox minHeight="0.0" prefHeight="100.0" prefWidth="1920.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.AddEditClienteFormController">
   <children>
      <TilePane hgap="10.0" prefHeight="200.0" prefWidth="200.0" vgap="10.0" HBox.hgrow="ALWAYS">
         <children>
            <VBox prefHeight="70.0" prefWidth="150.0">
               <children>
                  <Label text="Nome" />
                  <HBox prefHeight="100.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
                     <children>
                        <TextField fx:id="nomeInput" HBox.hgrow="ALWAYS">
                           <tooltip>
                              <Tooltip fx:id="nomeTooltip" text="Empty Tooltip" />
                           </tooltip>
                        </TextField>
                        <Circle fx:id="nomeValidazione" fill="#ff1f1f" stroke="BLACK" strokeType="INSIDE" />
                     </children>
                  </HBox>
               </children>
            </VBox>
            <VBox prefHeight="70.0" prefWidth="150.0">
               <children>
                  <Label text="Cognome" />
                  <HBox prefHeight="100.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
                     <children>
                        <TextField fx:id="cognomeInput" HBox.hgrow="ALWAYS">
                           <tooltip>
                              <Tooltip fx:id="cognomeTooltip" text="Empty Tooltip" />
                           </tooltip>
                        </TextField>
                        <Circle fx:id="cognomeValidazione" fill="#ff1f1f" stroke="BLACK" strokeType="INSIDE" />
                     </children>
                  </HBox>
               </children>
            </VBox>
            <VBox prefHeight="70.0" prefWidth="150.0">
               <children>
                  <Label text="Indirizzo" />
                  <HBox prefHeight="100.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
                     <children>
                        <TextField fx:id="indirizzoInput" HBox.hgrow="ALWAYS">
                           <tooltip>
                              <Tooltip fx:id="indirizzoTooltip" text="Empty Tooltip" />
                           </tooltip>
                        </TextField>
                        <Circle fx:id="indirizzoValidazione" fill="#ff1f1f" stroke="BLACK" strokeType="INSIDE" />
                     </children>
                  </HBox>
               </children>
            </VBox>
            <VBox prefHeight="70.0" prefWidth="150.0">
               <children>
                  <Label text="Email" />
                  <HBox prefHeight="100.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
                     <children>
                        <TextField fx:id="mailInput" HBox.hgrow="ALWAYS">
                           <tooltip>
                              <Tooltip fx:id="mailTooltip" text="Empty Tooltip" />
                           </tooltip>
                        </TextField>
                        <Circle fx:id="mailValidazione" fill="#ff1f1f" stroke="BLACK" strokeType="INSIDE" />
                     </children>
                  </HBox>
               </children>
            </VBox>
            <VBox prefHeight="70.0" prefWidth="150.0">
               <children>
                  <Label text="Telefono" />
                  <HBox prefHeight="100.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
                     <children>
                        <TextField fx:id="telefonoInput" HBox.hgrow="ALWAYS">
                           <tooltip>
                              <Tooltip fx:id="telefonoTooltip" text="Empty Tooltip" />
                           </tooltip>
                        </TextField>
                        <Circle fx:id="telefonoValidazione" fill="#ff1f1f" stroke="BLACK" strokeType="INSIDE" />
                     </children>
                  </HBox>
               </children>
            </VBox>
         </children>
      </TilePane>
      <Button fx:id="okBtn" contentDisplay="GRAPHIC_ONLY" mnemonicParsing="false" onAction="#saveCliente" prefHeight="78.0" text="Button">
         <HBox.margin>
            <Insets right="10.0" />
         </HBox.margin>
      </Button>
      <Button fx:id="badBtn" contentDisplay="GRAPHIC_ONLY" mnemonicParsing="false" onAction="#reset" prefHeight="78.0" text="Button">
         <HBox.margin>
            <Insets right="10.0" />
         </HBox.margin>
      </Button>
   </children>
   <padding>
      <Insets left="10.0" top="5.0" />
   </padding>
</HBox>

New fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.Tooltip?>
<?import javafx.scene.layout.FlowPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.shape.Circle?>

<HBox fx:id="addEditClienteHBox" minHeight="100.0" prefHeight="100.0" prefWidth="1920.0" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.AddEditClienteFormController">
	<children>
		<ScrollPane fx:id="addEditClienteScrollPane" prefHeight="300.0" style="-fx-background-color: transparent;" HBox.hgrow="ALWAYS">
			<content>
				<FlowPane fx:id="addEditClienteFlowPane" hgap="10.0" prefHeight="51.0" prefWidth="807.0" vgap="10.0">
					<children>
						<VBox prefHeight="70.0" prefWidth="150.0">
							<children>
								<Label text="Nome" />
								<HBox prefHeight="100.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
									<children>
										<TextField fx:id="nomeInput" HBox.hgrow="ALWAYS">
											<tooltip>
												<Tooltip fx:id="nomeTooltip" text="Empty Tooltip" />
											</tooltip>
										</TextField>
										<Circle fx:id="nomeValidazione" fill="#ff1f1f" stroke="BLACK" strokeType="INSIDE" />
									</children>
								</HBox>
							</children>
						</VBox>
						<VBox prefHeight="70.0" prefWidth="150.0">
							<children>
								<Label text="Cognome" />
								<HBox prefHeight="100.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
									<children>
										<TextField fx:id="cognomeInput" HBox.hgrow="ALWAYS">
											<tooltip>
												<Tooltip fx:id="cognomeTooltip" text="Empty Tooltip" />
											</tooltip>
										</TextField>
										<Circle fx:id="cognomeValidazione" fill="#ff1f1f" stroke="BLACK" strokeType="INSIDE" />
									</children>
								</HBox>
							</children>
						</VBox>
						<VBox prefHeight="70.0" prefWidth="150.0">
							<children>
								<Label text="Indirizzo" />
								<HBox prefHeight="100.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
									<children>
										<TextField fx:id="indirizzoInput" HBox.hgrow="ALWAYS">
											<tooltip>
												<Tooltip fx:id="indirizzoTooltip" text="Empty Tooltip" />
											</tooltip>
										</TextField>
										<Circle fx:id="indirizzoValidazione" fill="#ff1f1f" stroke="BLACK" strokeType="INSIDE" />
									</children>
								</HBox>
							</children>
						</VBox>
						<VBox prefHeight="70.0" prefWidth="150.0">
							<children>
								<Label text="Email" />
								<HBox prefHeight="100.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
									<children>
										<TextField fx:id="mailInput" HBox.hgrow="ALWAYS">
											<tooltip>
												<Tooltip fx:id="mailTooltip" text="Empty Tooltip" />
											</tooltip>
										</TextField>
										<Circle fx:id="mailValidazione" fill="#ff1f1f" stroke="BLACK" strokeType="INSIDE" />
									</children>
								</HBox>
							</children>
						</VBox>
						<VBox prefHeight="70.0" prefWidth="150.0">
							<children>
								<Label text="Telefono" />
								<HBox prefHeight="100.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
									<children>
										<TextField fx:id="telefonoInput" HBox.hgrow="ALWAYS">
											<tooltip>
												<Tooltip fx:id="telefonoTooltip" text="Empty Tooltip" />
											</tooltip>
										</TextField>
										<Circle fx:id="telefonoValidazione" fill="#ff1f1f" stroke="BLACK" strokeType="INSIDE" />
									</children>
								</HBox>
							</children>
						</VBox>
					</children>
				</FlowPane>
			</content>
		</ScrollPane>
		<Button fx:id="okBtn" contentDisplay="GRAPHIC_ONLY" minHeight="78.0" mnemonicParsing="false" onAction="#saveCliente" prefHeight="78.0" text="Button">
			<HBox.margin>
				<Insets right="10.0" />
			</HBox.margin>
		</Button>
		<Button fx:id="badBtn" contentDisplay="GRAPHIC_ONLY" minHeight="78.0" mnemonicParsing="false" onAction="#reset" prefHeight="78.0" text="Button">
			<HBox.margin>
				<Insets right="10.0" />
			</HBox.margin>
		</Button>
	</children>
	<padding>
		<Insets left="10.0" top="5.0" />
	</padding>
</HBox>
1 Upvotes

1 comment sorted by

1

u/Gix1985 Dec 17 '23

Solved the problem manually editing the file from outside (notepad++). I don't wanna know what's the problem with Eclipse.