Answer the question
In order to leave comments, you need to log in
JavaFX How to re-call initialize from another initialize?
MainController in which initialize is called just once.
public class MainController {
final Query PURGE = new Query();
@FXML
private Button idAdd;
@FXML
private Button idRemove;
@FXML
private ObservableList<ViewTable> usersData = FXCollections.observableArrayList();
@FXML
private TableView<ViewTable> idTable;
@FXML
private TableColumn<ViewTable, Integer> idNumColumn;
@FXML
private TableColumn<ViewTable, String> idNameValueColumn;
@FXML
private TableColumn<ViewTable, String> idCountColumn;
@FXML
public void initialize() throws Exception {
initData();
idNumColumn.setCellValueFactory(new PropertyValueFactory<ViewTable, Integer>("id"));
idNameValueColumn.setCellValueFactory(new PropertyValueFactory<ViewTable, String>("nameValue"));
idCountColumn.setCellValueFactory(new PropertyValueFactory<ViewTable, String>("count"));
idTable.setItems(usersData);
idAdd.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
try {
//генерируем и вызываем вторую форму, в которой соответственно вызывается уже свой initialize
UIGenerate uiGenerate = new UIGenerate("addForm", "AddFormStart");
Stage stage = uiGenerate.startUIGenerate();
stage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private void initData() {
try{
ResultSet resultSet = PURGE.getAll();
while (resultSet.next()) {
int id = resultSet.getInt("id");
String nameValue = resultSet.getString("namevalue");
String count = resultSet.getString("count");
usersData.add(new ViewTable(id, nameValue, count));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//дергаем последнею запись из базы
public void lastRecord() {
try{
ResultSet resultSet = PURGE.getLastRecord();
while (resultSet.next()) {
int id = resultSet.getInt("id");
String nameValue = resultSet.getString("namevalue");
String count = resultSet.getString("count");
usersData.add(new ViewTable(id, nameValue, count));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public class AddFormController extends MainController {
@FXML
private AnchorPane idAnchorPane;
@FXML
private TextField idNameValue;
@FXML
private TextField idCountValue;
@FXML
private Button idAddButton;
@FXML
private Button idCloseButton;
@FXML
private Label idMessage;
@FXML
public void initialize() throws Exception{
idCloseButton.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
try {
UIGenerate uiGenerate = new UIGenerate("addForm", "AddFormStart");
Stage stage = uiGenerate.startUIGenerate();
stage.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
idAddButton.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
String nameValue = idNameValue.getText().trim();
int countValue = Integer.parseInt(idCountValue.getText());
Query query = new Query();
try {
if (!nameValue.isEmpty()) {
query.queryAdd(nameValue, countValue);
UIGenerate uiGenerate = new UIGenerate("addForm", "AddFormStart");
Stage stage = uiGenerate.startUIGenerate();
stage.close();
} else {
idMessage.setText("Error");
idMessage.setVisible(true);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
Answer the question
In order to leave comments, you need to log in
Well, I did the interaction like this:
FXMLLoader something = new FXMLLoader(fxml);
root = something.load();
import javafx.fxml.Initializable;
public class MainController implements Initializable {
// ...
@FXML
public void initialize(URL location, ResourceBundle resources) {
// ...
}
// ...
}
public class AddFormController extends MainController {
// ...
@Override
public void initialize(URL location, ResourceBundle resources) {
super.initialize(location, resources);
// ...
}
// ...
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question