treeitem icon doesn't show - java

I'm working with treeview in javaFx, I want to put an icon to the root node, and other icon for every child. I was trying to put it at least to the root node, but it still shows me the arrow, I declare the node in the next way:
TreeItem<Object> rootNode = new TreeItem<Object>("Agentes");
rootNode.setGraphic(new ImageView(new Image(getClass().getResourceAsStream("/images/A.png"))));
I have the image "A.png" in a package called application.images
, and my loader class is Main.java is located in application, this is my directory:
when I run the application this is the treeview that I get:
it doesn't show me error or something like this. I must say, A.png is a image with 16x16Pixels. I don't know what I'm doing wrong, Thanks!

I resolved it, putting setGraphic(new ImageView(new Image(getClass().getResourceAsStream("/images/A.png")))); into the updateItem( Object item, boolean empty ) method in the a class that extends from TreeCell

Related

Do you also got a default model with colors, sports and food in Java and Jtree [duplicate]

I created a form with default NetBeans edito and put a jTree on it.
It somehow then creates bunch of elements such as "colors", "sports", "food" in there. But it is not in the creation code. Where is it coming from and how can I edit it...
Even if I do jTree1.removeAll(); everything is still there... and non of my code for adding new items to the jTree working.
private void test(java.awt.event.MouseEvent evt) {
//trying to remove all, but it does not remove anything
jTree1.removeAll();
//it does print it in debug meaning that this function is called
System.out.println("qwe");
//create the root node
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
//create the child nodes
DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Child 1");
DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Child 2");
//add the child nodes to the root node
root.add(child1);
root.add(child2);
//now how do I add it to the tree?
//???
}
I need to be able to edit jTree contents at runtime.
Problem in next you create your JTree like this JTree tree = new JTree() (according to docs) it has sample nodes. Add next lines after you create your nodes(root,child1,child2) and all will be work:
DefaultTreeModel model =(DefaultTreeModel) jTree1.getModel();
model.setRoot(root);
Also you needn't to call jTree1.removeAll(); it is used for other purposes.(docs)
Read tutorial for JTree
Initialize your JTree inside the custom GUI initializer createUIComponents() method.
To create custom GUI initializer source code for a certain component, follow this general procedure:
Select the desired component.
In the Inspector, check the option Custom Create.
In the text editor, locate the method createUIComponents(), and type the desired source code. The code in this method will not be removed on compilation.
Full explanation:
https://www.jetbrains.com/help/idea/creating-form-initialization-code.html

JavaFX lssue when loading fxml file and seting its Label text

I made a similar question yesterday but I think it wasnt well explained, so I wanted to ask it again but with some changes I made in my code. I apologise if i write too much but i want to make everything understandable.
So, Im making a pokemon simulator where you can catch and train pokemon.
I have one main fxml file which contains buttons to access to the diferent fxml files like catch, battle, shop, bag...
Yesterday i was doing the bag where all your items will be stored.
Everything is working fine and the windows are switching between them properly. The problem comes when i was trying to add a Label to each item in the bag, which was suposed to show the user the cuantity he has of each item. So i created all the Labels with no text so they are empty.
They will get filled from the information i get from a database, this thing also works properly, i connect to the db and get the item cuantity. The problem comes when i want to show that item cuantity in my bag window.
Why? Because as you can imagine, i want that when you click on the bag button, the bag file loads with all the Labels filled with the cuantity of each item. The Labels are defined on the bag fxml controller, so if i want to fill them with some text, i cant do it from my main windows which uses another controller, i need to do it throught the bag controller.
This is the code i tried to make it work(Located in main controller):
#FXML
void mochila (ActionEvent event) throws IOException, SQLException {
AnchorPane pane = FXMLLoader.load(getClass().getResource("mochila.fxml"));
anchorPaneContent.getChildren().setAll(pane);
anchorPane2.setStyle("-fx-background-color: #3f3f3f;");
cm.getCantidad();
}
getCantidad is a function that i have in my bag controller and this is it:
public void getCantidad() {
lblpokeballCount.setText("Cantidad: "+pokeballcount);
lblsuperballCount.setText("Cantidad: "+superballcount);
lblultraballCount.setText("Cantidad: "+ultraballcount);
lblmasterballCount.setText("Cantidad: "+masterballcount);
}
So when i try to run this function from the main controller, it returns me null pointer exception. That means the labels are not initialized but when i type first AnchorPane pane = FXMLLoader.load(getClass().getResource("mochila.fxml"));Shoudlnt all the resources from the file be loaded?
Because i creaded a button in my bag file, when on clicked runs the same function, and it works correctly because im calling it from the same controller/file.
So now i dont really know what to do, this is a proyect for school but my programing teachers have never touched javafx so they dont even know what im doing. You are my only hope. I tried to understand this post: post
But i dont understand it at all as im new to all this stuff. So guys if you can help me i would be really gratefull, thanks!
edit:
#FXML
void mochila (ActionEvent event) throws IOException, SQLException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("mochila.fxml"));
anchorPaneContent.getChildren().setAll(loader);
controladorMochila controller = loader.<controladorMochila>getController();
controller.getCantidad();
}
anchorPaneContent is an anchorpane thats inside the main pane. All the buttons are in the main pane, and depending on the button you click, anchorpanecontent will change for another fxml file. I tried to do it like in the post i mentioned above. But i cant do anchorPaneContent.getChildren().setAll(loader); because it says: the node setAll is not aplicable for the arguments(FXMLLoader)
You are trying to add an FXMLLoader to an anchor pane, which will not work, since an FXMLLoader is not a visual component (it is a thing that loads FXML files).
Additionally, you are trying to get the controller from the FXMLLoader without actually loading the FXML file; this won't work because the controller class is specified in the FXML file (so the FXMLLoader doesn't know what kind of controller to create until it loads the file).
You need to load the FXML file and add the result to the anchor pane:
#FXML
void mochila (ActionEvent event) throws IOException, SQLException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("mochila.fxml"));
anchorPaneContent.getChildren().setAll(loader.load());
controladorMochila controller = loader.<controladorMochila>getController();
controller.getCantidad();
}
or, if you want to be a bit more explicit:
#FXML
void mochila (ActionEvent event) throws IOException, SQLException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("mochila.fxml"));
Parent pane = loader.load();
anchorPaneContent.getChildren().setAll(pane);
controladorMochila controller = loader.<controladorMochila>getController();
controller.getCantidad();
}

How to show new content on button click in JavaFX?

Code is at https://bpaste.net/show/b7aa0530f2ac (because of StackOverflow limitations)
I'm currently trying to use
//Menu.java
btn.setOnAction(event -> {
primaryStage.setScene(doubleclick);
});
to change scene from current scene to Scene doubleclick but it isn't found because it is in another class (Mouse.java). Also the variables in that class are needed for it to work. I've tried to copy over the code from Mouse.java to Menu.java but I don't know how to make that work.
So when I click button in the image above I see what is below:
...instead of the first content (main menu).

How to get the IDs of nodes inside HTMLEditor, JavaFX

I wish to remove some of the control buttons from HTMLEditor, since I do not need them. for that I need to reach the desired node. How can I know the IDs of nodes inside HTMLEditor? Please see the following. Thank you!
public class myApp extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("myApp.fxml")); //this fxml has HTMLEditor named htmlEditor.
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
Node someControlInsideHtmlEditor = root.lookup("#htmlEditor").lookup("#what_Is_The_ID_of_This_someControlInsideHtmlEditor")
}
}
Download Scenic View from here
Add this to your application's class path
Add the following line to your start() method's end:
ScenicView.show(scene);
Run the application
Two windows will pop up: the primaryStage with the HTMLEditor and Scenic View's Stage
Now you can visit every node of the Scene Graph. Open the tree at the left pane, and select a Node from the HTMLEditor. You can access the controls by their CSS class.
For example, open HTMLEditor -> ToolBar -> HBox, and select the first Button. Look at "styleClass" in the "Node Details" at the right side. You will need "html-editor-cut". It can be used with this code:
Button cutButton = (Button) root.lookup(".html-editor-cut");
don't know if you're still looking for this answer. In Java 8, and HTMLEditor only has one child, which is a GridPane. The first two children of that are the ToolBars, the third is a WebView. Remove the first two children from the gridpane to do the formatting you want. Does that help?

how to rename Tree View node or change Tree View node using javascript

I am using Tree View Structure in Java Script rename Node like parent or child Node.
I have code like that not working properly .
function rename(TreeId , TreeNode)
{
var selectnode = Ztree1.getSelectedNode();
}
above var declaration i will get error like error alert message. can any one please let me how i can modify Parent Node and Child Node .Advance Thanks.strong text
Add listener to tree, it works here for rename.
refer here Rename a treeViewer Node with SWT not for java script but will give you idea

Categories