Tree Table : links are not shown - java

I've created a custom Table structure by combining a JTree and JTable.
it looks like this:
addition of leafs can be done to any element.
But when I try to add a node to a row which has children already, the link in not shown and the newly added row is also not shown.
this is how it looks when a new row is added to "Package":
But this problem does not occurred when a node is appended to a leaf.
But there are no errors or exceptions thrown.
after a node is added i do the following:
fireTableDataChanged
fireTableStructureChanged
scrollPathToVisible
although these are called the problem remains.
can anyone please give a suggestion?
thanks in advance...

Try calling updateUI() on the JTable or JTree. updateUI() will propagate, rendering the rest of the components beneath it.
http://docs.oracle.com/javase/6/docs/api/javax/swing/JTable.html#updateUI()

Related

TreeViewer custom node rendering (adding checkboxe and clickeable image)

I'm actually trying to create a tree following that article : https://www.eclipse.org/articles/Article-TreeViewer/TreeViewerArticle.htm
But the problem is that I need to add 2 clickable images (1 for reloading sub-nodes and 1 for adding new sub-node)
And as optional, if someone can explain me how to add a check box on sub-nodes so then I can delete from the parent node all selected sub-nodes
Thank you in advance for all your help

Dynamic columns for JTreeTable

I am building a JTreeTable. I found some starter code and have come pretty far. In the end my goal is to be able to have different data at different levels like a hierarchical list.
Currently, I have it working with data at different levels. However, I am running up against a wall when it comes to changing the columns as a next goal. From where I currently stand I have 3 more milestones:
Show different set of columns for different levels
Ability to adjust column widths for different levels
Ensure the JTree part of the table always stays to left
I am getting close to closing out this task but again stuck at the first of these 3.
Since creating a JTreeTable is complex, the minimum example leverages several class listed below in the image:
I am happy to post the code to any of those classes but I also did not want clog the question with useless code. First let me show the functionality I want.
The first image is when the top level is selected and the second image is when the second level is selected. Notice how the columns are different. That is what I want to happen in my application.
Top level selected:
Second level selected:
So one way I tried to solve this problem, is when the list selection is changed inside this section of code:
ListSelectionListener listener = (ListSelectionEvent e) -> {
TreeTableModelAdapter adapter = (TreeTableModelAdapter) JTreeTable.this.getModel();
//Need to see why this breaks.
JTreeTable.this.getTableHeader().setColumnModel(adapter.getColumnModel());
};
this.getSelectionModel().addListSelectionListener(listener);
This code is in the initialization of the JTreeTable. I have tried setting the column model on both the TableHeader and the table as well. Below is what happens then when I select a row:
The columns just disappear on me. The creation of the column model is happening in the TreeTableModelAdapter class with the following method:
public TableColumnModel getColumnModel(){
DefaultTableColumnModel model = new DefaultTableColumnModel();
for(int i=0;i<getColumnCount();i++){
TableColumn column = new TableColumn();
column.setIdentifier(getColumnName(i));
model.addColumn(column);
}
return model;
}
Any direction would be very helpful. Again happy to post any code you think could be helpful to answer the question. Just put a comment in and I will add it right away.
I will add the milestones as I find them in case this helps others, but for now this question is answered.
Milestone 1
I was actually able to solve the first milestone. The key is to trigger the creation of the columns of the column model, not to create a new column model. Below is the code for when the row selection is changed:
//Change columns depending on row
ListSelectionListener listener = (ListSelectionEvent e) -> {
createDefaultColumnsFromModel();
};
this.getSelectionModel().addListSelectionListener(listener);
This code creates the columns based on the row selected in the JTree part of the JTreeTable. The TreeTableModelAdapter implements the getColumnCount() and getColumnName() methods by also passing the selected row in the JTree to the JTreeTableModel so that the columns and their names are dynamically retrieved based on a particular node in the JTree. The key for this for me was trigger those to be called again to update the JTreeTable.
Milestone 2
Adjusting column widths based on the data level proved to be much more difficult than I had originally anticipated. In order to retain the cells state when the column model changed I had to disconnect the painting of the cells from it. This is a hairy process because this is done inside BasicTableUI and the method that gets the rectangle of the cell is private. So I had to subclass it, overload the paint() method and create my own methods that get called inside the paint method. There was a lot of copy pasting so that I could call normally private methods. I just renamed them and referenced these methods instead. The way the ui class was designed did not make it very flexible. Below is 2 images where I am selecting different levels and the columns are obviously different widths at different levels.
Milestone 3
I was able to make this work by keeping track of the view in the model. This seems very dirty to me as the model should separated from the view. Since the tree column's class is unique, I just returned the right class if that column was the first in the view.
The one problem I have with this technique is that I get unexpected behavior where the value returned is not consistent. I attempted to resolve this by overriding JTree.covertValueToText(). Since a JTree only expects 1 value and depending on the sequence of columns in the view this value could change. So in overriding this method I check the stored index for the JTree column's value. Again this causes the unexpected behavior. I will update the post if I find the fix.

JTree Drag and Drop: drag nodes with its childs

I would like to add drag and drop feature to my JTree.
I have tried to run example from this thread. It works but it works only on last leaves in tree, not on nodes that contain some childs. I have also looked on this thread.
I have defined canImport and importData methods, and also have set setDragEnabled(true). I just cannot drag nodes also with its childs.
How to edit example from first link to drag also nodes with its childs? As I understand correctly, I should set DataFormat instance, to set what types should be dragged.
Can anybody help?

removeAllViews() not removing TableRow children in a TableLayout

Some background:
I am having an issue with one part of my program where I am building a 'edit database' view and then removing the rows again. The fields which the 'edit database' view will show are different each time it is shown. The edit database view is a tablelayout, and each row in the database is a row in the table layout. when the user exits the edit view, the view is supposed to be disposed or wiped of rows for the next time.
My issue, is no matter what I try, I can't get the rows in the tablelayout to delete. Here are the relevent pieces of code:
Get a handle on the tablelayout in the xml file
setContentView(R.layout.database_entry);
TableLayout editorLayout = (TableLayout)findViewById(R.id.DatabaseLayout);
the layout is passed to another class, where they are added to the layout like this:
//layout is the tablelayout from earler, passed into this function
TableRow row = new TableRow(contex);
row .setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
//stuff added to the row....
//...
//...
layout.addView(row);
When I attempt to wipe the Tablelayout, I run this code
TableLayout editorLayout = (TableLayout)findViewById(R.id.DatabaseLayout);
editorLayout.removeAllViews();
Whatever I do, the next time I hit the button to edit the code, it will show the previous rows, as well as the new rows added. They are not removed from the table. I get no error messages or any useful debugging information, it just isn't functioning.
I have struggled with this for a while. Tried dynamically creating the tablelayout(no xml file), tried deleting each row (or just ONE row) with it's index. I have used the Hierarchy Viewer to check to make sure everything is a child of that tablelayout (it is). If anyone has any insight, it would be much appreciated.
I was able to figure out the issue. First of all, the comment above was a little bit wrong, the mChildren array did not contain any pointers to the views I had deleted. They were nulled out, just as they should have been.
The reason why none of my rows were deleted is because right after I called removeAllViews() I switched content views setContentView(R.layout.main);. when I commented this line out, the remove children code worked as expected. I assume some android back-end optimization code decided that the removeAllViews() didn't need to be run, or updated somewhere in memory. And all the changes made while inside this function didn't apply.
I will be putting all of this code in a new activity (where it really belonged in the beginning). But I wanted to provide an explanation in case someone else is having the same issue.
After removing the row try setting the visibility to GONE and back to Visible again, This should fix the remove row glitch.

How to create inverted JTree in Java?

Actually I'm interested to create an inverted JTree in Java in such a way that root exists at top and its child nodes at the next level and so on. As we know in case of simple JTree the child nodes appears expanding on the right side of the parent node but I want to implement inverted JTree in my project where child nodes expands downwards which gives an appearance of "TREE" type of data structure.
As this is very essential for my project so can anyone suggest me the code for the above mentioned problem?
Thanks in advance.
Then JTree is not the correct choice. Perhaps, you should look into Java2D and stuff like that.

Categories