I have created a table in JavaFx. I used reflection to populate the table with numbers 1 to 100. This table contains a zone number and a description. There are 100 zones. I want the table to be editable. I have used the following code to make the cells editable.
zonesTable.setEditable(true);
zone.setEditable(true);
zone.setCellFactory(TextFieldTableCell.<Zones>forTableColumn());
description.setEditable(true);
description.setCellFactory(TextFieldTableCell.<Zones>forTableColumn());
zone.setCellValueFactory(new PropertyValueFactory<Zones, String>("rZoneNumber"));
description.setCellValueFactory(new PropertyValueFactory<Zones, String>("rDescription"));
for(int i = 0; i < 100; i++){
data.add(new Zones(i + "", ""));
}
zonesTable.setItems(data);
At the moment, this code adds numbers to the zone column and makes the zone and description column editable. However, after I type a value into the column and click the next row, my values that I input into the table disappear. I have no idea why. What do I need to do to cause my typed values to stay visible in the table after I select a different row than the one I am editing? Thanks in advance!
However, after I type a value into the column and click the next row,
my values that I input into the table disappear.
It doesn't work because there's a severe, embarrassing bug in JavaFX that Oracle refuses to fix.
The solution for you would be to press enter before you click the next row. But of course you can't request that from your users.
You may find a workaround here.
If you want to upvote the fix and comment on the bug, here's the issue.
Not enough code to determine how you work with the table.
You could tableColumn.setOnEditCommit(new CustomEventHandler)
You could tableColumn.setCellFactory(TextFieldTableCell.forTableColumn(new StringConverter() { your logic});
Those still only commit changes in the cell when Enter is pressed. So you will still need your own implementation based on which event you consider to commit edition.
Related
Currently I'm working on a listgrid which is editable and my requirement is to create a a new row every time a user selects a row and presses the "Create" Button. The new row needs to be added immediately below the selected row. I tried using the below method
ListGrid.getRecordList().addAt(ListGridRecord rec, Index index)
However I got the warning message
15:48:04.373:MUP3:WARN:Log:ResultSets are readonly. This operation (addAt) will be ignored.
I've searched the smartgwt showcase to look for ways to edit a grid , so that new row is added at a specified index, however I wasn't able to find anything suitable.
I got to know that ResultSets is getting created because I'm using the statement
ListGrid.fetchData() Is there any way to solve this issue? Any suggestion is highly appreciated!
Muchas Gracias.
Figured it out, quite a weird trick...but works for me...
what you need to do is use grid.setRecords(grid.getRecords()); just before grid.getRecordList().addAt(rec, index); I think doing this makes the ResultSet editable.
How would I resize the column for it to just fit the largest string in that column. For example in one column I might have a word like "Monday" and I want this column to be shorter than the "Wednesday" column.
The data changes often as it shows the next 3 days, so I would want Java to automatically change it depending on the input data rather than me having to manually set the column width.
My JTable is within a JScrollPane if that changes anything
Hope that makes sense. Please point me in the right direction.
Whenever there is a lostFocus inside a JTable, i need to capture the existing cell's row and column.
However, the condition below is always false because the source is always either a JTextField or a JComboBox.
public void focusLost(FocusEvent e) {
int row, col;
Object source = e.getSource();
if(((Component) source).getParent() instanceof JTable_Ext){ //<-- always false
table = (JTable_Ext) ((Component) source).getParent();
row = table.getSelectedRow();
col = table.getSelectedColumn();
}
To mitigate the above, i remember the row and col during FocusGained (as class level variable). The problem is, if the user click very fast all over the place within the JTable, somehow the row and column information will be out of sync.
Is there a way to get the Row and Col during FocusLost? if not, is there a better way of doing this?
Well, there is the oppositeComponent. The weird thing is, if this listener is attached to the table, the documentation tells that the table should be the "source" component (because it is a FocusLost event and the component that lost the focus is the table itself).
http://download.oracle.com/javase/1.4.2/docs/api/java/awt/event/FocusEvent.html#getOppositeComponent%28%29
Could you just keep a record of row & column using
table.getSelectionModel().addListSelectionListener(...);
table.getColumnModel().getSelectionModel().addListSelectionListener(...);
So every time the use clicks update it, not just on focus events?
If you just want to save the data that was entered in the cell (without hitting return), then you don't need to do anything. The updated information is contained within TableModel of the JTable.
Otherwise you can take a look at .tableChanged() and the associated TableModelEvent, which gives you the last row/column modified. You could keep a variable that is always updated to the latest event row/column. I guess that if you change a cell number without hitting return, it nonetheless registers as an event.
However, the condition below is always false because the source is always either a JTextField or a JComboBox.
This implies that the focusLost event is being generated when you begin editing a cell. So the question is why are you doing this? I think you need to state your actual requirement, because you attempted solution does not seem appropriate.
I have JTable and rows in it. By default first row is selected and focus is in it. How can I deselect first row and change focus to somewhere else that .addListener(new RowSetListener() will work in first row too.
I already try:
tableZaposlenciView1.setRowSelectionAllowed(true);
//tableZaposlenciView1.getSelectionModel().clearSelection();
//tableZaposlenciView1.setColumnSelectionInterval(0,0);
//tableZaposlenciView1.setRowSelectionInterval(false,false);
tableZaposlenciView1.changeSelection(0,0,false,false);
tableZaposlenciView1.requestFocus();
but it is not working.
Have a look at JTable's changeSelection() method. I believe it does what you want.
EDIT: If you want to clear the selection:
JTable table = ...;
table.getSelectionModel().clearSelection();
if someone else have similar problem with ADF and JTable here is solution. I achieve that by overriding first() on the VO impl. my problem can be solved.
That comes in handy in many situations:
no selection after refresh (just return null from first())
reselect a particular row after refresh (before refresh, store a row key, after refresh in first() if stored key is found, navigate to that row and return that from first())
find next matching row for user to work on after refresh of a worklist
avoid costly detail executions in a VL situation
The desired behavior is akin to the mirrored text editing field provided in Excel when a given cell is selected, allowing more space to view the contents of the cell. I have a JTable with 5 columns and n rows. Column 2 holds expressions that can be arbitrarily long, thus I'd like to provide a separate JTextField to work with for editing the contents of the expression cell per row. The other fields are directly editable in the table. When the user clicks on a field in column 2, however, I want to send them to the text field. Any contents preexisting in the cell should be appear in the text field and additional edits in the text field should be mirrored in the table cell. Likewise, if someone double-clicks on the cell and edits it directly, I want those changes reflected in the text field. Thus, the user can choose to edit in either space and both are updated. Ideally, they are updated per keystroke, but update upon hitting return is acceptable.
So, far I've got the JTable, TableModel, TableModelListener, JTextField, ListSelectionListener, and AbstractAction, working together to provide most of the functionality described above. I'm missing the reflection of direct table cell edits to the text field and per-keystoke updates.
Are their ideas on how best to construct this behavior?
Well, if you want to get data from the table to the cell then you add the code to your TableModel's setValueAt() function, which should run when the user changes the content in an editable cell. I don't think that will update per-keystroke though.
If you want to move data from the textbox to the table cell use code like this
myJTextField.getDocument().addDocumentListener(new MyDocumentListener());
Where MyDocumentListener is an implementation of the javax.swing.event.DocumentListener interface
That will get you per-keystroke updates from the box to the table. But for the other way around it's a bit trickier.
There are two ways you might be able to go about doing it
1) Add a key listener to the table, and when the user starts typing check to see what table element is active, and intercept keystrokes as they type. That's kind of messy, though.
2) Another option might be to try to grab or replace the component that the table is using to actually let the user make the changes. I think that JTable actually allows you to change the editor component if you dig around.