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.
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().
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.
posted 4 years ago
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.
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.
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.
> 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?
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).