Bonjour, j'essaye de redimensionner un Bouton et une Combobox automatiquement avec le layout GridPane en fonction du nombre de colonne que je lui attribue, en gros j'aimerais qu'ils aient le même comportement qu'un TextField quand je redimensionne ma fenêtre, est-ce possible ?
Exemple d'un programme simple :
Classe AddCards
package vue;
import javafx.collections.FXCollections;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
public class AddCards extends GridPane {
private Label lblAuthor;
private ComboBox cbTheme;
private TextField txtAuthor;
private Button btnErase;
public AddCards() {
this.setPadding(new Insets(10));
// this.setGridLinesVisible(true); // Rend visible ligne de séparation
this.setVgap(10);
this.setHgap(10);
int nbCol = 20;
for (int i = 0; i < nbCol; i++) {
ColumnConstraints colConstr = new ColumnConstraints();
colConstr.setPercentWidth(100. / nbCol);
this.getColumnConstraints().add(colConstr);
}
// Line 1
this.add(getCbTheme(), 2, 0, 5, 1); // elt, columnIndex, rowIndex, colspan, rowspan
cbTheme.setItems(FXCollections.observableArrayList("IMPROBABLE", "PLEASURE", "INFORMATICS", "SCHOOL"));
cbTheme.getSelectionModel().selectFirst();
this.add(getLblAuthor(), 7, 0, 3, 1);
this.add(getTxtAuthor(), 9, 0, 11, 1);
// Line 2
this.add(getBtnErase(), 0, 1, 2, 1);
}
public Label getLblAuthor() {
if (lblAuthor == null) {
lblAuthor = new Label("Author : ");
}
return lblAuthor;
}
public ComboBox getCbTheme() {
if (cbTheme == null) {
cbTheme = new ComboBox();
}
return cbTheme;
}
public TextField getTxtAuthor() {
if (txtAuthor == null) {
txtAuthor = new TextField();
}
return txtAuthor;
}
public Button getBtnErase() {
if (btnErase == null) {
btnErase = new Button("Erase");
}
return btnErase;
}
}
Classe Main
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import vue.AddCards;
import javafx.scene.Scene;
import javafx.scene.image.Image;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
AddCards root = new AddCards();
Scene scene = new Scene(root,600,315);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}