-2

This is my fxml file and i made an eventlistener for my button with the id of "deletebutton":

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.VBox?>

<VBox minHeight="600.0" minWidth="800.0" spacing="20.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/20.0.1" fx:controller="com.example.carjava.HelloController">
    <padding>
        <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
    </padding>
   <children>
      <Button fx:id="deleteButton" mnemonicParsing="false" text="Törlés" />
      <TableView fx:id="carTable" prefHeight="500.0" prefWidth="200.0">
        <columns>
          <TableColumn fx:id="licensePlateCol" prefWidth="180.0" text="Rendszám" />
          <TableColumn fx:id="modelCol" minWidth="0.0" prefWidth="180.0" text="Model" />
            <TableColumn fx:id="brandCol" prefWidth="180.0" text="Márka" />
            <TableColumn fx:id="dailyCostCol" prefWidth="180.0" text="Napidíj (Ft)" />
        </columns>
      </TableView>
   </children>
</VBox>

I tried making an event listener but i was hoping for something simpler. And also is there a way of making a GUI faster and simpler?

Nikolai Shevchenko
  • 7,083
  • 8
  • 33
  • 42
  • 1
    *"And also is there a way of making a GUI faster and simpler?"* => Yes, but that is both a much too generic question to get an answer here and completely unrelated to your actual question title. – jewelsea May 30 '23 at 15:43
  • 2
    *"Is there a way to create an onclick event on my button?"* -> You can create a click event for a button by clicking on it, or by [firing](https://openjfx.io/javadoc/17/javafx.base/javafx/event/Event.html#fireEvent(javafx.event.EventTarget,javafx.event.Event)) a [`MOUSE_CLICKED`](https://openjfx.io/javadoc/17/javafx.graphics/javafx/scene/input/MouseEvent.html#MOUSE_CLICKED) event on the button. But I have a feel that isn't what you really want to know. – jewelsea May 30 '23 at 15:48

1 Answers1

1

You can use the onAction attribute of the button to bind the click to a method of your controller.

In your FXML :

<Button fx:id="deleteButton" mnemonicParsing="false" text="Törlés" onAction="#onDeleteButtonClicked" />

In your controller HelloController :

public void onDeleteButtonClicked(ActionEvent event) {
   ...
}

Regarding the question "is there a way of making a GUI faster and simpler?", you could use SceneBuilder.

Bastien Aracil
  • 1,531
  • 11
  • 6