package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("view/FXMLSample.fxml"));
Scene scene = new Scene(root, 640,480);
primaryStage.setScene(scene);
primaryStage.setTitle("User list");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
package sample.controller;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableView;
import sample.model.Person;
import sample.model.ConnectDB;
import javafx.scene.control.TableColumn;
import java.sql.*;
public class Controller implements Initializable {
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;
private ObservableList<Person> usersData = FXCollections.observableArrayList();
@FXML
private TableView<Person> tableUsers;
@FXML
private TableColumn<Person, Integer> idColumn;
@FXML
private TableColumn<Person, String> usernameColumn;
@FXML
private TableColumn<Person, String> firstnameColumn;
@FXML
private TableColumn<Person, String> lastnameColumn;
@FXML
private TableColumn<Person, String> emailColumn;
@FXML
private TableColumn<Person, String> genderColumn;
@FXML
private TableColumn<Person, String> descColumn;
@FXML
private TableColumn<Person, String> createdonColumn;
@FXML
private TableColumn<Person, String> editedonColumn;
@FXML
private TableColumn<Person, String> activeColumn;
@FXML
public void initialize(URL location, ResourceBundle resources) {
conn = ConnectDB.ConnectMariaDB();
initData();
tableUsers.setItems(usersData);
}
private void initData(ActionEvent event) throws SQLException {
String sql = "SELECT * FROM test";
rs = pst.executeQuery(sql);
if(rs.next()) {
int id = rs.getInt("id");
String username = rs.getString("username");
String firstname = rs.getString("firstname");
String lastname = rs.getString("lastname");
String email = rs.getString("email");
String gender = rs.getString("gender");
String description = rs.getString("description");
String createdon = rs.getString("createdon");
String editedon = rs.getString("editedon");
String active = rs.getString("active");
usersData.add(new Person(id, username, firstname, lastname, email, gender, description, createdon, editedon, active));
}
}
}
package sample.model;
import java.sql.*;
public class ConnectDB {
Connection conn = null;
public static Connection ConnectMariaDB() {
try {
Class.forName("org.mariadb.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mariadb://localhost/test_new","root","123456");
System.out.println("Connection success!");
return conn;
} catch(Exception e){
System.out.println(e);
return null;
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.controller.Controller">
<children>
<MenuBar VBox.vgrow="NEVER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="New" />
<MenuItem mnemonicParsing="false" text="Open…" />
<Menu mnemonicParsing="false" text="Open Recent" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Close" />
<MenuItem mnemonicParsing="false" text="Save" />
<MenuItem mnemonicParsing="false" text="Save As…" />
<MenuItem mnemonicParsing="false" text="Revert" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Preferences…" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Quit" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Undo" />
<MenuItem mnemonicParsing="false" text="Redo" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Cut" />
<MenuItem mnemonicParsing="false" text="Copy" />
<MenuItem mnemonicParsing="false" text="Paste" />
<MenuItem mnemonicParsing="false" text="Delete" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Select All" />
<MenuItem mnemonicParsing="false" text="Unselect All" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About MyHelloApp" />
</items>
</Menu>
</menus>
</MenuBar>
<AnchorPane id="AnchorPane" maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
<children>
<Label layoutX="20.0" layoutY="14.0" text="Table" />
<TableView fx:id="tableUsers" layoutX="20.0" layoutY="37.0" prefHeight="285.0" prefWidth="600.0">
<columns>
<TableColumn prefWidth="42.0" text="ID" fx:id="idColumn" />
<TableColumn prefWidth="67.0" text="Username" fx:id="usernameColumn" />
<TableColumn prefWidth="156.0" text="Fullname">
<columns>
<TableColumn prefWidth="75.0" text="First name" fx:id="firstnameColumn" />
<TableColumn prefWidth="75.0" text="Last name" fx:id="lastnameColumn" />
</columns>
</TableColumn>
<TableColumn prefWidth="75.0" text="E-mail" fx:id="emailColumn" />
<TableColumn prefWidth="69.0" text="Gender" fx:id="genderColumn" />
<TableColumn prefWidth="69.0" text="Description" fx:id="descColumn" />
<TableColumn prefWidth="63.0" text="Created" fx:id="createdonColumn" />
<TableColumn minWidth="0.0" prefWidth="57.0" text="Edited" fx:id="editedonColumn" />
<TableColumn minWidth="4.0" prefWidth="105.0" text="Active" fx:id="activeColumn" />
</columns>
</TableView>
<Button layoutX="23.0" layoutY="336.0" mnemonicParsing="false" text="Create" />
<Button layoutX="88.0" layoutY="336.0" mnemonicParsing="false" text="Update" />
<Button layoutX="154.0" layoutY="336.0" mnemonicParsing="false" text="Delete" />
<Button layoutX="219.0" layoutY="336.0" mnemonicParsing="false" text="Refresh" />
</children>
</AnchorPane>
</children>
</VBox>
Error:(64, 9) java: method initData in class sample.controller.Controller cannot be applied to given types;
required: javafx.event.ActionEvent
found: no arguments
reason: actual and formal argument lists differ in length
package sample.controller;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import sample.model.Person;
import sample.model.ConnectDB;
import javafx.scene.control.TableColumn;
import java.sql.*;
public class Controller implements Initializable {
Connection conn = null;
ResultSet rs = null;
Statement st = null;
@FXML
private URL location;
@FXML
private ResourceBundle resources;
@FXML
private TableView<Person> tableUsers;
@FXML
private TableColumn<Person, Integer> idColumn;
@FXML
private TableColumn<Person, String> usernameColumn;
@FXML
private TableColumn<Person, String> firstnameColumn;
@FXML
private TableColumn<Person, String> lastnameColumn;
@FXML
private TableColumn<Person, String> emailColumn;
@FXML
private TableColumn<Person, String> genderColumn;
@FXML
private TableColumn<Person, String> descColumn;
@FXML
private TableColumn<Person, String> createdonColumn;
@FXML
private TableColumn<Person, String> editedonColumn;
@FXML
private TableColumn<Person, String> activeColumn;
@FXML
private void tableUsersCreate(ActionEvent event) {
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Table refresh");
// alert.setHeaderText("Results:");
alert.setContentText("Refresh table to the database successfully!");
tableUsers.refresh();
alert.showAndWait();
}
@FXML
private void tableUsersUpdate(ActionEvent event) {
Alert alert = new Alert(AlertType.WARNING);
alert.setTitle("Table refresh");
// alert.setHeaderText("Results:");
alert.setContentText("Refresh table to the database successfully!");
tableUsers.refresh();
alert.showAndWait();
}
@FXML
private void tableUsersRefresh(ActionEvent event) {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Table refresh");
// alert.setHeaderText("Results:");
alert.setContentText("Refresh table to the database successfully!");
tableUsers.refresh();
alert.showAndWait();
}
@FXML
private void tableUsersDelete(ActionEvent event) {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Table refresh");
// alert.setHeaderText("Results:");
alert.setContentText("Refresh table to the database successfully!");
tableUsers.refresh();
alert.showAndWait();
}
@FXML
public void initialize(URL location, ResourceBundle resources) {
conn = ConnectDB.ConnectMariaDB();
final ObservableList data = FXCollections.observableArrayList();
String query = "SELECT * FROM test";
Statement st = null;
try {
st = conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
ResultSet rs = null;
try {
rs = st.executeQuery(query);
} catch (SQLException e) {
e.printStackTrace();
}
Person person;
try {
while (rs.next()) {
int id = rs.getInt("id");
String username = rs.getString("username");
String firstname = rs.getString("firstname");
String lastname = rs.getString("lastname");
String email = rs.getString("email");
String gender = rs.getString("gender");
String description = rs.getString("description");
String createdon = rs.getString("createdon");
String editedon = rs.getString("editedon");
boolean active = rs.getBoolean("active");
person = new Person(id,username,firstname,lastname,email,gender,description,createdon,editedon,active);
data.add(person);
}
st.close();
} catch(Exception e) {
System.out.println("There is an Exception.");
System.out.println(e.getMessage());
}
idColumn.setCellValueFactory(new PropertyValueFactory<>("id"));
usernameColumn.setCellValueFactory(new PropertyValueFactory<>("username"));
firstnameColumn.setCellValueFactory(new PropertyValueFactory<>("firstname"));
lastnameColumn.setCellValueFactory(new PropertyValueFactory<>("lastname"));
emailColumn.setCellValueFactory(new PropertyValueFactory<>("email"));
genderColumn.setCellValueFactory(new PropertyValueFactory<>("gender"));
descColumn.setCellValueFactory(new PropertyValueFactory<>("description"));
createdonColumn.setCellValueFactory(new PropertyValueFactory<>("createdon"));
editedonColumn.setCellValueFactory(new PropertyValueFactory<>("editedon"));
activeColumn.setCellValueFactory(new PropertyValueFactory<>("active"));
tableUsers.setItems(data);
}
}
public void initialize(URL location, ResourceBundle resources)
, в нем вы вызываете initData();
без входных аргументов,который у вас определен как private void initData(ActionEvent event) throws SQLException {
. Вы либо аргумент в вызове передайте, либо сам метод поправьте и уберите параметр ActionEvent event