5
我想在JavaFX中獲取一個TableView來動態顯示內容。JavaFX FXML控制器ClassNotFoundException
當我運行我的程序我得到這個錯誤:
java.lang.ClassNotFoundException: UserInterfaceController
我的控制器命名爲「UserInterfaceController.java」,這是相同的封裝FXML下的文件,我已經在FXML作爲進口包好。爲什麼找不到控制器?
FXML文件:
<?import javafx.collections.*?>
<?import javafx.geometry.Insets?>
<?import java.lang.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.control.cell.*?>
<?import javafx.scene.layout.*?>
<?import d1example2.*?>
<GridPane alignment="center" hgap="10.0" vgap="10.0" fx:controller="UserInterfaceController"
xmlns:fx="http://javafx.com/fxml">
<TableView fx:id="tableView" GridPane.columnIndex="0" GridPane.rowIndex="1">
<columns>
</columns>
</TableView>
</GridPane>
控制器:
package d1example2;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
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.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Text;
import javafx.util.Callback;
public class UserInterfaceController implements Initializable {
private Label label;
@FXML
private AnchorPane MainPane;
@FXML
private TextField FirstField;
@FXML
private Text TimesText;
@FXML
private Text EqualSign;
@FXML
private Text EquationResult;
@FXML
private TableColumn<?, ?> HalfColumn;
@FXML
private TableColumn<?, ?> DoubleColumn;
@FXML
private Button SubmitButton;
@FXML private TableView tableView;
@FXML
public void initialize(URL url, ResourceBundle rb) {
List<String> columns = new ArrayList<String>();
columns.add("col1");
columns.add("col2");
TableColumn [] tableColumns = new TableColumn[columns.size()];
int columnIndex = 0;
for(int i=0 ; i<columns.size(); i++) {
final int j = i;
TableColumn col = new TableColumn(columns.get(i));
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());
}
});
tableView.getColumns().addAll(col);
}
ObservableList<String> row = FXCollections.observableArrayList();
ObservableList<String> row1 = FXCollections.observableArrayList();
row.addAll("d1");
row.addAll("d11");
row1.addAll("d2");
row1.addAll("d22");
tableView.getItems().add(row);
tableView.getItems().add(row1);
}
@FXML
private void handleButtonAction(MouseEvent event) {
}
}
主類:使用它從FXML控制器
package d1example2;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class D1Example2 extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("FXML TableView Example");
Pane myPane = (Pane)FXMLLoader.load(getClass().getResource("UserInterface.fxml"));
Scene myScene = new Scene(myPane);
primaryStage.setScene(myScene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}