Dynamically changing jTable with HashMap - java

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

Related

Is a custom TableCellEditor constructor only called once?

I have a JTable. One column in the JTable is assigned an extended TableCellEditor that displays an extended JComboBox.
There is a fixed list of 100 String objects that populates the comboboxes.
The challenge:
Design the JComboBoxes so that any selection is unique relative to other boxes? That is, if "A" is slected from the combobox in the first row, it is automatically removed from the list of each other combobox.
When a new room is added to the table, the combobox it contains should auto-populate to the first available list item.
The problem:
My comboboxes work beautifully. I can select items at will. I even have made some progress in eliminating already used items from the lists. But I can't figure out how to correctly auto-populate.
I am very confused because it appears that my combobox constructor is only called once when the table is created, not once for each row.
Is this the case? Is the constructor for a TableCellEditor only called once ever? If so, how do I modify the behavior of each combobox as it come into existence?
Thanks for your help!
If you would like specific code, please let me know. I don't know if you want me to paste in the whole classes.
When a new room is added to the table, the combobox it contains should auto-populate to the first available list item.
When you add a new row of data to the TableModel you must add the values of all columns in the row. This should not be a function of the editor. The editor allows you to change values in the cell.
I was able to get around my problem by creating an abstract superclass for my combobox that can be accessed from the tablemodel extension when it sets up its data.

JTable Data Refresh Issue

I want to refresh the JTable data by clicking a button.
The problem is that the old data in the JTable can't be removed and the new data are just added into the table. I tried below ways to remove the old data but none of them works.
1. table.setModel(new DefaultTableModel());
2. ((DefaultTableModel)table.getModel()).setRowCount(0);
3. ((DefaultTableModel)table.getModel()).fireTableDataChanged();
4. ((DefaultTableModel)table.getModel()).getDataVector().removeAllElements();
5. table.repaint();
6. model = (DefaultTableModel)table.getModel();
while(model.getRowCount() > 0) {
model.removeRow(0);
}
Having a refresh button for a JTable is very suspect. It makes me think you aren't correctly adding data as JTables should refresh everytime data is added or removed.
I would verify a couple of things when using a DefaultTableModel:
Make sure to only add data using addRow
Data should only be inserted using insertRow
Remove data using removeRow
Never modify the internal vectors directly. It won't cause events to fire and you're stuck with a refresh button. I don't know why they even expose it. The JavaDocs should at least specifically warn against this.
If all else fails, fire up a debugger and see what happens.
More of your code might be appropriate here. Hard to tell exactly where you're calling these methods and the order. If you change the model and then call fireTableDataChanged() it should work....assuming you've updated the right TableModel. There is a good Java tutorial for using tables: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html
Had the same issue myself, but my solution was different. After checking just about everything, I checked the contents of the table via the console, and found that the contents were indeed being updated. However, the update was not being reflected on the table which was visible on the screen.
In fact, this code:
model = (DefaultTableModel)table.getModel();
while(model.getRowCount() > 0) {
model.removeRow(0);
}
Did not only remove the rows, but also removed the table.
My solution was to remove the table from the form and then re-add it, whenever the table data was changed.
Seemed in my case there was nothing wrong with my coding to generate the table but that the layout manager didn't like overwriting or updating a component the area where I wanted to put it already had something in there.
Something weird going on methinks but at the end of the day this worked for me.

restore jTable focus and position after removing a row from table model

one question with the java jTable class. Actually I am not a Java programmer and just now using Java to design a GUI in Matlab. What I've done is:
A jTable is built into a Matlab GUI.
I used/called a RowFilter in jTable, which can make the jTable to show the filtering results.
Then from the results in this filtered view I used the removeRow method from table model to remove one or several selected rows.
The problem is that everytime if I remove a row, the table content refreshs itself as wanted, but the scroll bar jumps back to the beginning.
Does anyone know how to inhibit this jumping and keep the original view of jTable? Because this helps me not to have to scroll back to find the original position where I started the deleting.
Thank u for ur advice and help.
Invoke the table's scrollRectToVisible() method; pass it the Rectangle returned by getCellRect() for the desired row.

Problem in giving input to JTable from keyboard

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.

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