2

I'm trying to create some kind of menu using tree view but I have the following problems : The tree view is small and I want to increase it's size but the css I used seem to not work . The tree is added on a anchor pane and I tried to resize it but there seems to be no change.Not only the items but the tree view doesn't change when I resize the container pane (it covers half of the scene but I want it as a side bar covering the left side of the scene)

public class ManagerPanel {

    AnchorPane anchor = new AnchorPane();

    public ManagerPanel(){

        TreeItem sideBar = new TreeItem();

        TreeItem createTest = new TreeItem("create test");

        TreeItem addStudents = new TreeItem("add students");
        TreeItem addQuestions = new TreeItem("add questions");

        createTest.getChildren().addAll(addStudents,addQuestions);

        TreeItem chats = new TreeItem("chats");

        sideBar.getChildren().addAll(createTest,chats);

        TreeView treeView = new TreeView();
        treeView.setRoot(sideBar);
        treeView.setShowRoot(false);

        AnchorPane container = new AnchorPane(treeView);
        treeView.getStyleClass().add("Tree.css");
        anchor.getChildren().add(container);

        Scene scene = new Scene(anchor);
        Stage stage = new Stage();
        stage.setWidth(1300);
        stage.setHeight(800);
        stage.setScene(scene);
        stage.show();
    }

}

I need to change the font too but nothing happens . This is the code I used in the css file :

.Tree .tree-cell{
    -fx-font: 20px "Calibri Light";
    -fx-stroke: #eeeeee;
    -fx-background-color: brown;
    -fx-text-fill: navajowhite;
    -fx-font-size: 20;
}

Thank you in advance :)

ps. I gave up on making it right aligned since I don't have much time and there isn't a lot of data about it.

vanes
  • 83
  • 8
  • Your CSS works for me (as in, it has an effect), except that I needed to surround the font name in quotes. If the CSS is not working _at all_ for you then please provide a [mre]. As for right-to-left orientation, check out the [`Scene#nodeOrientation`](https://openjfx.io/javadoc/14/javafx.graphics/javafx/scene/Node.html#nodeOrientationProperty()) and [`Node#nodeOrientation`](https://openjfx.io/javadoc/14/javafx.graphics/javafx/scene/Node.html#nodeOrientationProperty) properties. Unfortunately, neither property can be set from CSS. – Slaw Jun 30 '20 at 19:29
  • If it works for you I've probably made some obvious mistake or something that I don't know about , sorry , this is my first time working with a css file. I edited the post and tried to create a code like mine without making it long, hope it shows the problem. also thank you for the references , they helped a lot , my problem still remains but maybe with some changes I am able to use them. @Slaw – vanes Jul 01 '20 at 08:07
  • repeating - [mcve] please! and one problem per question – kleopatra Jul 01 '20 at 08:28
  • I did add the code needed, what I wrote is basically the code above with some new tree items but everything else is the same. @kleopatra – vanes Jul 01 '20 at 08:31
  • read the referenced help page... and act accordingly – kleopatra Jul 01 '20 at 08:32
  • I tried to put the best code resembling the complete one, sorry if it's still not good, I know you're trying to help. @kleopatra – vanes Jul 01 '20 at 08:42
  • 1
    Well ... don't quite get it: what you are showing are unrelated (to each other) code snippets. what you need to show is a single application class (plus the css snippet, that's fine) that I could copy into my IDE, compile, run - as is, no changes required by me - to reproduce the problem. It's your task to write such an example – kleopatra Jul 01 '20 at 09:09
  • that said: is css case dependent don't know?) If so, it should be .tree – kleopatra Jul 01 '20 at 09:13
  • Thank you very much for the help. I edited the post and also ran it on a different project to make sure it's ok to run . @kleopatra – vanes Jul 01 '20 at 10:06
  • Well done - and got you an answer :) – kleopatra Jul 01 '20 at 12:01

1 Answers1

1

Adding the Stylesheet

You're not adding the stylesheet to the scene graph correctly. You have:

treeView.getStyleClass().add("Tree.css");

Which, as the method name implies, is adding a style class. That's what would allow you to use .Tree { ... } in the stylesheet. However, you are adding "Tree.css" which indicates you're trying to use that method to add the stylesheet itself. You should change the above to:

// Ideally you'd use "tree" to follow CSS conventions, but "Tree" 
// will work. If you do use "tree" then you'll have to change .Tree
// to .tree in the stylesheet. Also, note that TreeView already has
// a style class of "tree-view" that you can use instead.
treeView.getStyleClass().add("Tree");

And add:

treeView.getStylesheets().add("<path-to-stylesheet>"); // or add to an ancestor of 'treeView'
// or
scene.getStylesheets().add("<path-to-stylesheet>");

See Scene#getStylesheets(), Parent#getStylesheets(), and the JavaFX CSS Reference Guide. To help determine what the path to the stylesheet should be check out:


Layouts

Your TreeView is small because of how you're using the AnchorPane. The AnchorPane layout works by setting constraints on its children. These constraints determine where to place the child relative to the sides of the layout. So if you want the TreeView to take up the entire left side of an AnchorPane you should use something like:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class App extends Application {

  @Override
  public void start(Stage stage) {
    TreeItem<String> rootItem = new TreeItem<>();

    TreeItem<String> createItem = new TreeItem<>("Create");
    createItem.getChildren().add(new TreeItem<>("New Student"));
    createItem.getChildren().add(new TreeItem<>("New Question"));
    rootItem.getChildren().add(createItem);

    rootItem.getChildren().add(new TreeItem<>("Chats"));

    TreeView<String> treeView = new TreeView<>(rootItem);
    treeView.setShowRoot(false);

    AnchorPane sceneRoot = new AnchorPane(treeView);
    AnchorPane.setTopAnchor(treeView, 0.0);
    AnchorPane.setLeftAnchor(treeView, 0.0);
    AnchorPane.setBottomAnchor(treeView, 0.0);

    stage.setScene(new Scene(sceneRoot, 600, 400));
    stage.show();
  }
}

Notice I don't set the anchor for the right side so that the TreeView uses its preferred width. You can then configure this preferred width (and min/max widths) to customize this.

However, using AnchorPane typically leads to an unresponsive UI (in the resizing sense). The layout encourages the use of hard-coded values which prevents nodes from resizing as the UI changes size. You should look into using other layouts which automatically reposition and resize their children to fill their allotted space. For example, you could use a BorderPane and set the TreeView as the left node:

BorderPane pane = new BorderPane();
pane.setLeft(treeView);

For more information check out Working with Layouts in JavaFX and the javafx.scene.layout package.


Left-to-Right Orientation

Regarding your goal of right-to-left orientation, you should look into using the Scene#nodeOrientation and/or Node#nodeOrientation properties.


Raw Types

Also check out What is a raw type and why shouldn't we use it?. Both TreeView and TreeItem are generic classes but you don't specify any type arguments. You appear to be using String as the value type so you should have, for example:

TreeItem<String> chats = new TreeItem<>("chats");
// and
TreeView<String> treeView = new TreeView<>();
// etc...
Slaw
  • 37,820
  • 8
  • 53
  • 80
  • Thank you so much , it worked :) I'll check the references you made and will write a better code. Also regarding to the first part of the question , do you happen to know how i can make the treeview bigger ? When all the items it the treeview collapse , the frame it's in becomes small for it and I also want it to cover the side of the scene. – vanes Jul 01 '20 at 10:48
  • I added a section to my answer (labeled it "Layouts"). – Slaw Jul 01 '20 at 11:15