Java 显示空表的TableView
我正在处理两个FXML文件,主框架和一个表。当我单击“Logs”按钮时,FXML文件中定义的表必须添加到框架中的锚链上。但问题是数据库中的数据没有显示在表的单元格中。它只是显示一个空白表。我尝试直接从另一个阶段加载表(无需单击按钮),数据会显示,但为什么从控制器调用时数据不会显示?我认为我创建的从数据库加载数据的方法没有将数据添加到表的正确实例中,但我不确定如何修复它。我已经研究这个问题两天了,似乎在网上找不到好的答案。谢谢所有能帮忙的人 这是我的Main代码Java 显示空表的TableView,java,javafx,Java,Javafx,我正在处理两个FXML文件,主框架和一个表。当我单击“Logs”按钮时,FXML文件中定义的表必须添加到框架中的锚链上。但问题是数据库中的数据没有显示在表的单元格中。它只是显示一个空白表。我尝试直接从另一个阶段加载表(无需单击按钮),数据会显示,但为什么从控制器调用时数据不会显示?我认为我创建的从数据库加载数据的方法没有将数据添加到表的正确实例中,但我不确定如何修复它。我已经研究这个问题两天了,似乎在网上找不到好的答案。谢谢所有能帮忙的人 这是我的Main代码 package applicati
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)
{
}
}
包应用;
导入java.io.IOException;
导入java.net.URL;
导入java.sql.SQLException;
导入java.util.ResourceBundle;
导入javax.swing.JOptionPane;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.beans.value.observeValue;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.event.ActionEvent;
导入javafx.fxml.fxml;
导入javafx.fxml.fxmloader;
导入javafx.fxml.Initializable;
导入javafx.scene.Parent;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.Hyperlink;
导入javafx.scene.control.Label;
导入javafx.scene.control.PasswordField;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableView;
导入javafx.scene.control.TextField;
导入javafx.scene.layout.ancorpane;
导入javafx.scene.control.TableColumn.CellDataFeatures;
导入javafx.stage.stage;
导入javafx.util.Callback;
公共类控制器实现可初始化
{
Stage primaryStage;//管理员主页的Stage
Stage;//用于登录的Stage
//登录阶段的变量
@FXML TextField usernameTF;
@FXML PasswordField passwordTF;
@FXML超链接放弃pw;
@FXML按钮登录;
@FXML标签超标签;
@抑制警告(“原始类型”)
@FXML私有表视图表;
@FXML-私家锚烷内肽;
@FXML私有按钮populateB;
私有MySQLConnection adminCon=新的MySQLConnection();
私有密码加密算法;
私有LogWriter LogWriter=新LogWriter();
私有字符串密码;
私有字符串密码;
私有字符串用户名;
//数据库属性
私有字符串主机;
私有字符串dbUsername;
私有字符串密码;
公共控制员()
{
//从H2本地数据库获取MYSQL连接设置
H2连接H2连接=新H2连接();
h2Connection.loadDriver();
h2Connection.connectToDB();
H2连接。doSomething(“从连接设置中选择*);
h2Connection.query();
试一试{
while(h2Connection.getRS().next())
{
host=h2Connection.getRS().getString(“ip”);
dbUsername=h2Connection.getRS().getString(“用户名”);
dbPassword=h2Connection.getRS().getString(“密码”);
}
}
捕获(SQLE异常)
{
e、 printStackTrace();
}
h2Connection.closeConnection();
}
公共无效登录(ActionEvent e)
{
username=usernameTF.getText();
rawPassword=passwordTF.getText();
algo=新密码加密算法(rawPassword);
algo.breakString(algo.split(algo.firstPass(rawPassword));
adminCon.loadDriver();
adminCon.connectToDB(主机,“admindb”、dbUsername、dbPassword);
adminCon.doSomething(“从admin中选择*,其中adminID=“+””“+用户名+””);
adminCon.query();
尝试
{
if(adminCon.getRS().next())
{
password=adminCon.getRS().getString(“密码”);
}
其他的
{
showMessageDialog(null,“不正确的用户名或密码”);
}
}
捕获(SQLException e1)
{
e1.printStackTrace();
}
if(password.equals(algo.getFinalPass()))
{
尝试
{
//关闭登录窗口
stage=(stage)loginB.getScene().getWindow();
stage.close();
//加载管理主窗口
primaryStage=新阶段();
父根=fxmloader.load(getClass().getResource(“/application/admintage.fxml”);
场景=新场景(根);
scene.getStylesheets().add(getClass().getResource(“application.css”).toExternalForm());
初级阶段。场景(场景);
primaryStage.show();
}
捕获(例外f)
{
f、 printStackTrace();
}
adminCon.closeConnection();
logWriter.writeLog(用户名,“登录”,主机,dbUsername,dbPassword);
}
其他的
{
showMessageDialog(null,“不正确的用户名或密码”);
}
}
@抑制警告(“原始类型”)
私有可观测列表数据;
@SuppressWarnings({“unchecked”,“rawtypes”})
public void populateTable()引发SQLException、IOException
{
FXMLLoader=newFXMLLoader(getClass().getResource(“/application/LogsTable.fxml”);
setAll((AnchorPane)loader.load());
MySQLConnection=newmysqlconnection();
connection.loadDriver();
data=FXCollections.observearraylist();
connectToDB(“localhost”、“logsdb”、“root”、“forever”);
连接。doSomething(“从日志中选择*);
connection.query();
对于(int i=0;i<?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>
<?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>