Here is my question.
I have created a hardcoded tree view with a few items in it.
There are nodes and leaf nodes in it.
how will i find the selected node is the leaf node.?
and i want to pass the content of that node to a new tab.
public void SetTreeSample(){
TreeItem<String> rootItem = new TreeItem<>("elan/6");
rootItem.setExpanded(true);
TreeItem<String> item = new TreeItem<>("Verkauf");
TreeItem<String> itemBeleg = new TreeItem<>("Belegbearbeitung");
itemBeleg.getChildren().add(new TreeItem<>("Verkaufsbeleg anlegen"));
itemBeleg.getChildren().add(new TreeItem<>("Verkaufsbelege bearbeiten"));
itemBeleg.getChildren().add(new TreeItem<>("Verkaufsbeleg abrufen"));
item.getChildren().add(itemBeleg);
TreeItem<String> itemXML = new TreeItem<>("XML-Schnittstelle");
item.getChildren().add(itemXML);
TreeItem<String> itemKun = new TreeItem<>("kunden");
item.getChildren().add(itemKun);
TreeItem<String> itemKund = new TreeItem<>("kunden-Auskun ft");
item.getChildren().add(itemKund);
TreeItem<String> itemArt = new TreeItem<>("Artikel-Auskunft");
item.getChildren().add(itemArt);
rootItem.getChildren().add(item);
item = new TreeItem<>("Kassenprogramme");
item.getChildren().add(new TreeItem<>("Kassenprogramm"));
item.getChildren().add(new TreeItem<>("Gutscheinverwaltung"));
rootItem.getChildren().add(item);
item = new TreeItem<>("Versand");
rootItem.getChildren().add(item);
item = new TreeItem<>("Bestandsfutrung");
rootItem.getChildren().add(item);
TreeView<String> treeview = new TreeView<>(rootItem);
StackPane root = new StackPane();
root.getChildren().add(treeview);
treeview.setOnMouseClicked(new EventHandler<MouseEvent>()
{
#Override
public void handle(MouseEvent mouseEvent)
{
if(mouseEvent.getClickCount() == 2)
{
TreeItem<String> item = treeview.getSelectionModel().getSelectedItem();
AddTab(item.getValue());
}
}
});
rootLayout.setLeft(root);
}
public void AddTab(String Text){
FXMLLoader loader = new FXMLLoader();
loader.setLocation(SampleApp2.class.getResource("view/NewAP.fxml"));
TabPane tabPane = (TabPane) rootLayout.getCenter();
tabPane.setTabClosingPolicy(TabClosingPolicy.ALL_TABS);
Tab tabdata = new Tab();
Label tabALabel = new Label(Text);
tabdata.setGraphic(tabALabel);
tabPane.getTabs().add(tabdata);
}
There is a method isLeaf() on the TreeItem.
TreeItem<String> selectedItem = treeview.getSelectionModel().getSelectedItem();
if (selectedItem.isLeaf()) {
AddTab(item.getValue());
}
Related
I am working on a Yahtzee game just to develop my programming skills and I can't seem to get all the Labels and Radio Buttons to appear in the HBox. Here is all the relevant code:
public class Main extends Application{
Label diceRoll1 = new Label("1");
Label diceRoll2 = new Label("2");
Label diceRoll3 = new Label("3");
Label diceRoll4 = new Label("4");
Label diceRoll5 = new Label("5");
RadioButton holdRButton1 = new RadioButton("Hold");
RadioButton holdRButton2 = new RadioButton("Hold");
RadioButton holdRButton3 = new RadioButton("Hold");
RadioButton holdRButton4 = new RadioButton("Hold");
RadioButton holdRButton5 = new RadioButton("Hold");
Button rollDice = new Button("Roll!");
Stage window;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage)throws Exception{
VBox diceRoll1Layout = new VBox(5);
diceRoll1Layout.getChildren().addAll(diceRoll1, holdRButton1);
VBox diceRoll2Layout = new VBox(5);
diceRoll2Layout.getChildren().addAll(diceRoll2, holdRButton2);
VBox diceRoll3Layout = new VBox(5);
diceRoll3Layout.getChildren().addAll(diceRoll3, holdRButton3);
VBox diceRoll4Layout = new VBox(5);
diceRoll4Layout.getChildren().addAll(diceRoll4, holdRButton4);
VBox diceRoll5Layout = new VBox(5);
diceRoll5Layout.getChildren().addAll(diceRoll5, holdRButton5);
HBox diceRolls = new HBox(5);
diceRolls.getChildren().addAll(diceRoll1Layout,diceRoll2Layout,diceRoll3Layout,
diceRoll4Layout,diceRoll5Layout, rollDice);
BorderPane rootPane = new BorderPane();
rootPane.setBottom(diceRolls);
Scene scene = new Scene(rootPane, 400, 500);
window = primaryStage;
window.setTitle("Yahtzee!");
window.setScene(scene);
window.show();
}
}
But for some reason whenever I run the code only diceRoll1, diceRoll2, holdRButton1, holdRButton2 and rollDice appear in the window. They're all coded the same so I don't know why they don't all appear. I tried setting them to visible and it made no difference. Am I missing something?
i am having a problem when i click on the button it doesn t work.
actually for the moment i am just asking that the button execute a system.out.println() just for the test. the name of the button is btndetailUser
listRechercheUser.setCellFactory(new Callback<ListView<User>, ListCell<User>>() {
#Override
public ListCell<User> call(ListView<User> p) {
ListCell<User> cell;
cell = new ListCell<User>() {
#Override
protected void updateItem(User user, boolean bln) {
super.updateItem(user, bln);
if (user != null) {
ImageView img;
String mm=user.getPath().trim();
if (mm.equals("fusee.png")){
img = new ImageView(new Image("mto/cr/images/"+mm));}
else{
img = new ImageView(new Image("mto/cr/images/mains-fonds.png"));
}
VBox vb = new VBox(10);
HBox hb = new HBox(20);
VBox vb1 = new VBox(20);
VBox vb3= new VBox(20);
vb1.getChildren().add(new Label(" "));
hb.setStyle("-fx-border-color: #C8C8C8 ;");
vb.setAlignment(Pos.CENTER);
VBox vb2 = new VBox(30);
vb.setAlignment(Pos.CENTER);
vb2.setAlignment(Pos.BASELINE_LEFT);
vb3.setAlignment(Pos.CENTER_RIGHT);
Label id = new Label(user.getId()+"id firas:");
Label username = new Label(user.getUsername()+" username firas:");
Label nom = new Label(user.getNom()+"nom firas:");
Label prenom = new Label(user.getPrenom()+"prenom firas:");
Button btndetailUser = new Button();
btndetailUser.setId("btndetailUser");
btndetailUser.setText("btndetailUser "+user.getId());
Image imageAccesProjet = new Image("mto/cr/images/enter.png");
ImageView enterIV= new ImageView(imageAccesProjet);
enterIV.setFitHeight(10);
enterIV.setFitWidth(10);
btndetailUser.setGraphic(enterIV);
btndetailUser.getStyleClass().add("buttonLogin");
System.out.println(btndetailUser.getText());
btndetailUser.setOnMouseClicked(
(MouseEvent event2) ->
System.out.println("firas"));
btndetailUser.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println(".handle()");
}
});
vb.getChildren().addAll(new Label(" "), img, new Label(" "),username);
vb2.getChildren().addAll(nom, prenom);
vb3.getChildren().addAll(btndetailUser);
hb.getChildren().addAll(vb1, vb, vb2,vb3);
setGraphic(hb);
}
}
};
return cell;
}
});
try something like,
btndetailUser.setOnAction(e -> {
System.out.println("freetext")
});
I'd appreciate any help at all, I have spent hours at this to no avail.
So I need to add an instance of a class I found online called GridDisplay to the Parent root, as I need the scene to then display this for me. But I also need to use the fxml file to set up the root and hence the scene. Here is what I have so far:
#FXML private BorderPane mainPanel = new BorderPane();
#FXML private TilePane tilePane = new TilePane();
#FXML private Pane topCanvas = new Pane();
#FXML private Pane leftCanvas = new Pane();
#FXML private Pane rightCanvas = new Pane();
#FXML private Pane bottomCanvas = new Pane();
#FXML private ScrollPane scrollPane = new ScrollPane();
#FXML private Button stacksButton = new Button();
#FXML private Button queueButton = new Button();
#FXML private Button stepStacksButton = new Button();
#FXML private Button stepQueueButton = new Button();
private MazeSolver solver;
private GridDisplay gridDisplay;
private ArrayList<String> maze;
private String fileLocation;
private int height;
private int width;
#Override
public void start(Stage primaryStage) throws IOException, InterruptedException
{
solver = new MazeSolver(fileLocation);
int[] dimensions = solver.getDimensions();
this.width = dimensions[0];
this.height = dimensions[1];
maze = solver.getMazeLayout();
primaryStage.setTitle("Maze Solver");
//this is the root which uses the fxml file and in turn sets up the scene
Parent root = FXMLLoader.load(getClass().getResource("MazeApp.fxml"));
Scene scene = new Scene(root);
//Represents the grid with Rectangles
gridDisplay = new GridDisplay(height, width);
gridDisplay.setMaze(maze);
mainPanel = new BorderPane();
//here i set the scroll pane as the mainPanel's content
mainPanel.setCenter(scrollPane);
//and then set the gridDisplay as the scroll panes content
scrollPane.setContent(gridDisplay.getDisplay());
//re paints the grid and its boxes
gridDisplay.createElements();
primaryStage.setScene(scene);
primaryStage.show();
}
The order in which I do any of these things makes no difference, it just never shows. I am always left with the window and a blank space where the maze should be, like so: https://www.dropbox.com/s/rqxa9dy2w9lw4tz/Screenshot%202015-03-11%2022.01.46.png?dl=0 (sorry about linking, cant post image directly).
Any help would be greatly appreciated.
EDIT:
My GridDisplay class:
public class GridDisplay{
private static final double ELEMENT_SIZE = 30;
private static final double GAP = ELEMENT_SIZE / 90;
private TilePane tilePane = new TilePane();
private Group display = new Group(tilePane);
private int nRows;
private int nCols;
private ArrayList<String> maze = null;
public GridDisplay(int nRows, int nCols)
{
tilePane.setStyle("-fx-background-color: rgba(255, 215, 0, 0.1);");
tilePane.setHgap(GAP);
tilePane.setVgap(GAP);
setColumns(nCols);
setRows(nRows);
}
public void setColumns(int newColumns)
{
nCols = newColumns;
tilePane.setPrefColumns(nCols);
createElements();
}
public void setRows(int newRows)
{
nRows = newRows;
tilePane.setPrefRows(nRows);
createElements();
}
public TilePane getDisplay()
{
return tilePane;
}
public void createElements()
{
tilePane.getChildren().clear();
for (int i = 0; i < nRows; i++) {
for (int j = 0; j < nCols; j++) {
if(maze==null)
tilePane.getChildren().add(createElement(Color.RED));
else
{
Color color = Color.WHITE;
if(Character.toString(maze.get(i).charAt(j)).equals("#"))
color = Color.BLACK;
if(Character.toString(maze.get(i).charAt(j)).equals("o"))
color = Color.GREEN;
if(Character.toString(maze.get(i).charAt(j)).equals("*"))
color = Color.RED;
tilePane.getChildren().add(createElement(color));
}
}
}
}
public void colorSolved()
{
tilePane.getChildren().clear();
for (int i = 0; i < nRows; i++) {
for (int j = 0; j < nCols; j++) {
if(maze==null)
tilePane.getChildren().add(createElement(Color.RED));
else
{
Color color = Color.WHITE;
if(Character.toString(maze.get(i).charAt(j)).equals("#"))
color = Color.BLACK;
if(Character.toString(maze.get(i).charAt(j)).equals("o"))
color = Color.GREEN;
if(Character.toString(maze.get(i).charAt(j)).equals("*"))
color = Color.RED;
tilePane.getChildren().add(createElement(color));
}
}
}
}
public void setMaze(ArrayList<String> maze)
{
this.maze = maze;
}
private Rectangle createElement(Color color) {
Rectangle rectangle = new Rectangle(ELEMENT_SIZE, ELEMENT_SIZE);
rectangle.setStroke(Color.BLACK);
rectangle.setFill(color);
return rectangle;
}
}
I cannot share the code for the xml as it will not paste into this window properly, here is a picture:
https://www.dropbox.com/s/27tbliubdlqdfzm/Screenshot%202015-03-12%2010.09.25.png?dl=0
Since your root is a BorderPane, you can just set the appropriate region in the code:
BorderPane root = FXMLLoader.load(getClass().getResource("MazeApp.fxml"));
Scene scene = new Scene(root);
//Represents the grid with Rectangles
gridDisplay = new GridDisplay(height, width);
gridDisplay.setMaze(maze);
ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(gridDisplay);
root.setCenter(scrollPane);
// etc
As you can see in the picture, the label in the third box, next to the ComboBox, has the text '...' instead of 'fase'.
I have previously attempted to increase the size of the HBox in which it resides, increasing spacing, but to no avail. Any suggestions?
public void start(Stage primaryStage) {
RadioButtonCustom inhoud = new RadioButtonCustom();
Fieldset fs = new Fieldset("eerste", inhoud);
RadioButtonCustom inhoud2 = new RadioButtonCustom();
Fieldset fs2 = new Fieldset("tweede", inhoud2);
//error
HBox hb = new HBox();
hb.minWidth(200);
Label fase = new Label("fase");
fase.setText("Fase");
fase.minWidth(500);
fase.getStyleClass().add("bordered-titled-title");
StackPane.setAlignment(fase, Pos.CENTER);
ComboBox<Integer> cb = new ComboBox<>();
hb.setSpacing(40);
for (Integer i : array) {
cb.getItems().add(i);
}
hb.getChildren().addAll(fase,cb);
Fieldset fs3 = new Fieldset("fase",hb);
// einde error
VBox vb = new VBox();
vb.getChildren().addAll(fs, fs2,fs3);
vb.setSpacing(20);
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
BorderPane root = new BorderPane();
root.getChildren().add(vb);
Scene scene = new Scene(root, 500, 500);
scene.getStylesheets().add("/lissa/stijl.css");
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
I can add SplitPane in a XYChart by clicking the "Add Pane" button, and it works fine.
Now I would like to remove SplitPane by clicking the "Remove pane" button.
Here is full code
public class SplitPaneFxTest extends Application {
SplitPane splitPane = new SplitPane();
double percSplit = 0.50;
int idxSplit = 0;
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(SplitPaneFxTest.class, args);
}
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("SplitPane Test");
Group root = new Group();
Scene scene = new Scene(root, 800, 650, Color.WHITE);
//CREATE THE SPLITPANE
splitPane.setPrefSize(scene.getWidth(), scene.getHeight());
splitPane.setOrientation(Orientation.VERTICAL);
//ADD LAYOUTS AND ASSIGN CONTAINED CONTROLS
BorderPane upperPane = new BorderPane();
HBox hbox = new HBox();
Button button1 = new Button("Add Pane");
hbox.getChildren().addAll(button1);
upperPane.setTop(hbox);
Button button2 = new Button("Remove Pane");
hbox.getChildren().addAll(button2);
upperPane.setTop(hbox);
BorderPane lowerPane = new BorderPane();
splitPane.getItems().addAll(upperPane);
splitPane.setDividerPosition(idxSplit, 0.70);
idxSplit++;
splitPane.getItems().addAll(lowerPane);
idxSplit++;
root.getChildren().add(splitPane);
primaryStage.setScene(scene);
primaryStage.show();
button1.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent e) {
BorderPane myborderpane = new BorderPane();
splitPane.getItems().addAll(myborderpane);
ObservableList<SplitPane.Divider> splitDiv = splitPane.getDividers();
System.out.println("splitDiv.size() "+splitDiv.size());
percSplit = 1/(double)(splitDiv.size()+1);
for (int i = 0; i< splitDiv.size(); i++) {
System.out.println("i "+i+" percSplit "+percSplit);
splitPane.setDividerPosition(i, percSplit);
percSplit += 1/(double)(splitDiv.size()+1);
}
}
});
}
}
Any help really appreciated.
You can store a list of inner panes and remove them based on this list:
final List<Pane> panes = new ArrayList<Pane>();
button1.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
BorderPane myborderpane = new BorderPane();
//adding
panes.add(myborderpane);
splitPane.getItems().addAll(myborderpane);
ObservableList<SplitPane.Divider> splitDiv = splitPane.getDividers();
System.out.println("splitDiv.size() " + splitDiv.size());
percSplit = 1 / (double) (splitDiv.size() + 1);
for (int i = 0; i < splitDiv.size(); i++) {
System.out.println("i " + i + " percSplit " + percSplit);
splitPane.setDividerPosition(i, percSplit);
percSplit += 1 / (double) (splitDiv.size() + 1);
}
}
});
button2.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
if (panes.size() > 0) {
// removing from both list and splitPane childs
Pane toDelete = panes.remove(0);
splitPane.getItems().remove(toDelete);
}
}
});
Also you can remove panes directly from ScrollPane.getChildren() but it can involve tricky and unreliable code to determine which pane to remove.