我正在研究两个FXML文件,即主框架和一个表格。单击“日志”按钮时,必须将FXML文件中定义的表添加到框架中的锚定窗格中。但问题是来自数据库的数据没有显示在表格的单元格中。它只显示一张空白表。我尝试直接从另一个阶段加载表(无需单击按钮),数据显示,但为什么当我从控制器调用数据时数据不显示?我认为我创建的从数据库加载数据的方法不是将数据添加到表的正确实例,但我不知道如何解决它。我已经在这个工作了两天,似乎无法在网上找到一个好的答案。感谢任何可以提供帮助的人。
这是我的Main代码。
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application {
@Override
public void start(Stage primaryStage)
{
try
{
FXMLLoader loader = new FXMLLoader(getClass().getResource("/application/AdminLogin.fxml"));
Parent root = (Parent) loader.load();
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
控制器
package application;
import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
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.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.stage.Stage;
import javafx.util.Callback;
public class Controller implements Initializable
{
Stage primaryStage;// STAGE FOR ADMIN HOME
Stage stage;//STAGE FOR LOGIN
//VARIABLES FOR LOGIN STAGE
@FXML TextField usernameTF;
@FXML PasswordField passwordTF;
@FXML Hyperlink forgotPW;
@FXML Button loginB;
@FXML Label superLabel;
@SuppressWarnings("rawtypes")
@FXML private TableView table;
@FXML private AnchorPane innerAP;
@FXML private Button populateB;
private MySQLConnection adminCon = new MySQLConnection();
private PasswordEncryptionAlgorithm algo;
private LogWriter logWriter = new LogWriter();
private String rawPassword;
private String password;
private String username;
//DB ATTRIBUTES
private String host;
private String dbUsername;
private String dbPassword;
public Controller()
{
//GET MYSQL CONNECTION SETTINGS FROM H2 LOCAL DB
H2Connection h2Connection = new H2Connection();
h2Connection.loadDriver();
h2Connection.connectToDB();
h2Connection.doSomething("select *from connectionSettings");
h2Connection.query();
try {
while(h2Connection.getRS().next())
{
host = h2Connection.getRS().getString("ip");
dbUsername = h2Connection.getRS().getString("username");
dbPassword = h2Connection.getRS().getString("password");
}
}
catch (SQLException e)
{
e.printStackTrace();
}
h2Connection.closeConnection();
}
public void login(ActionEvent e)
{
username = usernameTF.getText();
rawPassword = passwordTF.getText();
algo = new PasswordEncryptionAlgorithm(rawPassword);
algo.breakString(algo.split(algo.firstPass(rawPassword)));
adminCon.loadDriver();
adminCon.connectToDB(host, "admindb", dbUsername, dbPassword);
adminCon.doSomething("select *from admin where adminID = " + "'" + username + "'");
adminCon.query();
try
{
if(adminCon.getRS().next())
{
password = adminCon.getRS().getString("password");
}
else
{
JOptionPane.showMessageDialog(null, "Incorrect username or password");
}
}
catch ( SQLException e1)
{
e1.printStackTrace();
}
if(password.equals(algo.getFinalPass()))
{
try
{
//CLOSE LOGIN WINDOW
stage = (Stage) loginB.getScene().getWindow();
stage.close();
//LOAD ADMIN HOME WINDOW
primaryStage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("/application/AdminStage.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
catch(Exception f)
{
f.printStackTrace();
}
adminCon.closeConnection();
logWriter.writeLog(username, "LOGIN", host, dbUsername, dbPassword);
}
else
{
JOptionPane.showMessageDialog(null, "Incorrect username or password");
}
}
@SuppressWarnings("rawtypes")
private ObservableList<ObservableList> data;
@SuppressWarnings({ "unchecked", "rawtypes" })
public void populateTable() throws SQLException, IOException
{
FXMLLoader loader = new FXMLLoader(getClass().getResource("/application/LogsTable.fxml"));
innerAP.getChildren().setAll((AnchorPane) loader.load());
MySQLConnection connection = new MySQLConnection();
connection.loadDriver();
data = FXCollections.observableArrayList();
connection.connectToDB("localhost", "logsdb", "root", "forever");
connection.doSomething("select *from logs");
connection.query();
for(int i = 0; i < connection.getRS().getMetaData().getColumnCount(); i++)
{
final int j = i;
TableColumn col = new TableColumn(connection.getRS().getMetaData().getColumnName(i + 1));
col.setCellValueFactory(new Callback <CellDataFeatures<ObservableList, String>, ObservableValue<String>>()
{
public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param)
{
return new SimpleStringProperty(param.getValue().get(j).toString());
}
});
table.getColumns().addAll(col);
}
while(connection.getRS().next())
{
ObservableList<String> row = FXCollections.observableArrayList();
for(int i = 1; i<= connection.getRS().getMetaData().getColumnCount(); i++)
{
row.add(connection.getRS().getString(i));
}
data.add(row);
}
table.setItems(data);
}
public void buttonClick(ActionEvent e) throws SQLException, IOException
{
populateTable();
}
public void initialize(URL arg0, ResourceBundle arg1)
{
}
}
AdminStage.fxml(主框架)
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane fx:id="mainBG" prefHeight="650.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
<children>
<Button layoutX="14.0" layoutY="142.0" mnemonicParsing="false" prefHeight="74.0" prefWidth="120.0" text="Announcements" />
<AnchorPane fx:id="innerAP" layoutX="151.0" layoutY="143.0" prefHeight="496.0" prefWidth="835.0" />
<Button layoutX="14.0" layoutY="226.0" mnemonicParsing="false" onAction="#buttonClick" prefHeight="74.0" prefWidth="120.0" text="Logs" />
<Button layoutX="14.0" layoutY="310.0" mnemonicParsing="false" prefHeight="74.0" prefWidth="120.0" text="Employees" />
<Button layoutX="14.0" layoutY="394.0" mnemonicParsing="false" prefHeight="74.0" prefWidth="120.0" text="Students" />
<Button layoutX="14.0" layoutY="478.0" mnemonicParsing="false" prefHeight="74.0" prefWidth="120.0" text="Backup" />
<Button layoutX="14.0" layoutY="562.0" mnemonicParsing="false" prefHeight="74.0" prefWidth="120.0" text="Logout" />
</children>
</AnchorPane>
LogsTable.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
<children>
<TableView fx:id="table" layoutX="119.0" layoutY="91.0" prefHeight="491.0" prefWidth="367.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>