ciao!
ho un controller che potrebbe rappresentare o una lista di editori, o una lista di autori.
la struttura di autori / editori è la stessa; ad esempio:
codice:
public class Author {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Author)) {
return false;
}
Author a = (Author) obj;
return this.name.equals(a.toString());
}
@Override
public int hashCode() {
int hash = 3;
hash = 59 * hash + (this.name != null ? this.name.hashCode() : 0);
return hash;
}
}
questa la classe che rappresenta il controller:
codice:
public class ControllerAuthorEditor {
private Service service;
private ReadJson jsonRead;
private Stage stage;
@FXML
private BorderPane aePane;
@FXML
private ComboBox<Author> comboAE;
@FXML
public void initialize() {
jsonRead = new ReadJson();
service = new Service();
TypeToken<List<Author>> authorToken = new TypeToken<List<Author>>() {
};
try {
List<Author> listAuthors = jsonRead.readJson(new File(UrlAndPath.JSON_AUTORI), authorToken);
ObservableList<Author> authors = FXCollections.observableArrayList(listAuthors);
comboAE.setItems(authors);
comboAE.setCellFactory(new ComboListCell<Author>());
new KeyComboListener(comboAE);
} catch (IOException e) {
GenericDialog.showDialog(e.getMessage(), Alert.AlertType.ERROR);
}
}
private Stage getStage() {
stage = (Stage) aePane.getScene().getWindow();
return stage;
}
@FXML
private void escPressed(KeyEvent event) {
if (event.getCode() == KeyCode.ESCAPE) {
getStage();
stage.close();
}
}
}
e questo il layout (che devo ancora terminare):
codice:
<BorderPane fx:id="aePane" onKeyPressed="#escPressed" xmlns="http://javafx.com/javafx/8.0.121"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.mp.book.ControllerAuthorEditor">
<center>
<SplitPane fx:id="splitPane" dividerPositions="0.5" prefHeight="160.0" prefWidth="200.0"
BorderPane.alignment="CENTER">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<TextField/>
</children>
<children>
<ComboBox fx:id="comboAE"></ComboBox>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0"/>
</items>
</SplitPane>
</center>
</BorderPane>
vorrei generalizzare il controller, e fare in modo da riempire la ComboBox o con gli autori o con gli editori (le liste sono prese da due file json differenti).
mi dareste un input per iniziare???