I would like to implement a JTable which will only be edited and updated programatically. I dont want user interaction enabled.
Firstly the input will be from an array of string elements(after filling the array from a String split).
I then want to set the fields with the array data.
How are the column names stored as Final or whatever for the table life?
Do I need to implement a TableModel if I dont want interaction?
You should probably read through the Java documentation for How to Use Tables.
Things you seem interested in:
isCellEditable() - Have it return false means the user can not edit any cells.
getColumnName() - Look up the name in an array or list of strings.
Regarding editability: use JXTable - it supports layered programatic configuration control. Specifically,
supports per-column and per-table narrowing (that is, to read-only)
respects the cell editable property always (that is table.setValueAt does nothing if the cell is not editable)
Yes, the model will be useful but not necessary (also just a DefaultTableModel).
Actually you could just fill the JTable with static data using the right constructor:
public JTable(Object[][] rowData, Object[] columnNames)
but you won't have any control over the data inside the table.
The model is not used just to provide interaction with editing capabilities but to provide a data source for the table either to read either to write, that's why it's always good to have one.
Related
I am working with the com.sencha.gxt(3.0) grid widget. I am trying to write a Java function that searches for a string in a row(across all columns) and only display the rows that contain this string. Is there a built in, or easy method in which I can do this?
Thanks
No.
(I'm pretty sure you didn't actually mean to write a "yes or no" question...)
In short, it would be silly for GXT to assume that all data in your grid is a string or string-like or only makes sense to compare in string-y ways. Plus, you might have all your data locally so it makes sense to do a simple filter on the client, or you might really be saying 'please search all ten million rows, on the server/db, and only send back the ones that match to render on the client.
Client-side filtering
The Store (probably a ListStore for a grid, or a TreeStore for the tree grid, but your question doesn't specify) has a built-in method for local filtering - you give it a function that takes an item (and some other possibly-relevant data), and returns true/false indicating whether or not the item should be hidden from the user. More than one filter can be applied, and if any filter says the item is hidden, then it is gone. In other words, this can be thought of as a sql where clause, where all statements are joined with and - if you want or, you can implement a filter that checks if any single filter wants the item visible.
This filtering mechanism operates over the entire row item, the T in Store<T>. It doesn't know what a column is, since you could be using the store in any number of ways - items in a combobox, points on a chart, etc. It lets you build your own tools to perform the filtering. If your data is generalized in a way that makes it easy to know what all possible columns are, then you could easily write a loop over those columns that checks if any one of them contains the string, in which case return false so that the item is visible. Or, since you already built the ColumnModel full of ColumnConfigs, each of which has a ValueProvider and you know that it makes sense to compare each value with .toString().contains(queryText), you could iterate over the columns that way.
Server-side filtering
In this case, we have very little control on the client of what is happening, since you are very likely off-loading that work to your backend database. We still know the list of columns in the column model, but we either send a query for cells matching that text, or send a single query for the entire row. Either way, we modify the paging load config object that is being used, and send a new query for the data we want, and the actual work of doing the filtering is the server's job. It sounds like from your question that this is not what you want, but without a lot more detail, its hard to say for sure.
You can achieve this through filtering.
Initially, the grid takes an argument as store and modification to the store will be directly reflected on the User Interface.
To enable filtering:
store.setEnableFilters(true);
To filter the grid, use the following commands:
store.addFilter(new StoreFilter<M>()
{
#Override
public boolean select(...,...,...)
{
//return true if you want to display the row, else false
}
}
I wanted to ask for a clarification about the use of RowFilter and its effects on performances.
I implemented a filter through the method include(Entry) that for every row simply checks if its correspondent value in the model has a boolean flag set: if so, returns true, else false.
Now, the JTable I have can be pretty big (1000000+ rows), and I wasn't sure if this simple filtering, applied to such a big input set, could be costly.
How does the mapping between filtered rows and underlying data work exactly? I mean, does it store any extra data or it just draws the rows that match the filter "on the fly"??
Thank you very much for the clarifications.
no component in any of programing languages aren't designated to displaying too largiest matrix of data on the screen, then you have two choises
faster way is let's this job for SQL engine, that designated for searching and filtering rows in hugest Database table
slower way is hold these data in HashMap and there apply Comparator, and JTable would be display only result from this Comparator
Expanding on #mKorbel's second point, a TableModel for a very large data set may contain a List<Record>, as suggested here. The list may be sorted using a suitable Comparator and dynamically partitioned using the subList() method. At any one time, the corresponding JTable can only see one such partition, and separate controls will be required to change partitions.
I am using JXTable which is from SwingX components. If I use setSortable(boolean flag) method then it will enable or disable sorting for all columns.
As of my requirement I want to disable sorting for a few columns and enable sorting for other columns.
Can anyone help achieve this functionality?
Thanks for your reply. Can you help me with using setSorterClass(String sorterClassName) to disable sorting for one column? Could you give me any code examplex? It will be very helpful for me.
SwingX supports a per-column sortable property on the level of the TableColumnExt. It's default value is true, to disable it after column creation
table.getColumnExt(myColumnIndex).setSortable(false)
Or at creation time, use a custom ColumnFactory, like
ColumnFactory factory = new ColumnFactory() {
#Override
public void configureTableColumn(TableModel model, TableColumnExt column) {
super.configureTableColumn(model, column);
if (... your condition to disable sortable) {
column.setSortable(false);
}
}
}
table.setColumnFactory(factory);
table.setModel(model);
JXTable will take care of synchronizing the column property to the sorter, provided it is of type SortController (which is the default)
I think, at least according to what I have found on the net you can achieve it by setting
setSorterClass(null) for that column.
As we can read on the cached web site, as swinglabs tutorial page appears to be down, I bet it has something to do with the recent mess on the java.net service.
"JXTables have column sorting turned on by default. You can disable all column sorting using setSortingEnabled(boolean allowSort). You can also disable sorting on a single column by using setSorterClass(String sorterClassName) with a null sorter class name."
Personally, I think there is no point to block user from sorting on a selected table column. Anyway if a user wants to sort a column he/she should be able to do so, in the end I believe it is better to allow a user for more then less, of course when it goes to such details as what he/she can control in his/hers view.
I think you should have a look at TableRowSorter API and see whether JXTable supports it like:
TableModel myModel = createMyTableModel();
JTable table = new JTable(myModel);
table.setRowSorter(new TableRowSorter(myModel));
TableRowSorter has an API method isSortable():
public boolean isSortable(int column)
Returns true if the specified column
is sortable; otherwise, false.
Parameters: column - the column to
check sorting for, in terms of the
underlying model
Returns: true if the
column is sortable
Greetings,
I have been understanding how DefaultTableModel will work on my problem.
I had this JTable which already has the data from a query from my data using JDBC - MySQL.
I the role is that the index0 of the JTable always displays the primary key.
And there will always be an empty row at the end two-dimensional object array where the data of the cell will be retrieved.
As of now, I included in the model a setValueAt method which triggers a fireCellUpdated().
It worked but the downside is that the data would display on the JTable but is changed on the array.
How could I resolve this?
Your response is highly appreciated.
Thanks.
When I check the array, the changes were there. But it won't display on the JTable
You should never update the array directly. The array is used to populate the data in the DefaultTableModel when you create the model. The DefaultTableModel stores the data in a Vector of Vectors so all the data is copied from the Array to the model. After that all updates must to done directly to the TableModel.
If I understand correctly, your data model contains additional information that's not intended for display. DefaultTableModel may be a little awkward for this. In the alternative, your might want to consider AbstractTableModel, which should make it easier to expose only certain portions of the data to your JTable. There are examples in the article How to Use Tables: Creating a Table Model and here.
using a jtable -
Can I get the object behind the row
The data displayed in a JTable should be backed by a TableModel, in general a class mapping from a set of business objects to a "table-like" display.
Have a look at the Swing tutorial.
If you tell us more about your usage scenario, one might come up with a good pattern...
As of now, #ChrisJ has the best tip.... YOu can return the POJO in one of the getValueAt columns...