Insertion dynamique d'un bouton dans chaque ligne d un TableView avec pattern MVC
Bonjour,
J ai réalisé il y a quelques mois un petit programme qui permettait d'ajouter lors d une création de ligne d un tableView un bouton pour supprimer éventuellement la ligne et ça marchait plutôt pas mal mais j'ai été obligé de refaire ce programme avec une architecture MVC car par ailleurs il y avait des Bugs insolubles :calim2: . Malheureusement je n'arrive pas à intégrer mon bouton dynamique suppression dans la nouvelle monture . Pouvez vous m'aider ?
Voici un extrait de mon ancien programme avec la gestion et la création de mon bouton supprimer
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
TableView table = new TableView();
table.setEditable(true);
final TableColumn<Coordonnees, Boolean> visibleColumn = new TableColumn<>("A supprimer");
visibleColumn.setMinWidth(50/2);
visibleColumn.setCellValueFactory(new PropertyValueFactory<>("visible"));
final TableColumn<Coordonnees, Integer> OrdreCol = new TableColumn<>("Ordre");
OrdreCol.setMinWidth(50/2);
OrdreCol.setCellValueFactory( new PropertyValueFactory<>("Ordre"));
final TableColumn<Coordonnees, Double> LongeCol = new TableColumn<>("Longitude");
LongeCol.setMinWidth(50);
LongeCol.setCellValueFactory( new PropertyValueFactory<>("Longitude"));
final TableColumn<Coordonnees, Double> LatCol = new TableColumn<>("Latitude");
LatCol.setMinWidth(50);
LatCol.setCellValueFactory(new PropertyValueFactory<>("Latitude"));
table.getColumns().setAll(visibleColumn, OrdreCol, LongeCol, LatCol);
//////Gestion affichage bouton suppression Boolean sert � l'espace Bouton
visibleColumn.setCellFactory(
new Callback<TableColumn<Coordonnees, Boolean>, TableCell<Coordonnees, Boolean>>() {
@Override
public TableCell<Coordonnees, Boolean> call(TableColumn<Coordonnees, Boolean> p) {
return new ButtonCell();
} });
StackPane root = new StackPane();
root.getChildren().add(table);
primaryStage.setScene(new Scene(root, 200, 250));
primaryStage.show();
}
// Classe coordonees pour pouvoir afficher les coordonnees dans le tableau TableView
public static class Coordonnees {
static int ordrebridge=-1;
private final SimpleStringProperty longitude;
private final SimpleStringProperty latitude;
private final SimpleIntegerProperty ordre;
private Coordonnees (String ordre , String lati , String longi){
ordrebridge++;
this.ordre=new SimpleIntegerProperty(ordrebridge);
this.longitude = new SimpleStringProperty(longi);
this.latitude = new SimpleStringProperty(lati);
}
public String getLongitude() {
return longitude.get();
}
public void setLongitude(String longi) {
longitude.set(longi);
}
public String getLatitude() {
return latitude.get();
}
public void setLatitude(String lati) {
latitude.set(lati);
}
public int getOrdre() {
return ordre.get();
}
public void setOrdre(int position) {
ordre.set(position);
}
}
//Classe permettant d ajouter un bouton suppression a chaque ligne du tableau avec gestion de chaque bouton Suppression
public class ButtonCell extends TableCell<Coordonnees, Boolean> {
final Button cellButton = new Button("Suppression");
int monmarker=0;
ButtonCell() {
cellButton.setMinWidth((50/2)-4);
cellButton.getStyleClass().add("my-btn1");
cellButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
// do something when button clicked
Coordonnees coordoasup = (Coordonnees) ButtonCell.this.getTableView().getItems().get(ButtonCell.this.getIndex());
// obtenir le numero du bouton à detruire
/*
data.remove(coordoasup);
monmarker = coordoasup.getOrdre()-1;
mespoints.remove(coordoasup);
webview.getEngine().executeScript("clearMarkers("+ monmarker +");");
*/
}
});
}
//Display button if the row is not empty
@Override
protected void updateItem(Boolean t, boolean empty) {
super.updateItem(t, empty);
if (!empty) {
setGraphic(cellButton);
}
}
} |
et un essai de MVC avec un simple TableView mais là j ai quelques problèmes pour mettre un bouton dynamique à chaque ligne créer
Ici mon contrôleur:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
public class SampleController {
String [] latitrajet ;
String [] longitrajet;
String Envoijavascript;
boolean tableviewc,tableviewc1 ;
ObservableList<Coordonnees> coordonnees = FXCollections.observableArrayList();
static int compteur = 0;
int monmarker=0;
@FXML
private URL location;
@FXML
private ResourceBundle resources;
@FXML
private Button B1,B2,B3,B4,B5;
@FXML
public WebView webview;
@FXML
private Button menu;
@FXML
private Button enregistrement;
@FXML
private TableView<Coordonnees> tableview1 ;
@FXML
private TableColumn<Coordonnees,String> pointnumero;
@FXML TableColumn<Coordonnees,String> longitude1;
@FXML
private TableColumn<Coordonnees,String> latitude1;
@FXML
private TableColumn<Coordonnees,Button> delete;
//private Main main;
public SampleController() {
}
@FXML
private void initialize() throws SocketException
{
// initialisation des colonnes de tableview1
pointnumero.setCellValueFactory(cellData -> cellData.getValue().OrdreProperty());
latitude1.setCellValueFactory(cellData -> cellData.getValue().LatitudeProperty());
longitude1.setCellValueFactory(cellData -> cellData.getValue().LongitudeProperty());
// delete.setCellValueFactory(new PropertyValueFactory<>("true"));
tableview1.setVisible(true);
webview.getEngine().getLoadWorker().stateProperty().addListener((ObservableValue<? extends Worker.State> observableValue, Worker.State oldValue, Worker.State newValue) -> {
final boolean disabled = newValue != Worker.State.SUCCEEDED;
});
} |
et là , ma partie FXML :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.web.WebView?>
<BorderPane xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SampleController">
<top>
<AnchorPane prefHeight="800.0" prefWidth="600.0" BorderPane.alignment="CENTER">
<children>
<WebView fx:id="webview" layoutX="127.0" layoutY="20.0" prefHeight="580.0" prefWidth="604.0" />
<TableView fx:id="tableview1" layoutX="93.0" layoutY="226.0" prefHeight="200.0" prefWidth="604.0">
<columns>
<TableColumn fx:id="pointnumero" prefWidth="151.0" text="numero">
</TableColumn>
<TableColumn fx:id="longitude1" prefWidth="151.0" text="Longitude" />
<TableColumn fx:id="latitude1" prefWidth="151.0" text="Latitude" />
<TableColumn fx:id="delete" prefWidth="151.0" text="Supprimer">
<graphic>
<Button mnemonicParsing="false" prefHeight="25.0" prefWidth="135.0" text="Supprimer" />
</graphic></TableColumn>
</columns>
</TableView>
</children>
</AnchorPane>
</top>
</BorderPane> |
En attente de vous lire , et de mettre en pratique vos conseils.