JavaFx Image Click event

2

I have an implementation being developed in JavaFx only I came across some situation that seem to be simple, when designating an Event the image on your OnMouseClicked from my FXML I get the following error:

java.lang.IllegalArgumentException: argument type mismatch

When doing a test I changed the Image by Button and applied to OnMouseClicked I also got the same return, but on top of what I did I changed the method call to OnAction on the button and then yes I got the Event execution. p>

To apply this situation should you make a different method call?

As ImageView does not have the OnAction option, how can I apply the Event to it?

Here is the code for my method below:

@FXML
public void GravarTransportadora(ActionEvent event){
    char fisjurpessoa = 'J';
    SimpleDateFormat formatador = new SimpleDateFormat("dd/MM/yyyy");
    try {
        // CNPJ e IE insert
        pf.inserir(txRazaoSocial.getText(),
                Long.parseLong(txCnpj.getText()),
                Long.parseLong(txIe.getText()),
                (new java.sql.Date(((java.util.Date) formatador.parse(txDataInauguracao.getText())).getTime())),
                fisjurpessoa,
                txNomeFantasia.getText(),
                txSite.getText());
    } catch (Exception e) {
    }
}
    
asked by anonymous 17.10.2014 / 12:15

1 answer

3

OnMouseClicked is not very recommended because it is not generic (since there is also the onTouched etc). But I would also use the same in this situation.

The best way to create a "clickable image" in JavaFX is to change the graphic of a button and use it as the image button.

Below is a complete example (and a minimum of how to achieve such an effect)

Principal.java

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class Principal extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Pane p = new Pane();
        Parent root = FXMLLoader.load(getClass().getResource("documento.fxml"));
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}

MyController.java

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

public class MeuController implements Initializable {

@FXML private Button btn;
@FXML private Label label;
int numClicks = 0;
private final Image img = new Image(
        "http://icons.iconarchive.com/icons/sicons/basic-round-social/256/ember-js-icon.png");

@Override
public void initialize(URL location, ResourceBundle resources) {
    ImageView imgview = new ImageView(img);
    btn.setGraphic(imgview);
}

@FXML
private void gravarTransportadora() {
    //Aqui executa o método que for, chamo outro método por frescura.
    alteraLabel();
}

private void alteraLabel() {
    label.setVisible(true);
    label.setText("Ocorreram " + (++numClicks) + " cliques na Imagem");
}
}

document.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MeuController">
    <children>
        <Label fx:id="label" layoutX="123.0" layoutY="14.0" text="Ocorreram X cliques na Imagem" visible="false">
            <font>
                <Font size="25.0" />
            </font>
        </Label>
        <Button fx:id="btn" layoutX="192.0" layoutY="121.0" mnemonicParsing="false" onAction="#gravarTransportadora">
            <graphic>
                <ImageView fitHeight="150.0" fitWidth="200.0" layoutX="107.0" layoutY="74.0" pickOnBounds="true" preserveRatio="true" />
            </graphic>
        </Button>
    </children>
</Pane>
    
17.10.2014 / 19:59