Hallo zusammen,
habe ein kleines Problem.
Ich erstelle mit einem "+"- Button beliebig viele Textfelder. Die Texte in den Textfeldern möchte ich mit einem "Erstellen"-Button in einer TableView anzeigen lassen.
Ich habe schon rumprobiert aber leider ist es mir nicht gelungen eine logische Lösung zu finden, da ich diesen Code benutzt habe um Textfelder zu erzeigen.
addTextFieldButton.setOnAction(e -> hbox2.getChildren().add(new TextField()));
also haben die einzelnen Textfelder natürlich auch keine spezielle variablen.
Unten seht ihr ein Bild wie die GUI momentan aussieht.
mit dem addButton möchte ich die einzelnen Texte in den Spalten angezeigt bekommen.
Mein Code:
Freue mich auf Vorschläge und Tipps.
Danke
habe ein kleines Problem.
Ich erstelle mit einem "+"- Button beliebig viele Textfelder. Die Texte in den Textfeldern möchte ich mit einem "Erstellen"-Button in einer TableView anzeigen lassen.
Ich habe schon rumprobiert aber leider ist es mir nicht gelungen eine logische Lösung zu finden, da ich diesen Code benutzt habe um Textfelder zu erzeigen.
addTextFieldButton.setOnAction(e -> hbox2.getChildren().add(new TextField()));
also haben die einzelnen Textfelder natürlich auch keine spezielle variablen.
Unten seht ihr ein Bild wie die GUI momentan aussieht.
mit dem addButton möchte ich die einzelnen Texte in den Spalten angezeigt bekommen.
Mein Code:
Code:
package Tableview;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class CreateScheme extends Application {
// Variablen
@Override
public void start(Stage secondStage) throws Exception {
// Scene / root
Pane pane = new Pane(); // Layout -> alle Elmente haben die Position 0/0
Scene scene = new Scene(pane, 800, 600);
// Variablen erstellen
VBox vbox = new VBox();
VBox vbox2 = new VBox();
HBox hbox = new HBox();
HBox hbox2 = new HBox();
HBox hbox3 = new HBox();
Label label = new Label("Bewertungsschema erstellen: ");
Button button = new Button("New");
TextField textField1 = new TextField();
CheckBox checkbox = new CheckBox();
Button addButton = new Button("Erstellen");
Button cancelButton = new Button("Abbrechen");
Button saveButton = new Button("Speichern");
Button addTextFieldButton = new Button("+");
TableView<Model> tableview = new TableView<Model>();
TableColumn<Model, String> table1 = new TableColumn<Model, String>("Table1");
TableColumn<Model, String> table2 = new TableColumn<Model, String>("Table2");
TableColumn<Model, String> table3 = new TableColumn<Model, String>("Table3");
TableColumn<Model, String> table4 = new TableColumn<Model, String>("Table4");
TableColumn<Model, String> table5 = new TableColumn<Model, String>("Table5");
ObservableList<Model> list = Listen.getUserList();
tableview.setItems(list);
tableview.setEditable(true);
tableview.getColumns().addAll(table1, table2, table4, table5, table3);
table1.setCellFactory(TextFieldTableCell.<Model>forTableColumn());
table2.setCellFactory(TextFieldTableCell.<Model>forTableColumn());
table3.setCellFactory(TextFieldTableCell.<Model>forTableColumn());
table4.setCellFactory(TextFieldTableCell.<Model>forTableColumn());
table5.setCellFactory(TextFieldTableCell.<Model>forTableColumn());
textField1.setVisible(false);
checkbox.setVisible(false);
vbox2.setVisible(false);
hbox.setSpacing(10.0);
hbox.getChildren().addAll(button, textField1, checkbox);
vbox.setLayoutX(10.0);
vbox.setLayoutY(30.0);
vbox.setSpacing(30.0);
vbox.setPadding(new Insets(10.0, 20.0, 30.0, 10.0));
vbox.getChildren().addAll(label, hbox);
vbox2.setLayoutX(300.0);
vbox2.setLayoutY(30.0);
vbox2.setSpacing(30.0);
vbox2.setMaxHeight(200.0);
vbox2.setPadding(new Insets(50.0, 30.0, 30.0, 30.0));
vbox2.getChildren().add(tableview);
hbox2.setLayoutX(10.0);
hbox2.setLayoutY(500.0);
hbox2.setSpacing(20.0);
hbox3.setLayoutX(10.0);
hbox3.setLayoutY(550.0);
hbox3.setSpacing(20.0);
hbox3.getChildren().addAll(addButton, cancelButton, saveButton, addTextFieldButton);
button.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
textField1.setVisible(true);
checkbox.setVisible(true);
}
});
checkbox.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
vbox2.setVisible(true);
textField1.setEditable(false);
}
});
addTextFieldButton.setOnAction(e -> hbox2.getChildren().add(new TextField()));
addButton.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
//list.add(new Model();
tableview.refresh();
}
});
pane.getChildren().addAll(vbox, vbox2, hbox2, hbox3);
scene.getStylesheets().add("style.css");
secondStage.setTitle("GBO-Tool");
secondStage.setScene(scene);
secondStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Code:
package Tableview;
import javafx.beans.property.SimpleStringProperty;
public class Model {
private SimpleStringProperty table1;
private SimpleStringProperty table2;
private SimpleStringProperty table3;
private SimpleStringProperty table4;
private SimpleStringProperty table5;
public Model(String table1, String table2, String table3, String table4, String table5) {
this.table1 = new SimpleStringProperty(table1);
this.table2 = new SimpleStringProperty(table2);
this.table3 = new SimpleStringProperty(table3);
this.table4 = new SimpleStringProperty(table4);
this.table5 = new SimpleStringProperty(table5);
}
// Getter Setter
public String getTable1() {
return table1.get();
}
public void setTable1(SimpleStringProperty table1) {
this.table1 = table1;
}
public String getTable2() {
return table2.get();
}
public void setTable2(SimpleStringProperty table2) {
this.table2 = table2;
}
public String getTable3() {
return table3.get();
}
public void setTable3(SimpleStringProperty table3) {
this.table3 = table3;
}
public String getTable4() {
return table4.get();
}
public void setTable4(SimpleStringProperty table4) {
this.table4 = table4;
}
public String getTable5() {
return table5.get();
}
public void setTable5(SimpleStringProperty table5) {
this.table5 = table5;
}
public void setTable1(String newValue) {
// TODO Auto-generated method stub
}
public void setTable2(String newValue) {
// TODO Auto-generated method stub
}
public void setTable3(String newValue) {
// TODO Auto-generated method stub
}
public void setTable4(String newValue) {
// TODO Auto-generated method stub
}
public void setTable5(String newValue) {
// TODO Auto-generated method stub
}
}
Freue mich auf Vorschläge und Tipps.
Danke