• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Tableview show combobox on edit  RSS feed

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a small program which takes data from textfields and comboboxes and inserts into a database and to a textview. But I want to be able to select any row in the tableview and be able change data using a combobox (same values as the combobox who inserted the data in the first place). And if someone have tips on  how to improve my code I would also be glad.

This is my code:

 
Marshal
Posts: 5988
156
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the late reply.  If you're still interested, here is a (not so) little demo of how to do something when a combobox is changed.  The guts of the matter are lines 61 through 75.  You set up your combobox as normal, then add an onEditCommit() callback.  The parameter is an EventHandler that you can get any information you need to do an update.  (I, perhaps foolishly, used a Map as the TableView's item type.  When you use e.getRowValue(), it will return a Supplier, not a Map.)

I would also suggest that the initialization() should be only for init code and that the start() method be short.
 
Knute Snortum
Marshal
Posts: 5988
156
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, here it is without the Map table obfuscation.  The Supplier class is the same.
 
Cliff Karlsson
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!, I will try to adapt my code with this tomorrow. But if possible I have another question. In my latest attempts I tried adding a "Edit" -column when I started editing a row. I managed to get a new column to appear when I started the edit. But I really would want a two buttons to appear in this newly created cell (Save and abort -button). And If I click save it saves the current row with updated values at the same place and if I click abort it discards all changes.

This is what I tried (before your last posts):

 
Knute Snortum
Marshal
Posts: 5988
156
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you can get two Nodes inside one TableColumn.  What about a Combobox and the two choices Save and Cancel?
 
Cliff Karlsson
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, but I really would like two buttons. Could it be possible to create two columns with no header-text with one button in each column? or maybe one column named "edit" which have two nested columns with one button in each?
 
Knute Snortum
Marshal
Posts: 5988
156
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, if I had to have two buttons, I would create two extra columns with the buttons in them, but disabled until an edit is made.
 
Cliff Karlsson
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks for all the tips. But my biggest problem right now is to figure out exactly how I add a button to a column. Is it possible to get an example of how to do that?
 
Knute Snortum
Marshal
Posts: 5988
156
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately, adding anything other than editing with a combobox, choicebox, checkbox, or textfield is a bit complex.  Here is my addTableCellButton() method:
First you create your TableColumn as always.  Then you have to create your own cell factory callback  "by hand."  You override the call() method of the callback with two lines: one creating a cell and one returning it.  The cell has two lines: one creating the button you will use in the TableCell, and a method overriding the cell's updateItem().  This is where the action is. The code has to follow the pattern shown here strictly or the cell won't update correctly.  It's in the button's onAction() event that you put your own updating code in.  Here I have just grabbed the supplier and printed the ID and name.
 
Cliff Karlsson
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, it feels like I get new questions after every answer.  How would I proceed to only add a button to the current row?

I figured out that I can get the row-number from this:

Is there any better way? and how can I use this to only add the button to the current row?
 
Knute Snortum
Marshal
Posts: 5988
156
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, to try to answer your question, I would first set the SelectionModel to single:
Then I would create two extra columns for the buttons (you can't add and delete table columns for one row).  Now the tricky part that I don't know how to do (yet).  I would create some sort of listener that fires when you leave one row and go to another.

I'm not sure the above will work, so I'll show you a different way in the next post.
 
Knute Snortum
Marshal
Posts: 5988
156
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another way to get what you're trying to do.  Have the TableView be non-editable and single row selectable.  Then as you go from row to row, have the contents of that row display in a form you create.  This form will watch for the onAction event of its buttons and save and cancel as needed.

I know this would be a big change in your application, but right now you're fighting against the way a table is used in JavaFX (and other places).  Just a thought.
DepositEntry.PNG
[Thumbnail for DepositEntry.PNG]
Using a combination of a TableView and a form
 
Knute Snortum
Marshal
Posts: 5988
156
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I wrote:I would create some sort of listener that fires when you leave one row and go to another.


I knew there was a way to do this.  You would get the selected item property from the tableview and add a listener to it:
 
Cliff Karlsson
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been trying to fix my last problems with your posts as help but still have a couple of problems I can't figure out.


How do I make the buttons disappear when I change row without pressing save first?
How do I "reset" the row to its original value if I change one or many fields and then change row without pressing save?

Right now I have only set up listeners for the two comboboxes in coulumns ("Leverantör(columnSupplier)", "Product(columnRadiopharmaceutical)"). Do I need to add listeners for every other field and adding the "addTableCellButton()" to every listener method as well ?






 
Knute Snortum
Marshal
Posts: 5988
156
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, I'm impressed that you got two buttons to work in one table cell!

Back to your question.  The listener I was suggesting is on the table, not on the cell.
 
Cliff Karlsson
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am pretty sure I managed to add the listener to the table:


But I must admit the code is quite messy, I got a little wild trying to find different listeners/methods. But did you mean that I could remove the buttons from that listener? I tried creating instance variables of the buttons/Hbox and then tried .setVisible(false) on them but with no progress.  
 
Cliff Karlsson
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh, I think I understand what you meant. Placing the "addTableCellButton()" in the table-listener fixed the button issue. Now I only have to understand how to "reset" a row.
 
Cliff Karlsson
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I spoke to soon, I managed to get the buttons to disappear when mouse is clicked outside the table by using a focusPropertyListener and checking for "lost focus". But now my tableviewComboboxes doesent work. They only generate some css error when clicked on. Any tips?

 
Knute Snortum
Marshal
Posts: 5988
156
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But now my tableviewComboboxes doesent work. They only generate some css error when clicked on.


When you write something like, "It gets an error," remember to post the full error message and stack trace is there is one.  
 
Knute Snortum
Marshal
Posts: 5988
156
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why did you change this from using the selectedItemProperty?
 
Cliff Karlsson
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did not change this, I still use the selectedItemProperty() listener to add the buttons but the problem is that when I click outside the tableview the buttons are still visible on the last row I clicked. I would like them to disappear in that case.

I tried adding the addButtons method to the focus listener so when out of focus it would catch the first if in the addButtons:


and the buttons would be removed. But it did not work as expected.
 
Knute Snortum
Marshal
Posts: 5988
156
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still use the selectedItemProperty to add the buttons.  If you want them to clear if you window losses focus (which I don't think is standard practice), add a ChangeListener to the stage:
 
Paper jam tastes about as you would expect. Try some on this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!