各種コントローラクラスの作成
● FXMLファイルからイベントハンドルを受取り、メインクラスと結びつけます。
RootController.java
package sample.controller;
import javafx.event.ActionEvent;
import javafx.scene.control.Alert;
public class RootController {
public void siteExit(ActionEvent actionEvent) {
System.exit(0);
}
public void siteAbout(ActionEvent actionEvent) {
Alert alert = new Alert (Alert.AlertType.INFORMATION);
alert.setTitle("JavaFXSampleFxmlApp");
alert.setHeaderText("FXMLの切り換えを自由に操作");
alert.setContentText("データベース操作を目的に応じて画面に表示し、データの処理を視覚的に容易にした。");
alert.show();
}
public void siteVersion(ActionEvent actionEvent) {
Alert alert = new Alert (Alert.AlertType.INFORMATION);
alert.setTitle("バージョン情報");
alert.setHeaderText("JavaFXSampleFxmlApp 1.0");
alert.setContentText("");
alert.show();
}
}
package sample.controller;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import sample.view.Main;
// call Mainクラス
public class IndexController {
@FXML
public void toCreate(ActionEvent actionEvent) {
Main.getInstance().create();
}
@FXML
public void toSearch(ActionEvent actionEvent) {
Main.getInstance().search();
}
@FXML
public void selectAllView(ActionEvent actionEvent) {
Main.getInstance().selectAllView();
}
}
package sample.controller;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import sample.view.Main;
import sample.DBJ.SampleDBJ;
public class CreateController {
@FXML
private TextField nameCreate;
@FXML
private TextField addressCreate;
@FXML
private TextArea memoCreate;
@FXML
private Label createSuccsess;
public void linkSave(ActionEvent actionEvent) {
String insName = nameCreate.getText();
String insAddress = addressCreate.getText();
String insMemo = memoCreate.getText();
if (insName.isEmpty()) {
createSuccsess.setText("Name is required input!.");
} else {
try {
SampleDBJ.insertData(insName,insAddress,insMemo);
createSuccsess.setText(null);
createSuccsess.setText("Success.");
} catch (SQLException | ClassNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
// Show Alldata
public void linkShowAll(ActionEvent actionEvent) {
Main.getInstance().selectAllView(); // call Mainクラス
}
// Index
public void linkIndex(ActionEvent actionEvent) {
Main.getInstance().indexView(); // call Mainクラス
}
}
package sample.controller;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.util.Callback;
import sample.view.Main;
import sample.Model.SampleModel;
import sample.DBJ.SampleDBJ;
public class SearchController {
@FXML
private TableColumn<SampleModel, Integer> idCol;
@FXML
private TableColumn<SampleModel, String> nameCol;
@FXML
private TableColumn<SampleModel, String> addressCol;
@FXML
private TableColumn<SampleModel, String> memoCol;
@FXML
private TableColumn<SampleModel, String> created_atCol;
@FXML
private TableColumn<SampleModel, String> updated_atCol;
@FXML
private TextField searchName;
@FXML
private Label searchError;
@FXML
private TableView<SampleModel> table;
private ObservableList<SampleModel> data;
// 初期化
public void initialize() {
/**
* カラム
*/
idCol.setCellValueFactory(cellData -> cellData.getValue().IdProperty().asObject());
nameCol.setCellValueFactory(cellData -> cellData.getValue().NameProperty());
addressCol.setCellValueFactory(cellData -> cellData.getValue().AddressProperty());
memoCol.setCellValueFactory(cellData -> cellData.getValue().MemoProperty());
created_atCol.setCellValueFactory(cellData -> cellData.getValue().Created_atProperty());
updated_atCol.setCellValueFactory(cellData -> cellData.getValue().Updated_atProperty());
/**
*カラム追加-Hyperlink
*/
editLink();
delLink();
}
// nameSearchAction
public void nameSearchAction(ActionEvent actionEvent) throws SQLException {
String name = searchName.getText();
if(name.isEmpty()) {
searchError.setText("Please input !.");
table.setVisible(false);
} else {
try {
searchError.setText(null);
data = SampleDBJ.searchName(name); // SampleDBJクラス
table.setItems(data);
table.setVisible(true);
} catch (ClassNotFoundException ex) {
System.out.println("data not!.");
}
}
}
// Select AllView
public void toSelectAllView(ActionEvent actionEvent) {
Main.getInstance().selectAllView();
}
// Index
public void linkIndex(ActionEvent actionEvent) {
Main.getInstance().indexView();
}
// Edit Link
private void editLink() {
TableColumn<SampleModel, Void> edit = new TableColumn("Edit");
edit.getStyleClass().add("table-Col-Edit");
Callback<TableColumn<SampleModel, Void>, TableCell<SampleModel, Void>> cellFactory;
cellFactory = (final TableColumn<SampleModel, Void> param) -> {
final TableCell<SampleModel, Void> editCell;
editCell = new TableCell<SampleModel, Void>() {
private final Hyperlink editLink = new Hyperlink("edit");
{ editLink.getStyleClass().add("editlink");
editLink.setOnAction((ActionEvent actionEvent) -> {
// TableViewインスタンスから指定されたインデックスのアイテムの値を取得
SampleModel editItems = getTableView().getItems().get(getIndex());
{ Main.getInstance().edit(editItems); } // call Mainクラス
});
}
@Override
public void updateItem(Void item, boolean empty) {
super.updateItem(item, empty);
if (!empty) {
setGraphic(editLink);
} else {
setGraphic(null);
}
}
};
return editCell;
};
edit.setCellFactory(cellFactory);
table.getColumns().add(edit);
}
// Delete Link
private void delLink() {
TableColumn<SampleModel, Void> del = new TableColumn("Del");
del.getStyleClass().add("table-Col-Del");
Callback<TableColumn<SampleModel, Void>, TableCell<SampleModel, Void>> cellFactory;
cellFactory = (final TableColumn<SampleModel, Void> param) -> {
final TableCell<SampleModel, Void> delCell;
delCell = new TableCell<SampleModel, Void>() {
private final Hyperlink delLink = new Hyperlink("del");
{ delLink.getStyleClass().add("dellink");
delLink.setOnAction((ActionEvent actionEvent)-> {
SampleModel delItems = getTableView().getItems().get(getIndex());
int id = delItems.getId(); // getId() - by SampleModelクラス
try {
boolean delJudg = SampleDBJ.dataDelete (id); // by SampleDBJクラス
if (delJudg) {
System.out.println("Delete Success.");
} else {
System.out.println("Delete Error!.");
}
} catch (SQLException | ClassNotFoundException ex) {
Logger.getLogger(SelectAllController.class.getName()).log(Level.SEVERE, null, ex);
}
});
};
@Override
public void updateItem(Void item, boolean empty) {
super.updateItem(item, empty);
if (!empty) {
setGraphic(delLink);
} else {
setGraphic(null);
}
}
};
return delCell;
};
del.setCellFactory(cellFactory);
table.getColumns().add(del);
}
}
package sample.controller;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Pagination;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.AnchorPane;
import javafx.util.Callback;
import sample.view.Main;
import sample.Model.SampleModel;
import sample.DBJ.SampleDBJ;
public class SelectAllController {
@FXML
private TableColumn<SampleModel, Integer> idCol;
@FXML
private TableColumn<SampleModel, String> nameCol;
@FXML
private TableColumn<SampleModel, String> addressCol;
@FXML
private TableColumn<SampleModel, String> memoCol;
@FXML
private TableColumn<SampleModel, String> created_atCol;
@FXML
private TableColumn<SampleModel, String> updated_atCol;
@FXML
private AnchorPane anchor;
@FXML
private Pagination pager;
@FXML
private TableView<SampleModel> table;
private ObservableList<SampleModel> data;
private static final int ROWS_PER_PAGE = 5; // レコード数/1ページ当たり
// ページ区切りコントロール作成とページ・ファクトリの実装
public AnchorPane dataViewPage() {
pager = new Pagination( setPages(), 0 );
pager.setPageFactory((Integer pageIndex) -> createPage(pageIndex));
anchor.getChildren().add(pager);
AnchorPane.setTopAnchor(pager, 170.0);
AnchorPane.setRightAnchor(pager, 20.0);
AnchorPane.setBottomAnchor(pager, 160.0);
AnchorPane.setLeftAnchor(pager, 20.0);
return anchor;
}
// ページ数
public int setPages() {
int pages = 1;
if (data.size() % ROWS_PER_PAGE == 0) {
pages = data.size() / ROWS_PER_PAGE;
} else if (data.size() > ROWS_PER_PAGE) {
pages = data.size() / ROWS_PER_PAGE + 1;
}
return pages;
}
// オブジェクトtableにデータ・モデルとページを関連付け
private TableView<SampleModel> createPage(Integer pageIndex) {
int fromIndex;
fromIndex = 0;
int toIndex;
toIndex = 0;
fromIndex = pageIndex * ROWS_PER_PAGE;
toIndex = Math.min(fromIndex + ROWS_PER_PAGE, data.size());
// ListオブジェクトのtableにsetItemsでデータをセット
table.setItems(FXCollections.observableArrayList(data.subList(fromIndex, toIndex)));
// セル高調整
table.setFixedCellSize(34);
return table;
}
// 初期化
public void initialize() {
/**
* カラム
*/
idCol.setCellValueFactory(cellData -> cellData.getValue().IdProperty().asObject());
nameCol.setCellValueFactory(cellData -> cellData.getValue().NameProperty());
addressCol.setCellValueFactory(cellData -> cellData.getValue().AddressProperty());
memoCol.setCellValueFactory(cellData -> cellData.getValue().MemoProperty());
created_atCol.setCellValueFactory(cellData -> cellData.getValue().Created_atProperty());
updated_atCol.setCellValueFactory(cellData -> cellData.getValue().Updated_atProperty());
/**
* カラム追加 - Hyperlink
*/
seeLink();
editLink();
delLink();
/**
* 起動時全データ表示
*/
allDataView();
}
// Create
public void linkCreate(ActionEvent actionEvent) {
Main.getInstance().create();
}
// Search
public void linkSearch(ActionEvent actionEvent) {
Main.getInstance().search();
}
// Index
public void linkIndex(ActionEvent actionEvent) {
Main.getInstance().indexView();
}
// call DB
public void allDataView() {
try {
data = SampleDBJ.searchAllData();
getAllData(data);
} catch (SQLException | ClassNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
// after delete
public void searchAllReview() {
pager.getStyleClass().add("deletepagination"); // css-Initialize Pagination
try {
data = SampleDBJ.searchAllData();
getAllData(data);
} catch (SQLException | ClassNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
// call AllData
private void getAllData(ObservableList<SampleModel> data) {
// DBデータ有無の判断
if (data.isEmpty()) {
System.out.println("data not!.");
} else {
dataViewPage();
}
}
// See Link
private void seeLink() {
TableColumn<SampleModel, Void> see = new TableColumn("See");
see.getStyleClass().add("table-Col-See");
Callback<TableColumn<SampleModel, Void>, TableCell<SampleModel, Void>> cellFactory;
cellFactory = (final TableColumn<SampleModel, Void> param) -> {
final TableCell<SampleModel, Void> editCell;
editCell = new TableCell<SampleModel, Void>() {
private final Hyperlink seeLink = new Hyperlink("see");
{ seeLink.getStyleClass().add("seelink");
seeLink.setOnAction((ActionEvent actionEvent) -> {
SampleModel seeItems = getTableView().getItems().get(getIndex());
{ Main.getInstance().see(seeItems); } // call Mainクラス
});
}
@Override
public void updateItem(Void item, boolean empty) {
super.updateItem(item, empty);
if (!empty) {
setGraphic(seeLink);
} else {
setGraphic(null);
}
}
};
return editCell;
};
see.setCellFactory(cellFactory);
table.getColumns().add(see);
}
// Edit Link
private void editLink() {
TableColumn<SampleModel, Void> edit = new TableColumn("Edit");
edit.getStyleClass().add("table-Col-Edit");
Callback<TableColumn<SampleModel, Void>, TableCell<SampleModel, Void>> cellFactory;
cellFactory = (final TableColumn<SampleModel, Void> param) -> {
final TableCell<SampleModel, Void> editCell;
editCell = new TableCell<SampleModel, Void>() {
private final Hyperlink editLink = new Hyperlink("edit");
{ editLink.getStyleClass().add("editlink");
editLink.setOnAction((ActionEvent actionEvent) -> {
SampleModel editItems = getTableView().getItems().get(getIndex());
{ Main.getInstance().edit(editItems); } // call Mainクラス
});
}
@Override
public void updateItem(Void item, boolean empty) {
super.updateItem(item, empty);
if (!empty) {
setGraphic(editLink);
} else {
setGraphic(null);
}
}
};
return editCell;
};
edit.setCellFactory(cellFactory);
table.getColumns().add(edit);
}
// Delete Link
private void delLink() {
TableColumn<SampleModel, Void> del = new TableColumn("Del");
del.getStyleClass().add("table-Col-Del");
Callback<TableColumn<SampleModel, Void>, TableCell<SampleModel, Void>> cellFactory;
cellFactory = (final TableColumn<SampleModel, Void> param) -> {
final TableCell<SampleModel, Void> delCell;
delCell = new TableCell<SampleModel, Void>() {
private final Hyperlink delLink = new Hyperlink("del");
{ delLink.getStyleClass().add("dellink");
delLink.setOnAction((ActionEvent actionEvent)-> {
SampleModel delItems = getTableView().getItems().get(getIndex());
int id = delItems.getId(); // getId() - by SampleModelクラス
try {
boolean delJudg = SampleDBJ.dataDelete (id); // by SampleDBJクラス
if (delJudg) {
searchAllReview();
} else {
System.out.println("Delete Error!.");
}
} catch (SQLException | ClassNotFoundException ex) {
Logger.getLogger(SelectAllController.class.getName()).log(Level.SEVERE, null, ex);
}
});
};
@Override
public void updateItem(Void item, boolean empty) {
super.updateItem(item, empty);
if (!empty) {
setGraphic(delLink);
} else {
setGraphic(null);
}
}
};
return delCell;
};
del.setCellFactory(cellFactory);
table.getColumns().add(del);
}
}
package sample.controller;
import java.sql.SQLException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import sample.DBJ.SampleDBJ;
import sample.Model.SampleModel;
import sample.view.Main;
public class EditController {
@FXML
private Label succsess;
@FXML
private TextField idEdit;
@FXML
private TextField nameEdit;
@FXML
private TextField addressEdit;
@FXML
private TextArea memoEdit;
// Edit TextField
/**
* @param editItems
*/
public void editItem(SampleModel editItems) {
idEdit.setText(Integer.toString(editItems.getId()));
nameEdit.setText(editItems.getName());
addressEdit.setText(editItems.getAddress());
memoEdit.setText(editItems.getMemo());
}
// Edit Save
public void linkSave(ActionEvent actionEvent) throws SQLException, ClassNotFoundException {
String id_edit = idEdit.getText();
String address_edit = addressEdit.getText();
String memo_edit = memoEdit.getText();
boolean editJudg = SampleDBJ.dataUpdate(id_edit, address_edit, memo_edit);
if (editJudg) {
succsess.setText("Succsess.");
} else {
succsess.setText("Update Error!.");
}
}
// Show Alldata
public void linkShowAll(ActionEvent actionEvent) {
Main.getInstance().selectAllView();
}
// Index
public void linkIndex(ActionEvent actionEvent) {
Main.getInstance().indexView();
}
}
package sample.controller;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import sample.Model.SampleModel;
import sample.view.Main;
public class SeeController {
@FXML
private Label idlabel;
@FXML
private Label namelabel;
@FXML
private TextField addressTxtFld;
@FXML
private TextArea memoTxtArea;
// See Item
/**
* @param seeItems
*/
public void seeItem(SampleModel seeItems) {
idlabel.setText(Integer.toString(seeItems.getId()));
namelabel.setText(seeItems.getName());
addressTxtFld.setText(seeItems.getAddress());
memoTxtArea.setText(seeItems.getMemo());
}
// Show Alldata
public void linkShowAll(ActionEvent actionEvent) {
Main.getInstance().selectAllView();
}
// Index
public void linkIndex(ActionEvent actionEvent) {
Main.getInstance().indexView();
}
}
以上、Fxmlファイルの役割毎にControllerクラスを作成しました。
効率よく一つのControllerクラスにまとめればよいが、かえって複雑かつ膨大な処理を書くことになるかと思います。