Java: JTable reordering rows and refresh - java

I have a JTable (extended) and an implementation of TableModel, in which I have the methods ...
moveEntryUp(int rowIdx)
moveEntryDown(int rowIdx)
... within the table model implementation. I have verified that these work properly.
However, I am having problems in getting the changes made in the table model to propagate to the actual JTable. In this table I only allow single row selection, and have seperate button for Up and Down. When a row is elected, I need to be able to move that row up and down, and preserve the selection.
E.g.
If my rows are [A, B, C(selected)], and I press the up button,
I should get [A, C(selected), B].
The various approaches I have tried so far either fail to refresh the values in the JTable, or lose the selection.
NOTE:
I am hesitant to use a solution which requires an explicit call to repaint().
Assume that all cells in the JTable are not editable.

Answering own question:
int row = mTable.getSelectedRow();
mTableModel.moveEntryUp(row);
mTable.getSelectionModel().setSelectionInterval(row -1, row -1);
Where mTable is the JTable, and mTableModel is my table model implementation, and is equivalent to mTable.getModel() (and typecast).
Should've poked around longer before asking!

Related

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.

How can delete/hide the mid row(5)th row before it listed in jtable

Here i have an 1-10 row is listed in jtable i want to delete/hide the 5th row before it listed in jtable.
i set the rowheight but it affected the cellselection.Is there any way to hide/delete the row without affected the normal flow code?
If i remove the row it will throws ArrayIndexoutofBoundException.
in my project executed means one gui open in that gui listed the some string. In here we can add the more string via Add Button on popup Button
Here what i need is i have to hide the particular string. That string is placed on 1st row.
i need to hide the string from end user.
now u hope understand.
You can use the JTable row filtering support in order to hide certain rows without deleting them from the model. Also see this: How can I filter rows in a JTable?
You can eliminate rows in the table by calling the removeRow() method. If you want to just hide it instead of elimintaing it you need to customize the JTable's model to meet your specs on what to display.
http://docs.oracle.com/javase/tutorial/uiswing/components/table.html
using DefaultTableModel with JTable you should be able to use model.removeRow(int row) function to remove A row from JTable. There is no way to hide a row based on index as much as i know. However, If you need to hide and re-show mechanism you need to save the row prior to delete it and Save the removedRow in a ArrayList to re-use them.. Something as follows:
List<Vector>deletedRows = new ArrayList<>();
Vector removingRow = (Vector) model.getDataVector().get(5);
deletedRows.add(removingRow);
model.removeRow(5);

How can I hide a row in my JTable?

I'm using Eclipse Indigo SR1 with JDK 1.7, on Windows 7 Pro.
I've written a desktop app, Swing based.
My app includes a JTable; it shows many records of type T, one row per record.
The table model points to Vector vect, named vect, containing all data to be shown in the JTable.
The app includes a combo, named sele, showing three values: 0, 1, 2.
When sele = 0, every record of vect has to be visible in the JTable.
When sele = 1, the JTable has to show only vect records having odd row index and all records with even row index mustn't be visible. Viceversa, when sele = 2.
So, here's my question: how can I make a row not visible in the JTable ?
I can't use the table model, because it points to vect that contains "all" data.
I tried a table cell renderer, but it seems that you can set the color of a cell, but you can't set it not visible or modify its size.
I've tried another way: if r is the row index, and I want that row to be not visible, I write table.setRowHeight(r,0), but this instruction throws an exception, the height can't be set to zero.
I could solve the problem by splitting the data, dividing vect in two, but I don't like that.
Does anybody have an idea ?
thanx in advance,
William
PS: someone told me to create a filtering TableModel that wraps the existing TableModel. The filtering model would be sensitive to the filtering criteria and fire the appropriate methods (TableDataChanged) when the filter was changed. The getRowCount method would return the filtered count. The getValueAt method would map the filtered row to the actual row in the underlying TableModel.
Mah, perhaps it's a good idea, but frankly I'm not able to understand it...
Use a TableRowSorter - which is:
An implementation of RowSorter that provides sorting and filtering using a TableModel. ..
See How to Use Tables & especially Sorting and Filtering for more info.

Column index not changing after dragging columns in Jtable

I am using JTable for displaying the information. After rendering the information if I drag the columns to reorder them, the information is displayed in the same fashion in the session. But when I try to capture the changes by checking the column names by iterating the column names, the sequence is same as the older one. Why is the latest view not available from the API?
As commented by Hovercraft Full Of Eels, the column indices in the view change independently of the column indices in the model. JTable's JavaDoc has this to say about it:
By default, columns may be rearranged in the JTable so that the view's columns appear in a different order to the columns in the model. This does not affect the implementation of the model at all: when the columns are reordered, the JTable maintains the new order of the columns internally and converts its column indices before querying the model.
JTable offers the methods convertColumnIndexToModel() and convertColumnIndexToView() that you can use to translate column numbers from one to the other. You can use these to figure out if (and how) the columns were rearranged.
To be notified of column changes as they happen, use a TableColumnModelListener:
myTable.getColumnModel().addColumnModelListener( new TableColumnModelListener() {
//etc.
} );

How to Mirror Edits across a Table Cell and Text Field

The desired behavior is akin to the mirrored text editing field provided in Excel when a given cell is selected, allowing more space to view the contents of the cell. I have a JTable with 5 columns and n rows. Column 2 holds expressions that can be arbitrarily long, thus I'd like to provide a separate JTextField to work with for editing the contents of the expression cell per row. The other fields are directly editable in the table. When the user clicks on a field in column 2, however, I want to send them to the text field. Any contents preexisting in the cell should be appear in the text field and additional edits in the text field should be mirrored in the table cell. Likewise, if someone double-clicks on the cell and edits it directly, I want those changes reflected in the text field. Thus, the user can choose to edit in either space and both are updated. Ideally, they are updated per keystroke, but update upon hitting return is acceptable.
So, far I've got the JTable, TableModel, TableModelListener, JTextField, ListSelectionListener, and AbstractAction, working together to provide most of the functionality described above. I'm missing the reflection of direct table cell edits to the text field and per-keystoke updates.
Are their ideas on how best to construct this behavior?
Well, if you want to get data from the table to the cell then you add the code to your TableModel's setValueAt() function, which should run when the user changes the content in an editable cell. I don't think that will update per-keystroke though.
If you want to move data from the textbox to the table cell use code like this
myJTextField.getDocument().addDocumentListener(new MyDocumentListener());
Where MyDocumentListener is an implementation of the javax.swing.event.DocumentListener interface
That will get you per-keystroke updates from the box to the table. But for the other way around it's a bit trickier.
There are two ways you might be able to go about doing it
1) Add a key listener to the table, and when the user starts typing check to see what table element is active, and intercept keystrokes as they type. That's kind of messy, though.
2) Another option might be to try to grab or replace the component that the table is using to actually let the user make the changes. I think that JTable actually allows you to change the editor component if you dig around.

Categories