我正在开发此GUI(编程的新手),基本上,当我右键单击“新组件”并添加“添加参数”并单击“高”或“低”时,应该显示带有2个选项卡的tabPane。我在“场景”构建器中为Borderpane指定了一个fx:id,但是我认为问题是场景构建器中的第一个Borderpane覆盖了所有内容,我尝试在ContextMenuTreeCell.java中创建一个新的borderpane(如下所示)。但是我不确定如何解决这个问题,我想在Controller.java中调用它
谢谢您的任何帮助。
Main.java
$to_warn
Controller.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class Main extends Application {
Stage window;
Button button;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
window = primaryStage;
window.setTitle("LOPA Software");
window.setScene(new Scene(root, 800, 500));
window.show();
}
}
ContextMenuTreeCell.java
package sample;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.util.Callback;
import java.awt.event.MouseEvent;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML
TreeView<String> treeView;
@FXML
TableView tableView1;
@FXML
TableView tableView2;
@FXML
BorderPane borderPane;
@FXML
Button button1;
@FXML
Button button2;
Image icon = new Image(
getClass().getResourceAsStream("/img/icon.png"));
private MouseEvent mouseEvent;
@Override
public void initialize(URL location, ResourceBundle resources) {
TreeItem<String> root = new TreeItem<>("Project", new ImageView(icon));
root.setExpanded(true);
TreeItem<String> compA = new TreeItem<>("New Component", new ImageView(icon));
root.getChildren().addAll(compA);
compA.setExpanded(true);
treeView.setRoot(root);
treeView.setEditable(true);
treeView.setCellFactory(new Callback<TreeView<String>,TreeCell<String>>(){
@Override
public TreeCell<String> call(TreeView<String> p) {
return new ContextMenuTreeCell();
}
});
}
sample.fxml
package sample;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.TextFieldTreeCell;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.util.converter.DefaultStringConverter;
import sun.reflect.generics.tree.Tree;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Map;
public class ContextMenuTreeCell extends TextFieldTreeCell<String> {
Image icon = new Image(
getClass().getResourceAsStream("/img/icon.png"));
private ContextMenu menu = new ContextMenu();
@FXML
BorderPane borderPane;
static final Map<TreeItem<String>, BorderPane> map = new HashMap<>();
public ContextMenuTreeCell() {
super(new DefaultStringConverter());
MenuItem addItem = new MenuItem("Add Component");
menu.getItems().add(addItem);
addItem.setOnAction(arg0 -> {
TreeItem<String> additem = new TreeItem<>("New Component", new ImageView(icon));
getTreeItem().getChildren().add(additem);
});
MenuItem addItem2 = new MenuItem("Add Parameter");
menu.getItems().add(addItem2);
addItem2.setOnAction(arg0 -> {
TreeItem<String> newLeaf = new TreeItem<>("Parameter", new ImageView(icon));
TreeItem<String> uxItem1 = new TreeItem<>("High", new ImageView(icon));
map.put(uxItem1, getcenterPane());
TreeItem<String> uyItem1 = new TreeItem<>("Low", new ImageView(icon));
map.put(uyItem1, getcenterPane2());
newLeaf.getChildren().add(uxItem1);
newLeaf.getChildren().add(uyItem1);
getTreeItem().getChildren().add(newLeaf);
});
MenuItem renameItem = new MenuItem("Rename");
menu.getItems().add(renameItem);
renameItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
startEdit();
}
});
MenuItem deleteitem = new MenuItem("Delete");
menu.getItems().add(deleteitem);
deleteitem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
TreeItem c = (TreeItem)getTreeView().getSelectionModel().getSelectedItem();
boolean remove = c.getParent().getChildren().remove(c);
}
});
}
public BorderPane getcenterPane() {
BorderPane borderPane = new BorderPane();
Tab tab = new Tab("Test tab");
Tab tab2 = new Tab("Tab 2");
TabPane tabPane = new TabPane(tab, tab2);
borderPane.setCenter(tabPane);
return borderPane;
}
public BorderPane getcenterPane2() {
BorderPane borderPane = new BorderPane();
Tab tab = new Tab("PHA Low");
Tab tab2 = new Tab("LOPA Low ");
TabPane tabPane = new TabPane(tab, tab2);
borderPane.setCenter(tabPane);
return borderPane;
}
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (!isEditing()) {
setContextMenu(menu);
}
}
}