Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    javamarc is offline Member
    Join Date
    Nov 2016
    Posts
    13
    Rep Power
    0

    Default Explain callback to me please (and this code)!

    Hi, Im struggling to understand the code here that im using to make a dynamic table (as based on this tutorial javafx - How to add dynamic columns and rows to TableView in java fxml - Stack Overflow)

    Ive edited it back to an anonymous inner class to try to understand it, but im still not sure what is happening. I've googled callback many times, and im shakily happy with my interpretation of something that happens when something else finishes happening! but can someone explain every bit of this code to me and how it fits in with the definition of a callback? Thanks

    Java Code:
    column.setCellValueFactory(
                new Callback<TableColumn.CellDataFeatures<ObservableList<String>, String>, ObservableValue<String>>() {
                @Override
                public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList<String>, String> cell) {
                return new ReadOnlyObjectWrapper<>(cell.getValue().get(finalIdx));
                    }
                });

  2. #2
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    6

    Default Re: Explain callback to me please (and this code)!

    There are a few ways to create a callback pattern. JavaFX 8 introduced the Callback Interface as another way. It is very generic and more a specialty imho. The first parameter defines the type of object that is passed into the method, the second parameter defines the return type of the method.
    I can't read the full code here on my limited screen, but it looks like this is exactly what's happening here.
    I found some examples here
    Last edited by benji2505; 11-28-2016 at 04:07 AM.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Explain callback to me please (and this code)!

    A call back is nothing more than providing a method in your code that another class or method calls.
    Usually, the calling class is passed the method to call as an argument. This is the basis for listening for and
    handling events in Java.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    javamarc is offline Member
    Join Date
    Nov 2016
    Posts
    13
    Rep Power
    0

    Default Re: Explain callback to me please (and this code)!

    Thankyou.. I have been up half the night studying callbacks in many languages. I seem to understand them all but not really in Java!

    Im new to TableView, and cant really find any comprehensive information about the underlying functioning of the table except Oracles horrendous class references that are spread out over 100 links and doesnt really explain anything. Can anyone explain what is the model for a tableView?

    So the code I posted above. I need to go through it chunk by chunk.

    Java Code:
    column.setCellValueFactory(
    - Am I telling this column where to find the right data for this particular column, and this will update automatically if the underlying data changes?


    Java Code:
    new Callback<TableColumn.CellDataFeatures<ObservableList<String>, String>, ObservableValue<String>>()
    - So I creating an instance of a callback, the sent object is of type
    Java Code:
    <TableColumn.CellDataFeatures<ObservableList<String>, String>
    and the return type is of type
    Java Code:
    ObservableValue<String>>()
    Java Code:
    <ObservableList<String>, String>
    -I dont understand this type...is it an observable list of type Sting, but then another String after the comma, what does this mean?

    Java Code:
     {  @Override
    - I understand this is known as an anonymous inner class, but of what? Is it the interface called Callback?

    Java Code:
    public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList<String>, String> cell)
    - So am I implementing a method named 'call' of an interface that returns an ObservableValue<String> and is passed a parameter
    Java Code:
    TableColumn.CellDataFeatures<ObservableList<String>, String>
    and is referenced by the variable 'cell'.

    Java Code:
    return new ReadOnlyObjectWrapper<>(cell.getValue().get(finalIdx));
    - So I create a new object and return it as a ReadOnlyObjectWrapper<> ----but I thought we needed a
    Java Code:
    ObservableValue<String>>()
    return type??

    which gets its value from
    Java Code:
    (cell.getValue().get(finalIdx))
    - what value are we calling up here and why?

    Thanks. I hate being a noob
    Last edited by javamarc; 11-28-2016 at 11:16 AM.

  5. #5
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    6

    Default Re: Explain callback to me please (and this code)!

    The underlying challenge in the code is that the collections are Observable Collections, for example an ObservableList. The advantage of these observable collections is that they update the table dynamically with every change in the underlying data (which saves a lot of drama).
    All the mojo of an observable collection is in the valueFactory behind the table cells. The disadvantage is that you have to set some parameters for the valueFactory and the returned objects. The returned object inside the callback determines the type that you want to display.
    Overall it is less complicated than it looks. Most of the code is ctrl-space code. I would suggest to let a youtube video walk you through the process. I quickly grabbed:
    Video1
    Video2
    Last edited by benji2505; 11-29-2016 at 03:50 AM.

  6. #6
    javamarc is offline Member
    Join Date
    Nov 2016
    Posts
    13
    Rep Power
    0

    Default Re: Explain callback to me please (and this code)!

    Quote Originally Posted by benji2505 View Post
    The underlying challenge in the code is that the collections are Observable Collections, for example an ObservableList. The advantage of these observable collections is that they update the table dynamically with every change in the underlying data (which saves a lot of drama).
    All the mojo of an observable collection is in the valueFactory behind the table cells. The disadvantage is that you have to set some parameters for the valueFactory and the returned objects. The returned object inside the callback determines the type that you want to display.
    Overall it is less complicated than it looks. Most of the code is ctrl-space code. I would suggest to let a youtube video walk you through the process. I quickly grabbed:
    Video1
    Video2
    thanks.

    I have watched many videos and sought out information already.

    The issue is, is that im trying to develop a TableView that I can pass any database too not knowing its schema or having a java class for it, and it knows how to display it. I dont have a problem with the instructions as per the videos you gave (that ive already viewed hundreds from both the boston guy and Derek Banas, both fantastic tutors) but these all work around creating classes for the data, which I can't do if I want to display *any* database.

    I dont really care about Observable Lists, although i'd like to understand them, as im planning on manipulating the database through SQL, not the Observable list. Unless there is a better way?

    I dont really get why displaying a dumb table has to be so involved or fixed down in code via the classes model. I just want to display a dumb table, double click on a row and have an edit database window pop up to make the changes via SQL. I would have thought this is very straightforward no?

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Explain callback to me please (and this code)!

    So essentially this:
    https://docs.oracle.com/javase/tutor...jdbcswing.html
    ?

    That covers the display of the table. You'll want to modify how the pop ups work, as they're set up against a specific table I think.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    javamarc is offline Member
    Join Date
    Nov 2016
    Posts
    13
    Rep Power
    0

    Default Re: Explain callback to me please (and this code)!

    maybe, but in fxml not swing

  9. #9
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    6

    Default Re: Explain callback to me please (and this code)!

    If you know/access the sql table data, you already know all the data types, correct? So, why worry about all the different callback patterns and the generics? I think the critical step here is more in the DAO. I don't think the obstacle will be in the GUI.
    We have had a very similar post in the javafx subsection on this forum, you might be able get a good idea there.
    JavaFx post
    Last edited by benji2505; 11-30-2016 at 12:44 PM.

  10. #10
    javamarc is offline Member
    Join Date
    Nov 2016
    Posts
    13
    Rep Power
    0

    Default Re: Explain callback to me please (and this code)!

    sorry, some of the terminology is new to me, im only 6 weeks into my java programming...by DAO do you mean Data Access Object?

    This is what im specifically trying to avoid I think, I didnt want anything concrete, because I am playing around with my test databases and tables, and dont want to have to program a DAO class every time I change something.

    Anyway, I have managed to create a TableView that displays anything I send it from any database and any Sql statement, so im pretty happy. But I would like to better understand the mechanics behind tableView, as I would like to have checkboxes for boolean values and treeviews for grouped results.....

  11. #11
    javamarc is offline Member
    Join Date
    Nov 2016
    Posts
    13
    Rep Power
    0

    Default Re: Explain callback to me please (and this code)!

    im also having problems posting to this thread when I am logged in, although I can post elsewhere,..why is this?

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Explain callback to me please (and this code)!

    Well, I don't do DB's but from what I have read, it seems that if you know the data types in your DB then you
    could write helper routines to convert them to String equivalents and display them in your table. Of course, I don't
    know how many possible data types there are so it may not be feasible.

    Regards,
    Jim
    Last edited by jim829; 11-30-2016 at 05:53 PM. Reason: grammar
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #13
    javamarc is offline Member
    Join Date
    Nov 2016
    Posts
    13
    Rep Power
    0

    Default Re: Explain callback to me please (and this code)!

    Java Code:
     column.setCellValueFactory((cell) -> new ReadOnlyObjectWrapper<>(cell.getValue().get(finalIdx)));
    Im guessing this line in my code is what sets the type of cell. Should I look into changing this line if I want to create a checkbox cell?

  14. #14
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    6

    Default Re: Explain callback to me please (and this code)!

    Jim is right.
    The DAO is the interface of the Java world to the database world. It manages the connection and it handles all the read, add and edit functionality (both ways). That DAO will not get to work if you don't declare the type that you want to read/edit for each column. If you have that type, you can use it to declare the data type in the GUI. That's what I tried to say.
    I would focus on the DAO for now.

  15. #15
    javamarc is offline Member
    Join Date
    Nov 2016
    Posts
    13
    Rep Power
    0

    Default Re: Explain callback to me please (and this code)!

    What do you mean by DAO?

    Are you referring to the classes that are created that mirror the data structures, with the get, set and property methods?

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Explain callback to me please (and this code)!

    OK, a quick reread of the JavaFX docs to remind myself of what the Table expects.

    Since you want a generic model then your model is pretty much going to be something like a DataRow which holds a Map<String, Object>, where the String maps to the column name (as returned by the ResultSetMetaData) and the Object is the data itself.

    Your DAO will return a List<DataRow>. Actually, you probably want a ResultModel, which holds the List<DataRow> and the List<String> which is the column names. That way you know you'll have the column names if there are no results.

    Building your table, you can then loop over the column names to create the TableColumn, and for each one assign the cell value factory.
    That would probably look like:

    Java Code:
    ... this is inside the column creation loop
    ... we have the column name (final String columnName)
     currentColumn.setCellValueFactory(new Callback<CellDataFeatures<RowData, String>, ObservableValue<String>>() {
         public ObservableValue<String> call(CellDataFeatures<RowData, String> p) {
             return p.getValue().getData(columnName);
         }
      });
     }
    I haven't tested any of this, but this is the route you probably need to take.

    ETA: Obviously this is all Strings. You'll need some extra work to handle the datatype.
    This is just a start point.

    Get this working first.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  17. #17
    javamarc is offline Member
    Join Date
    Nov 2016
    Posts
    13
    Rep Power
    0

    Default Re: Explain callback to me please (and this code)!

    It is working very well with strings, thanks.

    Now I need to work out how to add something like a checkbox in one column to represent boolean values in the db.

    I think i've *almost* worked out the callback implementation by writing the whole thing out on paper and scribbling notes all over it, following the logic through the innerclass.

    What is a cellValueFactory? is it just the data in the cell?

  18. #18
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    6

    Default Re: Explain callback to me please (and this code)!

    OK, I edited this because Tolls gave the much better answer. Check this for CheckBoxTableCell.
    Last edited by benji2505; 11-30-2016 at 07:57 PM.

  19. #19
    javamarc is offline Member
    Join Date
    Nov 2016
    Posts
    13
    Rep Power
    0

    Default Re: Explain callback to me please (and this code)!

    A really dumb question.

    Is it possible to dynamically create a class at runtime based on the database structure that gives me all the getters and setters for using the TableView in the same way as the tutorials show?

  20. #20
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Explain callback to me please (and this code)!

    In theory, yes. At least this is how I would envision it.

    1. Create a java file that has the required methods, etc.
    2. Execute the compiler to create the class file.
    3. Load the class file.
    4. Use reflection to do what you need to do.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Page 1 of 2 12 LastLast

Similar Threads

  1. Can Anyone Explain To Me This Code?
    By Akirien in forum New To Java
    Replies: 2
    Last Post: 11-05-2012, 03:34 PM
  2. Can someone explain this code?
    By Akirien in forum New To Java
    Replies: 3
    Last Post: 02-26-2012, 11:52 PM
  3. Anyone could explain the Code.....
    By seongchog in forum New To Java
    Replies: 2
    Last Post: 09-22-2011, 08:38 PM
  4. Any one help me fix JAVA code and explain ?
    By batista11b5 in forum New To Java
    Replies: 4
    Last Post: 10-23-2010, 06:19 PM
  5. need to explain this code
    By reached in forum New To Java
    Replies: 3
    Last Post: 12-03-2007, 11:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •