Can someone please provide a sample code or at least a method that I can use to get the string values of multiple selections in a JTable? I searched the web, but I found only examples of how to get values from a single selection. Based on that I tried to implement the code myself using loops, but it blew up on my face. Any help will be greatly appreciated.
JTable has a method for that :
int[] selection = table.getSelectedRows();
Of course, this method returns the indices of the selected rows. You can use these indices to get the values you want from the the table model.
Related
I have implemented a JTable, on which I can search entries with a textField.
When a request is made, my code gets the DefaultTableModel, looks for the element in each column and row, and then sets the selection in the table to the line where the element was found.
This works, but it's useless if I sort the table by clicking on any column, because the sorting doesn't seem to update the DefaultTableModel.
The table is part of a bigger project which is extremely complicated and full of dependencies so I cannot post a small example, but I think this will sum it up:
Given a DefaultTableModel A full of non-sorted data about a JTable B, where B.setAutoCreateRowSorter() is true , how does one update B after several/any cloumn-sortings of A?
I have read the docs and also looked into this:
http://www.codejava.net/java-se/swing/6-techniques-for-sorting-jtable-you-should-know
As well as dug a bit into TableRowSorter#addRowSorterListener, but it can only tell me that a column was sorted, not really useful. Unless of course I use what that column is to try and sort all the values in a multidimensional array, then clear the table, then assign everything back.. but clearly this is extremely slow for and not really an option in my case.
Refer to this for the info provided by a RowSorterEvent:
https://docs.oracle.com/javase/7/docs/api/javax/swing/event/RowSorterEvent.html
Can someone point me in the right direction ?
When a request is made, my code gets the DefaultTableModel, looks for the element in each column and row...
So don't search the TableModel. You can use the table.getValueAt(...) method to search for the element in each row/column of the table. The data will be accessed in the currently sorted order of the table.
because the sorting doesn't seem to update the DefaultTableModel.
Correct, only the View (JTable) is updated.
If you want to keep searching the TableModel directly then you need to convert the model indexes to the view indexes whenever you want to invoke a JTable method (ie. selecting a table row). This is done by using the following JTable methods:
int columnColumn = table.convertColumnIndexToView(modelColumn);
int row = table.convertRowIndexToView(modelRow);
There are also methods to convert the view values to the model values.
I need to cancel all selections within a JTable model object. Java provides this function "clearSelection()" which does, what I need, as far as I understand.
But I am confused why this function can be called on a JTable object as well as on a selection model for a JTable object:
1) mytable.clearSelection();
2) mytable.getSelectionModel().clearSelection();
Both ways work, but I do not understand in what situation a clearSelection() of a SelectionModel (like at 2) ) would make any sense. As far as I understood SelectionModels, they are used to decide what kind of selections a JTable allows. I use the SelectionModel to only allow a Selection of exactly one row
//allow only one row to be selected
mytable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
Which way is to be preferred in what kind of situation? Is there a good reason not to use way 1?
I would be glad if anyone has some beginner friendly explanation for that. Thx in advance.
Here is the implementation of JTable#clearSelection()
public void clearSelection() {
selectionModel.clearSelection();
columnModel.getSelectionModel().clearSelection();
}
As you can see, there is two ListSelectionModel which are cleared, because you can select column and/or row and/or cell.
From Oracle tutorial :
JTable uses a very simple concept of selection, managed as an
intersection of rows and columns. It was not designed to handle fully
independent cell selections.
A ListSelectionModel handle all aspect of the selection such as which row is selected, how can we select some rows, etc... Not only the kind of selection !
More information in the Oracle JTable tutorial
Usually when you see two methods like that it is because the table will invoke the SelectionModel.clearSelection() method for you. So the table method is a convenience method.
In this case the actual code is:
public void clearSelection()
{
selectionModel.clearSelection();
columnModel.getSelectionModel().clearSelection();
}
So both the row and column selection models are cleared.
I have a JTable and a Model for that table.
Now I want to change the order of the columns and to hide or show some columns (e.g. like Windows Explorer in "Detail View" via Menu on rightclick).
My first problem here is, the getColumnName function. Do I have to keep track of, which column is at which place and then return the right columnName or is this already part of the model?
Same for the getValueAt function. Can I always return the value for the first column if I get columnIndex = 0, even if the user has dragged this column to the end of the table?
And nearly the same problem for adding/removing columns. If I do that, of course I have to fireTableStructureChanged, but do I also have to adapt e.g. the getColumnName function?
I haven't found a tutorial for that. All tutorials stop at "you can use a model".
I'd really like to see an example of such a dynamic model.
Thanks a lot.
You should use the getColumn(int) method of the model, and for accessing the model, you'll need to convert the row and column view indices with JTable's convertRowIndexToModel(int), convertColumnIndexToModel(int) and the equivalents for converting the model indices to view indices.
You need to understand the difference between the "View" and the "Model". When you reorder the columns in the JTable (view), this does not change the order of the data in the model.
If you want to access the first column that is displayed in the table you use:
table.getValueAt(row, 0);
if you want to access the first column in the model, then you use:
table.getModel().getValueAt(row, 0);
I want to hide or show some columns
See Table Column Manager.
I've a problem that is driving me crazy.
I have a classic JTable, with several columns. I need that a particular column, instead of simple texts values in its cell, it must contain a ComboBox. I searched A LOT, all I found was examples that would implement the same JComboBox in each cell of the column, that it's not what I need: I need that each cell of the column has a combo box with different values.
Can anyone give me some practical example of how to do it, please?
Thanks.
PS: I'm using NetBeans.
The TableCellEditor.getTableCellEditorComponent() method takes a row as argument. Use the existing example as a guide, and use the row argument of this method to decide which values must be proposed by the combo box.
I am currently building a database using JTable and DefaultTableModel. In my program I
have a function which allows users to search the database. I have the search part build but I don't
know how to highlight a row or a cell in the JTable. Can someone please help me?
Thank you
Are you sure you want to highlight as opposed to filter out the extraneous results? If you highlight, you'll have to scroll through the whole list to find all of the matching results, whereas if you filter the display, it's a lot easier to find what you're looking for.
If you go the filtering route I'd look into GlazedLists, a truly great Java library for doing things like dynamic filtering, sorting, etc. of JTables.
If you still want to go the highlighting route, then there's two main ways I see of accomplishing this. The first is to use the ListSelectionModel of the JTable and ensure that all of the matching rows are in the selected set; this will cause them to be visually distinguished with a minimum of coding. On the other hand, as soon as the user drags in the table and selects something else, the visual effect is lost.
The second way to accomplish this would be to use a custom TableCellRenderer that changes how a row is rendered if the row matches your selection criteria. An easy way to do that would be to change the background color.
The Swing tutorial on How to Use Tables has a section on filtering so you can just dislay the data that meet the search criteria. If you want to see all the data, then you just remove the filter.
If you really want to do separate highlighting then I would take a look at the Table Row Rendering approach.
I can tell you how I am doing it.
I implemented my search to work as the search in a document, i.e. finding single result at a time. I am storing the current index of the selected row or starting from the first one if no row was previously selected. Then I have my model implement my interface with functionality to search for next or previous match, example below shows use of find next match method which returns an index of the row in a table where the matching string was found, then I change the selection to it else I clear the selection to let the user know there is no match.
int index = serchableTableModel.findNextMatchIndex(serchedText, currentIndex);
if(index != -1)
table.changeSelection(index, 0, false, false);
else
table.clearSelection();
I hope this sorts your problem.
NOTE: I was not aware of the glazed lists before, they really seem promising. They would have saved me implementing sorting of tables, searching myself.