Event click on Image Javafx

Asked

Viewed 2,234 times

2

I have an implementation being developed in Javafx only that I came across some situation that seem to be simple, when designating an Event the image in your Onmouseclicked from my FXML got the return of the following error:

java.lang.IllegalArgumentException: argument type mismatch

When making a test I changed the Image for the Button and applied in the Onmouseclicked also obtained the same return, only on top of what I did I changed the method call to the Onaction button and then yes I got the execution of the Event.

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

Since Imagemview does not have the Onaction option, how can I apply the Event to it?

Follow the code of 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) {
    }
}
  • You will have no more than a method with the same name, where each of them has a different argument list?

  • @Zuul in this class I am only this method set to make sure that it would not be something that was interfering.

  • This matter of defining a method for an Imageview I tried to do for navigation between screens but also unsuccessful.

1 answer

3


Onmouseclicked is not very recommended because it is not generic, (because there is also onTouched etc). But you could also use the same in this situation.

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

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

Main 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);
    }
}

Meucontroller.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");
}
}

fxml document

<?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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.