Reputation: 27
i have a GUI, in which i have two tabs. in one tab i want to display a another stage by a button click. i couldn't find a way to solve this problem. i hope you can help me.
I program the whole without fxml.
You can see the gui below. You can also find markings. My Main Window is the one with the markings. The other one should be displaying in the main window with button click.
public class Controller implements Observer {
public static final int WIN_WIDTH = 1064;
public static final int WIN_HEIGHT = 600;
CreateScheme cs = new CreateScheme();
private Pane pane;
VBox vbox = new VBox();
VBox vbox2 = new VBox();
HBox hbox = new HBox();
HBox hbox2 = new HBox();
Pane pane2 = new Pane();
TreeView<File> treeview = new TreeView<File>(new SimpleFileTreeItem(new File("C:\\")));
private TabPane tabPane1 = new TabPane();
private Tab tab3 = new Tab("Bewertungsschema");
private Tab tab4 = new Tab("Ordner");
TabPane tabPane = new TabPane();
Tab tab2 = new Tab("Code");
Tab tab1 = new Tab("Bewertung");
private Button createScheme = new Button("Bewertungsscheme erstellen");
private Button editScheme = new Button("Bewertungsscheme bearbeiten");
private Button exportScheme = new Button("Bewertungsscheme exportieren");
private Button startScheme = new Button("Bewertung starten");
TextArea textarea = new TextArea();
File file;
public void init() {
vbox.setLayoutX(10.0);
vbox.setLayoutY(35.0);
vbox.setSpacing(10.0);
vbox.setMaxHeight(500.0);
vbox.getChildren().add(treeview);
vbox.getChildren().add(tabPane1);
vbox2.setLayoutX(10.0);
vbox2.setLayoutY(50.0);
vbox2.setSpacing(30.0);
vbox2.setPadding(new Insets(80.0, 30.0, 30.0, 30.0));
vbox2.getChildren().addAll(createScheme, editScheme, exportScheme, startScheme);
treeview.setMaxWidth(220.0);
treeview.setMaxHeight(550.0);
treeview.setLayoutY(35.0);
tab2.setContent(textarea);
tab3.setContent(vbox2);
tab4.setContent(treeview);
//tab1.setContent(cs);
textarea.getMaxHeight();
textarea.getMinHeight();
tabPane.getTabs().addAll(tab1, tab2);
tabPane.setLayoutX(250.0);
tabPane.setLayoutY(30.0);
tabPane.setMinWidth(780.0);
tabPane1.getTabs().addAll(tab3, tab4);
tabPane1.setLayoutX(10);
tabPane1.setLayoutY(30);
pane = new Pane();
pane.getChildren().addAll(vbox, tabPane, tabPane1);
createScheme.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
changeStage();
}
});
}
public void changeStage() {
Stage secondStage = new Stage();
CreateScheme cs = new CreateScheme();
try {
cs.start(secondStage);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
secondStage.show();
}
public Pane getPane() {
return this.pane;
}
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("Bewertungsschema erstellen");
secondStage.setScene(scene);
secondStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Upvotes: 0
Views: 307
Reputation: 13859
I think you should load a Parent
node instead of a Stage
. In the example, BorderPane
is used as the root/Parent
. BoderPane
needs to be the root of the Tab
that you want to have this functionality.
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* @author blj0011
*/
public class JavaFXApplication353 extends Application
{
@Override
public void start(Stage primaryStage)
{
BorderPane root = new BorderPane();
Button btnDisplay1 = new Button("Display 1");
VBox.setMargin(btnDisplay1, new Insets(0, 0, 0, 5));
btnDisplay1.setOnAction((event) -> {
loadDisplay1(root);
});
Button btnDisplay2 = new Button("Display 2");
VBox.setMargin(btnDisplay2, new Insets(0, 0, 0, 5));
btnDisplay2.setOnAction((event) -> {
loadDisplay2(root);
});
VBox vbNav = new VBox(btnDisplay1, btnDisplay2);
vbNav.setAlignment(Pos.CENTER);
vbNav.setSpacing(5);
root.setLeft(vbNav);
loadDisplay1(root);//initial load of scene 1.
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
public void loadDisplay1(BorderPane root)
{
Label label = new Label("Scene 1");
StackPane displayRoot = new StackPane(label);
root.setCenter(displayRoot);
}
public void loadDisplay2(BorderPane root)
{
Label label = new Label("Scene 2");
StackPane displayRoot = new StackPane(label);
root.setCenter(displayRoot);
}
}
Upvotes: 1