• Post Reply Bookmark Topic Watch Topic
  • New Topic

Take user input in TextField, display in ListView  RSS feed

 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am brand new to JavaFX (maybe a week). I am supposed to make a simple GUI (without FXML) that has two buttons on the first scene. The buttons are for adding a course and closing the window. When the user clicks the "Add Course" button, it takes them to the 2nd scene, where they enter a String into the TextField. If they click "Save" it is supposed to add that String to a list in the 1st scene. Here are my main problems:

1) We have lectures that are slides showing Java Swing (they're old).
2) The instructions use Swing terminology (i.e. add elements to a JList, create a JOptionPane, etc).
3) My textbook discusses JavaFX ONLY. It even states that it isn't needed in the book since the release of JavaFX.
4) I don't know the equivalent of a "JList" in JavaFX.

Everything is finished except that the Save button does nothing. It just returns to the 1st scene. If I click "Add Course" again, I return to the 2nd scene with my String still sitting there (I don't know how to clear it upon clicking "Save" yet.

The code you're about to see is probably horribly written and out of order. Console applications were just starting to become easy, and now this.

I simply don't know how to get the text entered by the user, and populate some sort of list in JavaFX. I don't know the methods or Classes to use. I thought an ArrayList might work, but I don't know how to use an ArrayList in FX that's not predefined like EVERY example that I've found.

I feel like I'm so close to figuring it out, but I've fought with this ONE task for almost a week now. Every google search gives me FXML and Android usage. I just want plain, hard-coded Java. Anyway, here it is:



I get no compile or runtime errors. When clicking save, it just does.....nothing.
 
Knute Snortum
Sheriff
Posts: 3615
87
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not a JavaFX expert, but here is a tutorial that might help. Also, I'm moving this to the JavaFX forum.
 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the reply. I've read several of the javadocs, but sadly they don't help me with user input. All the examples always show predefined strings or arrays.
 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not at my IDE until around 6pm CST, but I discovered a method setPromptText() for JavaFX. I'm going to look into this later. I might be on to something finally.....maybe.
 
Knute Snortum
Sheriff
Posts: 3615
87
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does list get set into a scene anywhere?
 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nevermind, that just prompts the user. The .getText method should be doing it. Maybe I'll try to debug with some print statements to see what is being taken as input.
 
John Damien Smith
Ranch Hand
Posts: 337
21
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Knute points out:

> Does list get set into a scene anywhere?

The list is never added to any scene, so it is never visible. So, even though you add items to the list, you never see them.

To fix this, add the list to your scene, e.g. like below:


Also, the following line does not do what you think it does:



Actually, contrary to the comment, it does not work and does not return the user to the first scene. This is because you execute the code in the action event handler of the button, so the first time you click save it does nothing, then the next time you click save, it returns you to the first scene, but without executing your code to add an item to the list. To fix that change that line to the following:



For a nicer UI experience, when the scene to allow the user to enter a course name is displayed, you can request focus on the input field so the user does not need to manually focus on it:



There are other cleanups and improvements which could be performed (for example you set the list view to editable but don't implement an editable list cell, so the list view is not really editable), but the above set of changes should at least solve your most basic functional issues.
 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understood everything except what you mentioned about stage1.setScene (scene1). Are you saying I need to remove it from the lambda expression and put it in curly braces underneath the expression instead? Or something completely different?

Thank you for the other criticism. It's greatly appreciated.
 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just noticed where Knute asked about setting the list in the scene. Completely overlooked it.

 
John Damien Smith
Ranch Hand
Posts: 337
21
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> I understood everything except what you mentioned about stage1.setScene (scene1). Are you saying I need to remove it from the lambda expression and put it in curly braces underneath the expression instead?

No, I mean you don't need and should not have any lambda expression. Nor do you need any curly braces.
You literally take this code:

And you replace it with this code:

Try it...
 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now that I'm sitting in front of my machine and looking at my code, that makes complete sense. I noticed the two clicks originally with my Save button, and the erroneous lambda expression in my buttonClick() method.

I'm looking forward to making richer UI's.

One thing I realized with the setting of the ListView is that I struggle greatly with constructors and parameters, even if it's coding up a console-only app. I just need to get a better grasp on them. I would've never thought to next the new VBox inside the new Scene, at least not at this point in my learning.

Despite my error, I'm happy to know that I made what I made through reading and trial and error up until those points. Thank you for your help.

Next, I think I'll work on adding a ScrollPane. :-)
 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nevermind on the scroll pane. I kept typing Strings and as soon as it reached the bottom, it automatically generated one. Is this a feature that I'm unaware of? What caused it?
 
Knute Snortum
Sheriff
Posts: 3615
87
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing I realized with the setting of the ListView is that I struggle greatly with constructors and parameters, even if it's coding up a console-only app. I just need to get a better grasp on them. I would've never thought to next the new VBox inside the new Scene, at least not at this point in my learning.


If you lookup Scene in the Java API, you will see a constructor like this:

Scene(Parent root, double width, double height)
Creates a Scene for a specific root Node with a specific size.


In our case, root will be a VBox(), so we could write:

The above code is fine, but notice that root is only useful for the nanoseconds between the two lines of code. You can get rid of root by noticing that the red code is equivalent:

VBox root = new VBox(list, layout1);
scene1 = new Scene(root, 400, 300);


So exchange the one for the other and you get:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!