Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Populating TableView from a method

 
S Fox
Greenhorn
Posts: 18
1
Eclipse IDE Firefox Browser Netscape
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone, I'm new to java and trying to learn GUI programming in JavaFX. I got stuck on something, I cannot figure out how to populate a TableView! I have a 2 dimensional array and want to fill up the table with it.
I need to somehow convert the 2d array into an ObservableList or ObservableArray, but after looking at many examples I still can't figure this out.

Most examples only show how to populate the table by manual input from the user, but I need to generate my data with a method and fill it.
I definitely don't want to create a different setter method per table column, I want to easily change the table size if needed: 3x3, 6x6, 20x20, etc.
Also, every object in my table will have the same properties, imagine something like this:

Red | Green | Blue
-----------------------
jeep | camaro | corvette
accord | camry | mustang


I think I'm close to understanding how this works, so please if anyone can help it's much appreciated. The many examples I have found so far on the internet are far too simplistic to be of any actual use to me.
 
Steffe Wilson
Ranch Hand
Posts: 165
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome aboard.

That's quite a hard problem to set yourself as someone new to java, but I'm guessing you have previous programming experience in other languages.

ObservableList provides an addAll() method which I would think to be the way to go, so you would need to copy or re-implement your 2D array into an ArrayList so it can be passed into observerList.addAll().

https://docs.oracle.com/javase/8/javafx/api/javafx/collections/ObservableList.html#addAll-E...-
 
John Damien Smith
Ranch Hand
Posts: 299
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to:
  • Load the data from your 2D array up into an ObservableList.
  • Set the ObservableList as the items for your TableView.
  • Define an appropriate cellValueFactory which extracts the corresponding value for a cell in the table for a given table column.

  • When you see the solution coded up it is perhaps simpler than it seems (or perhaps not :-)
     
    S Fox
    Greenhorn
    Posts: 18
    1
    Eclipse IDE Firefox Browser Netscape
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for the example John! I'm coming from C and java is a bit confusing to me. I'm about halfway through the intro to java book by Daniel Liang and I still don't feel like I've learned very much at all. I'm taking a class but its moving too slow so I'm doing my own projects. I haven't learned yet why some functions have angle brackets, or what generics and collections and factories are. I'm not sure how much of this is java and how much is the javaFX api.

    There seems to be 2 schools of thought, one is that swing is dead, don't even bother learning it and just learn javaFX. The other is that javaFX is the dead one, because scene builder is no longer maintained, and etc... what's your take on this? I'm using eclipse but I'm about to install efx eclipse, wanted to see if the unsupported scenebuilder is in there.
     
    Steffe Wilson
    Ranch Hand
    Posts: 165
    12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think that Java is beguiling to the C programmer because it looks very familiar, but actually there is a lot to learn for a C programmer (I took the same path). Generics and Collections are part of the java language but, depending on your background, there is usually a lot to learn (not least, all the OO stuff) before you get on to those topics. But it would be hard to write FX without understanding them, that's why I mentioned earlier you've chosen a tricky project for a newcomer. But that is not to say its impossible, I just think you might feel you are making rapid progress only to later discover you have missed out on a lot of foundational stuff.

    There have been a few topics recently about JavaFX futures and swing which might interest you, eg:
    http://www.coderanch.com/t/658117/JavaFX/java/JavaFX-Dead
    http://www.coderanch.com/t/655517/JavaFX/java/Java-FX-replaced-Swing
     
    John Damien Smith
    Ranch Hand
    Posts: 299
    15
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    > I haven't learned yet why some functions have angle brackets, or what generics and collections and factories are.

    I recommend all of the basic trails at the Oracle Java Tutorial.
    https://docs.oracle.com/javase/tutorial/

    The specialist ones you can skip for now and perhaps some of them you can skip forever, generally the basics with suffice for a lot of programming. Well except for the Generics tutorial, but even then initially you only really need to know how to use generics with existing libraries not write new generic libraries, so that is usually quite a bit easier and you don't have to worry if you don't initially get all the generics stuff.
    https://docs.oracle.com/javase/tutorial/extra/generics/index.html

    Do also spend time getting to know lambda expressions. You can write programs without them, but you will start to see them pop up more and more often in sample code and besides, functional programming is fun.
    https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html

    In terms of books, I haven't read a Java one in years, but if you are just starting, I think that is a good way to do (if you can find a good book). Books I know others have recommended, but I have never used are:
    http://www.amazon.com/Effective-Java-Edition-Joshua-Bloch/dp/0321356683
    http://www.amazon.com/Core-Java-I--Fundamentals-10th/dp/0134177304
    Effective Java is older, but, by all accounts a classic. Core Java is updated for Java 8, so it is not as outdated as some other books may be.

    Honestly, when I program Java, I use an IDE and I'm pretty lazy as the IDE hints lots of functions and types and syntax corrections for you. Not sure if that is the best way to learn a language rather than a simple text editor and command line, but I sure do find the IDE helpful.
    https://www.jetbrains.com/idea/

    > I'm not sure how much of this is java and how much is the javaFX api.

    It's all Java, the JavaFX API is only an API. It is just a set of classes you can program using the Java language, it has no syntax of it's own. Unlike other languages such as Scala, the Java syntax is not extensible to build custom domain-specific languages (DSLs). So everything written in Java has the same syntax, regardless of the API you write against. You lose a lot of flexibility, but it is, IMO, one of the reasons why Java has been relatively successful and long-lived. JavaFX does bring with it it's own conventions, such as it's use of properties and binding, but those functions are all just Java library functions, there is no specific syntax for it. One caveat is if you see very old books or JavaFX applications written in a language called JavaFX Script - that is different from Java and now obsolete. If you see a JavaFX Script application or book, ignore it or burn it. JavaFX Script was phased out prior to JavaFX 2.0, which is some years ago now, so it is unlikely you will come across it anymore.

    > There seems to be 2 schools of thought, one is that swing is dead, don't even bother learning it and just learn javaFX. The other is that javaFX is the dead one, because scene builder is no longer maintained, and etc... what's your take on this?

    Neither is dead. Swing is good for maintaining legacy apps or integrating into existing frameworks such as NetBeans. JavaFX is good for new applications. Both are only good if you are looking for a Java application framework. Most UIs these days seem to be written for web based front ends (html/javascript) rather than client GUI frameworks such as Swing or JavaFX. That said, I prefer writing in JavaFX than web apps, but that is just a personal preference of mine. You should develop your own personal preference and go with that. If it were me and I was learning something new and choosing between learning Swing and JavaFX, I'd learn JavaFX unless I needed to work on an existing Swing application. If your only purpose is to get a job and not necessarily to have fun or work on a project which specifically requires a standalone client application, then don't learn either, instead learn back-end programming using many of the web backend frameworks and angularjs+html5 for the front end. If your learning to program for the purpose of becoming a good programmer and want a nice quick way in GUIs, then learning JavaFX is definitely a good way to go rather than other approaches IMO.

    Scene Builder is maintained by Gluon:
    http://gluonhq.com/open-source/scene-builder/
    Some Oracle engineers might still work on Scene Builder (I don't know). Gluon seem to put out new releases every now and then. I think it is open source and anybody can contribute. The latest versions seem to work and be pretty stable to me. I don't use Eclipse and don't know if it or e(fx)clipse includes Scene Builder. Even if it did, I think you would be best to download and use the latest Gluon version (that is what I do with IntelliJ Idea).
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic