JTable with AbstractTableModel does not edit on click - java

I am constructing a JTable for an Applet that should be able to handle user edits. As such, I have extended the AbstractTableModel and have successfully populated the table with data. My problem is that once the data has been populated, clicking on the table does not allow for edits.
I have overridden the isCellEditable() method to always return true, as well as print a message to the console every time the method is invoked. However, when I interact with the table (through any number of consecutive mouse clicks on any given single cell), the cell does not become editable, and isCellEditable() never gets called either.
My question is, what needs to be called in order to edit a particular cell? I apologize for the lack of code in the post, but the code is highly proprietary, and my superiors are very strict on releasing any code.

To protect your superiors' interests, edit your question to include an sscce that exhibits the problem you describe. Several example suitable for a starting point may be found in How to Use Tables, and this example illustrates an editable AbstractTableModel. You might compare it with your implementation.

1.are you added AbstractTableModel to the JTable already visible on the screen
2.if yes then codes lines isn't isCellEditable(), but should be
#Override
public boolean isCellEditable(int row, int column) {
return true;
}
3.I'd suggest to use DefaultTableModel rather than to override required methods for AbstractTableModel

Related

How to disable cell editing of a Jtable displayed from a result set

Hi guys i am having issues trying to disable editing for a table after displaying it. i would have used setEnabled but i still want the table to be clickable because i am displaying and editing the content of it's rows with the help of text fields.
i have searched and got hints that i have to override isCellEditable() or use DefaultTableModel. However, the major problem now is that my table is displayed using rs2xml because i am actually loading contents of a database table into the JTable. here is the segment of my code that displays the table from a result set:
do {
//get the table...
attendanceTable.setModel(DbUtils.resultSetToTableModel(
} while (rs2.next());
rs2 is my result set.
i tried using default table model... i tried something like this:
do {
DefaultTableModel myTable = (DefaultTableModel)attendanceTable.getModel();
myTable.setModel(DbUtils.resultSetToTableModel(rs2));
} while (rs2.next());
but gave me errors because there was no setModel method under defaultTableModel. that was what i understood by using defaultTableModel...
about overriding isCellEditable(), someone that asked a similar question (but without displaying table with rs2xml) mentioned that doing that also made it impossible for his program to edit the table.
please guys i really need help with this...thanks in advance
i have searched and got hints that i have to override isCellEditable()
Good advice.
However, the major problem now is that my table is displayed using rs2xml because i am actually loading contents of a database table into the JTable
Why is that a problem? You can override the isCellEditable(...) method of the JTable.
Also, why does you code have a do...while loop? You only create one TableModel for the JTable. The DBUtils code will do the looping to read all the data from the ResultSet and create the TableModel.
...trying to disable editing for a table after displaying it. i would have used setEnabled but i still want the table to be clickable because i am displaying and editing the content of it's rows with the help of text fields.
Overriding isCellEditable() should not disable clicking on a cell or selecting a cell. It just prevents the cell from being edited.
i have searched and got hints that i have to override isCellEditable() or use DefaultTableModel. However, the major problem now is that my table is displayed using rs2xml because i am actually loading contents of a database table into the JTable.
This should have no bearing on whether or not you can override and disable editing.
here is the segment of my code that displays the table from a result set:...
Snippets don't help much. Post a minimal code example program please, one without need of a database.
i tried using default table model... i tried something like this:
This code makes no sense to me, mainly because you're creating a DefaultTableModel object and then promptly ignoring it -- why? Then you're using a class, DbUtils, that we have no knowledge of and so can't help you with.
about overriding isCellEditable(), someone that asked a similar question (but without displaying table with rs2xml) mentioned that doing that also made it impossible for his program to edit the table.
That's about all the help I can give other than to direct you to the tutorials and ask for more and better information and code.

Java swing toggle button to filter jtable rows

I have a JTable, it contains a custom AbstractTableModel that return an object when getValueAt is called. And of course I have a custom TableCellRenderer that is capable of getting/constructing the text from the object so that it can be displayed.
However now I would like to write a filter. Filter will be a simple toggle button. When it is turned on I would like the filter to be applied and when it is turned off I would like filter to be removed.
I have two problems due to that.
First one is that I have absolutely no idea how to write a filter when you have to filter by object rather than a primitive.
Second is I have no idea how to attached the said filter to the toggle button to be able to turn it on and off.
I am sorry if this is a retarded question but http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting was the most useless documentation I saw since explanation was not in depth.
Thanks to anyone for their help.
EDIT:
The object contains multiple fields, but I am interested in two filter toggles specifically. One returns a boolean value when I say isSuper, and the second return a string when I call getName. If first toggle is turned on it should show all entries that return true on isSuper, and the second toggle should show all entries where name is compromised of two words (space is present) when I call getName.
To be honest, the JavaDocs spell it out quite well...
With such little information to go on, the best you can hope for is an overview...
TableRowSorter<TableModel> trs = new TableRowSorter<TableModel>();
table.setRowSorter(trs);
// Create the row filder...
trs.setRowFilter(new RowFilter<TableModel, Integer>() {
#Override
public boolean include(RowFilter.Entry<? extends TableModel, ? extends Integer> entry) {
boolean include = false;
// Returns the value for the specific column...
CustomObject value = (CustomObject)entry.getValue(filterColumn);
if (filterBySuper) {
include = value.isSuper();
} else {
include = value.getName().startsWith(fistPart) && value.getName().endWith(lastPart);
}
return include;
}
});
When you want to update the filter, you simply need to call...
trs.sort();
First one is that I have absolutely no idea how to write a filter when you have to filter by object rather than a primitive.
Did you read the RowFilter API? It shows an example of how to create a custom filter based on a custom Object.
Second is I have no idea how to attached the said filter to the toggle button to be able to turn it on and off.
Did you read the tutorial and try running the demo? The tutorial uses a DocumentFilter to change the filter dynamically every time the user changes the text in the text field. So the tutorial shows you how to dynamically changes the filter based on user input. You can modify the code to change the filter every time the toggle button is pressed.
it contains a custom AbstractTableModel that return an object when getValueAt is called. And of course I have a custom TableCellRenderer that is capable of getting/constructing the text from the object so that it can be displayed.
Unrelated to my answer, but I don't really understand that statement. Are you saying every cell in the model returns a differently object, or does every cell return the same object but you just display a different property of the object for column1, column2, column3 etc? Either way it sounds like a weird design. We can probably suggest something better. Post your SSCCE that demonstrates the problem.

JTable.clearSelection() vs Jtable.getSelectionModel.clearSelection() - When to use what?

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.

fireTableDataChanged has no effect on JTable

I have a problem with updating my JTable in Java Swing.
The datas I want to show changes a few times per second and I look for a efficient way to update the data in the JTable.
I used the method setModel() to update the data, and it works, BUT it has 2 drawbacks:
If the user resize the table columns in the header, then he wil get about 10 exceptions (I think because the model is no longer available because it changes a few times per second)
The information of the length of the resized column (in Pixel) get lost, every time the data (and so also the TableModel) changed.
For the TableModel i use my own model ResultSetTableModel which extends AbstractTableModel.
This ResultSetTableModel has a method setResultSet(ResultSet rs) and overwrites the method getValueAt(x,y)...
As I told if I set a new ResultSet to my ResultSetTableModel and then add it to the JTable by the method setModel(resultSetTableModel) it works, but it has the 2 drawbacks i told.
So I think I can solve this problem with the method fireTableDataChanged() but I tried many possibilities but get no change.
Do you know, where I have to place the fireevent?
At the moment I try this, but it doesn't work and I don't know why:
private ResultSetTableModel resultSetTableModel;
private DataFetcher dataFetcher;
private JTable table;
...
//works fine
public void initaialUpdateTable() {
resultSetTableModel = new CachingResultSetTableModel(dataFetcher.getRS());
table.setModel(resultSetTableModel);
}
//does not work
public void updateTable(){
resultSetTableModel.setResultSet(dataFetcher.getRS());
resultSetTableModel.fireTableDataChanged();
}
If I every times call initaialUpdateTable(), it works fine, but i want that just the data changes and not the whole model
Thanks for your answers
Michael
but i want that just the data changes and not the whole model
Hmm how can I..., there is no only one ...
1) Something that you can see in the GUI is TableView, only presentation layer, and all data are always stored in the TableModel
2) If you don't declare any TableModel, this doesn't mean that there isn't exist, still are there DefaultTableModel
3) Your private ResultSetTableModel resultSetTableModel; must extend AbstractTableModel,
4) If you'll to block any of fireXxxXxxChanged();, then no changes goes back to the TableView,
5) Basic stuff here, start with fireTableCellUpdated(row, col);
EDIT
More informations about TableModels here, here or search for ResultSetTableModel, TableFromDatabase
Sorry I don't have a concrete answer to your question, but I couldn't quite fit all that I want to say in a comment.
I used the method setModel() to update the data
You should probably stick to a single model that provides methods to modify its data. These methods should appropriately notify listeners when something has changed.
Here's a really awesome article that shows how to implement a high-performance, multi-threaded table with frequently changing data. You could probably use a lot of the example source code.
How to Create Frequently-Updated JTables that Perform Well

Working with data and a JTable?

I have a JTable that I want to use to display some data (a String and a Boolean in each row). The data is maintained by my own class. Is there some way to bind the data model to the JTable, so that when I add to the model, the JTable is dynamically updated and when I remove something from the model, the row is removed from the JTable?
I have previously worked with Flex and Actionscript, and this is very easy to do there with data binding, so I'm just wondering how it's done in Java.
Thanks.
You will need to have your dataset implement the TableModel interface. if you do that then you can apply it to the JTable. If you extend AbstractTableModel you will inherit some event firing methods that your table will handle and will update the view. see this tutorial. Note that the default implementation of JTable will renderer your data for you, and if a Boolean is found, it will show up as a check box.
You'll probably find both the Java JTable tutorial and the JTable API documentation helpful in understanding how JTable works, but otherwise here's a quick rundown.
The premise of a JTable is that it is paired with an object that implements the TableModel interface, which by default is an instance of DefaultTableModel. The table model object is made up of a list of columns, each of which has its own data type (String and Boolean in your case), and a list of rows containing the actual data for the table.
Whenever the JTable is drawn by the swing drawing code, it repeatedly calls the method:
public Object getValueAt(int row, int col)
Thus, when you add data to the table model, it is always rendered as you expect in the next screen refresh (dynamically).
The only thing you really need to worry about, then, is getting the data from your object into the table model and back out again. Other than that, JTable takes care off all the heavy lifting.
While implementing TableModel is easy enough for simple cases, you might want to consider a true binding approach (my favorite is Glazed Lists - watch the 30 second video on how easy this is and you'll be won over). Beans Binding (now Better Beans Binding) also has an implementation of observable lists that might be useful (although I much prefer the Glazed Lists approach)

Categories

Resources