I want to know how to give input to the cell in a jtable from keyboard.
And when I try to do this when I move on to next cell the previous enter data is removed or erased automatically.
I am using abstracttable model for creating jtable.
iam using abstracttable model for creating jtable....
and when i try to do dis when i move on to next cell the previous enter data is removed or erased automatically....
The AbstractTableModel doesn't implement the setValueAt(...) method. So unless your custom model implements this correctly you will lose the data entered in the editor.
I suggest you keep it simple and use the DefaultTableModel until you better understand how a JTable works. The code would be:
DefaultTableModel model = new DefaultTableModel(...);
JTable table = new JTable( model );
As discussed in How to Use Tables, you can specify a renderer and editor for each column in a JTable, or you can override getColumnClass() to obtain the default for any of the listed data types. In addition, you might compare what you're doing with one of the examples listed there or edit your question to include an sscce.
Sounds like you are not saving the data entered into your model. Secondly, I would suggest extending DefaultTableModel instead of AbstractTableModel unless you have a good reason.
Related
I didn't write any code yet, so sorry about that but need some direction and clarification before proceeding. Can I create a dynamic link between with a JTable and Hashmap? So when ever my listenrs add something new or delete something, it will update the Hashmap and that would update the JTable, I was thinking of re-creating the JTable everytime a change happens? That is one of my buttons being pressed.
Any suggestions?
A JTable is just the visible component, the data is contained in a TableModel. Any changes inside the model will be reflected in the JTable itself. So you can create a TableModel that uses a HashMap internally.
Here's Oracle's table tutorial for more info
https://docs.oracle.com/javase/tutorial/uiswing/components/table.html
so the scenario is that I've got a JTable with a number of JComboBox's as cells. On the selection of an element of a JComboBox, there needs to be a change in the structure of the Table Model. I've also got an 'output table' below which listens to the selection of the JComboBox's and re-validates accordingly, because of this, I need to keep the model of the query table the same so that it can reuse the listener. How can I change the structure of the Table Model?
DefaultTableModel QueryTableModel = new DefaultTableModel(dropDownUserSelection, resultsListHeadings );
queryTable.setModel(QueryTableModel);
JComboBox box = new JComboBox(boxModel);
queryTable.getColumnModel().getColumn(i).setCellEditor(new DefaultCellEditor(box));
I apologize if I am asking a question which has already been asked elsewhere, but I've had a poke around and couldn't find what I was looking for.
Thanks
The TableModel has the responsibility for notifying the parent table (or anybody listening) of changes to the model.
The general events available are data changed, cell updated, row inserted/removed and structure changed.
The "structure changed" tells the parent table that the structure of the table model (the number of columns and/or column names and/or types has changed) and it should completely update itself.
There are a number of ways you could achieve this. You could have the existing table model change it self accordingly and fire a "structure changed" event or you could construct a new table model and apply it to the JTable, depending on your needs.
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
java JTable, Say I have a huge JTable (800*50) with AbstractTableModel. Now I want to remove all table rows and put new data rows into that table. Which way is easiest and high-performance way to achieve this ?
Thanks.
The AbstractTableMoeel doesn't support this. If you extend the AbstractTableModel to create a custom model then you need to implement this method yourself.
Or you can use the DefaultTableModel which implements a setRowCount() method. So you can reset the rows to 0. You can then use the insertRow(...) method to add new rows.
However the easier way is to probably just create a new TableModel. Then you can refresh the table by using:
table.setModel( newlyCreatedModel );
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)