I am using javafx for first time. I am using TableView and in 1 column TreeView is used.
Its root node is "Person (Joe,Jack)" and it has sub node like address, phone no etc. So now I want to give separate hyperlinks to Joe and Jack so that when they are clicked it redirects me to their friend lists.
so it will be shown like :Person (Joe,Jack) but "Person" remains string and should not have hyperlink. This is how I want root node of TreeView.
String root_string = "Person";
HyperLink name1 = new Hyperlink("Joe");
HyperLink name2 = new Hyperlink("Jack");
root_string = root_string +"("+name1+","+"name2"+);
TreeItem<String> root = new TreeItem<String>(root_string);
root.setExpanded(true);
root.getChildren().addAll(
new TreeItem<String>("address"),
new TreeItem<String>("phone")
);
TreeView<String> treeView = new TreeView<String>(root);
Here I want Person as simple string and Hyperlinks to Joe and Jack only..
Related
To make it clear, I'm creating a treeview that will instantiate itself after the user creates a folder and said folder will be added as treeitem.
I currently have this:
TreeView treeView = new TreeView();
// Create new folder
MenuItem menuItem1 = new MenuItem("Create New Folder");
menuItem1.setOnAction(e -> {
System.out.println("Please name your directory:");
Scanner in = new Scanner(System.in);
String strFolder = in. nextLine();
createFolder(strFolder); // Create folder
TreeItem rootFolder = new TreeItem(strFolder); // Create new TreeItem
treeView.setRoot(rootFolder); // Replace old folder with new one
// rootFolder.getChildren().add(rootFolder);
// rootItem.getChildren().add(rootFolder);
});
After the treeView, I declared a new Menu Item that which will trigger an event after being clicked on.
The event will ask the user for a name to use as the root folder for the treeView. Now thats working fine.
Now what I'm having troubles with, is how to create more folders inside the root folder created and display them as subfolders in the treeView?
So far my code only does replace the old root folder by the new one created. Instead of setting the root folder again, how can i make it so it just adds those folders inside the first one and display them - again - in the treeView as subfolders?
Did I explain myself?
Thanks.
Simply add TreeItem(s) to the child list of the TreeItem. The following example replaces the root, if no items are selected and otherwise adds the new item as a child of the selected one:
TreeView<String> treeView = new TreeView<>(); // never use raw type without good reason
// Create new folder
MenuItem menuItem1 = new MenuItem("Create New Folder");
menuItem1.setOnAction(e -> {
TextInputDialog dialog = new TextInputDialog(); // replacing console input with dialog here
dialog.setHeaderText("Please name your directory:");
String strFolder = dialog.showAndWait().orElse(null);
if (strFolder != null) {
TreeItem<String> newFolder = new TreeItem<>(strFolder); // Create new TreeItem
TreeItem<String> selection = treeView.getSelectionModel().getSelectedItem();
createFolder(strFolder); // Create folder ; TODO: make dependent on parent???
if (selection == null) {
treeView.setRoot(newFolder); // Replace old folder with new one
} else {
selection.getChildren().add(newFolder);
selection.setExpanded(true); // make sure we're able to see the new child
}
}
});
In the controller of my view, I created a list of HBoxes for the input of different values, each hbox consists of a textfield with an index number and two text boxes with information I want to retrieve. This is how I dynamically create each HBox.
void addRowToList() {
HBox hb = new HBox(50);
Text num = new Text(Integer.toString(numEdges + 1));
hb.getChildren().add(num);
TextField sep = new TextField();
makeNumeric(sep);
hb.getChildren().add(sep);
TextField area = new TextField();
makeNumeric(area);
hb.getChildren().add(area);
rows.add(hb);
}
After this, there is a point in which I need the information from those textBoxes in order to continue the execution of my program, my question is what is the method should I follow to access these since they don't have any given id? Thanks in advance.
Background
Using JavaFX, I render a TableView and a Button side by side.
The Table contains a number of entries which are more then the Table size. Hence TableView automatically shows a scrollbar for scrolling down.
The Problem
I want to scroll down the table by using the button. So pressing the button, scroll down the table by 5 entries. I found the scrollTo() method, which would be sufficient for the task, but I need an index to scroll to. Which I do not have, as far as I understand there is no "getfirstvisibleindex" method (or something similar, you get the idea).
How can a determine the table row index to scroll to?
Edit
Thanks everyone for helping me out. For the sake of completeness I post "my" final solution as well, which is based/copied on the link provided below, Link1 and Link2. The following method is implemented as part of an extented version of TableView and will be called by a button. (Side Node: I am not sure if the check for the end is reached cased is really necessary.)
public void scrollbartest(){
TableViewSkin skin = (TableViewSkin) getSkin();
VirtualFlow vf = null;
ObservableList<Node> kids = skin.getChildren();
if (kids != null && !kids.isEmpty())
{
vf = (VirtualFlow)kids.get(1);
}
if (vf == null) { return; }
if (vf.getFirstVisibleCell() == null) { return; }
int first = vf.getFirstVisibleCell().getIndex();
int last = vf.getLastVisibleCell().getIndex();
int numberofrows = this.getItems().size();
if(last != numberofrows-1){
this.scrollTo(first+1);
}
}
I'm using the tableview tutorial as an example: http://docs.oracle.com/javafx/2/ui_controls/table-view.htm
The TableView is backed by an observable list of some object type you define. In the tutorial, they use:
final ObservableList<Person> data = FXCollections.observableArrayList(
new Person("Jacob", "Smith", "jacob.smith#example.com"),
new Person("Isabella", "Johnson", "isabella.johnson#example.com"),
new Person("Ethan", "Williams", "ethan.williams#example.com"),
new Person("Emma", "Jones", "emma.jones#example.com"),
new Person("Michael", "Brown", "michael.brown#example.com")
);
Along with the line:
table.setItems(data);
The data list object has a size method which would be useful for your case. In your button action, you could look up the size of the list and set the scrollTo() index 5 positions forward. Just keep track of what the starting index is outside of the button action definition.
I have a created Entity Framework.I am trying to add a new songs and New album to the table. For example when I try to add a new song I used the method below and it tells me "Object reference not set to an instance of an object" on the s.Artist.ArtistName. I have a webform that shows a list of Artists in a drop down menu and now I want to add a new song for that artist. How do I do that?
public Song AddNewSongNav(String ArtistName, String SongName)
{
using (var context = new MyEntities())
{
var s = new Song();
s.SongTitle = SongName;
s.Artist.ArtistName = ArtistName;
s.Artist.WikipediaUrl="http://en.wikipedia.org/Testing";
context.Songs.AddObject(s);
context.SaveChanges();
return s;
}
}
The frame work for the Artist Table has ArtistID,ArtistName and WikiPediaURL. It has a navivgation property for Album and Song. It is linked to Song as 1 to many and Album as 1 to Many.
It has a Song table, which has a SongID, SongTitle and Artist_ArtistID. It has a navigation property of Artist and Album. It's linked as a many to many to the Albums table.
Finally the Album table has an AlbumID,AlbumTitle,CoverArt,Year,Genre,MimeType and Artist_ArtistID. It has a navigation property of Artist and Song.
The Artist property is not automatically created - you have to create an instance of the artist first:
var s = new Song();
s.SongTitle = SongName;
s.Artist = new Artist();
s.Artist.ArtistName = artistName;
I'd like to create a combobox which will get users from an arrayList & another field for creating a new user.
for example when i open the combobox i'd like to see the rows :
Create new user
User A
User B
where the users come form an arrayList and the "create new user" option is always there.
How can I do it?
P.S
I don't wan't to add "create new user" obj to the arryList - so this is not a desired solution.
Thanks for your help,
Dan
Just create an intermediate ArrayList, add the "create new user" option to it, copy all the elements from the other ArrayList, and finally assign it to the combobox:
var cbArray = new ArrayList();
cbArray.push("Create new user ");
for (int i = 0; i < yourArrayList.lenght; i++) {
cbArray.push(yourArrayList[i]);
}
// Then assign cbArray to the combo box