An ActionListener is an event handler that defines what should be done when a user performs a certain operation. ActionListeners are functional interfaces. A functional interface is an interface that has only one abstract method. This method must be overridden before it can be used. Lambda Expressions can be used with functional interfaces to make this whole process easier. Essentially what we’re going to use a Lambda Expression to tell Java, hey make this property, do this thing when you do something with it.

After we set all our if statements we call the searchPart method we created in the setPartTable method, then we add the FilteredList to a sortedList so that the TableView can sort by the column headers, and then we pass this finished filterable, and sortable sorted list into the TableView’s setItems method.

The last thing the statement evaluates is if there isn’t a match for the text the user entered at all then it will return false, which basically means that the FilteredList will return nothing, and it will change the placeholder text of the TableView to show ‘No Part Found’.

In conclusion, JavaFX may not be able to give you all the answers to your life questions, but it can help you search for them.

C482Ford

Next, we set an ActionListener on the TextField so that it can pass in a predicate to our FilteredList whenever the user actually types in text into it.

So, that’s essentially it for our wonderful searchPart method. However it’s useless if we can’t have your TableView make use of the FilteredList.

WGU Software 1

Next it will evaluate if the text the user entered is either A: Part or all, of a part name, or B: If it’s a digit that matches the part ID of a part in the list. This means you can search by Part ID or by Part name essentially. As you can see, since it’s monitoring the textProperty this is done on the fly.

I’m going to be using the conventions that I received for my Software I project, note that your project may be searching different things instead of parts and products, either way the principles here will work. The TextField allows the user to type in text that can be passed into the FilteredList, what we need to do is create a TextField using SceneBuilder. Technically, you can hard code one using pure Java. I HIGHLY do not recommend you ever do this. Inevitably, you’ll want to move your JavaFX objects, like this TextField, around in the scene, or resize them. This can be extremely difficult to do if you hard code them. You can see in my TextField that I have prompt text “Search by Product ID or Name”. You don’t have to do this but it’s super easy to do in SceneBuilder and I think it make things look better.

Our searchPart method here takes in a second argument, a FilteredList. We can see that the FilteredList takes in ‘Part’ objects as input. The ‘Part’ objects are based on a custom class that you must create as part of this project. Your project may have something different, but the same ideas apply. These are the objects that we are searching for. What I did was make the FilteredList that’s going to be passed into this method here in the code.

Then we use the setPredicate method on the FilteredList that we passed in so that it can evaluate a few if statements to return true or false depending on the user types in.

What I did was call the searchPart method here in a method called setPartTable, this way the very first thing gets activated is the search system. After that I created a SortedList that takes in the Filtered parts list as an argument. This SortedList is then bound to the comparator property of the part table view. This enables you to easily sort the table by selecting the column headers similarly to what you would do in excel. After that we can set the items of the part table view to this sorted list. And BAM that’s it! Seriously, now the TableView items will be sorted according to whatever we type into the TextField AND it will be able to sort by the column headers.

Today we’re going over the Western Governor’s University Software I Project Search Bar. Specifically, how to use JavaFX to create a dynamic search bar that can filter results as soon as we type them in.

I created a FilteredList that gets passed in an observable List of parts to filter through. It will return a sub list passed on if the predicate ‘part’ is true or not. So basically, if the value we’re searching for are in the main, big, observable list, then it will show up in the FilteredList. Now that we understand what’s happening in the FilteredList we can go back to our searchPart method. The searchPart method sets a predicate for the FilteredList that we passed in. Technically, you can call this predicate whatever you want but I highly recommend for readability’s sake that you give it a name that makes sense. For us, that’s ‘part’. We’re going to have this predicate evaluate three different if statements and will manipulate the FilteredList depending on if these conditions are true or not.

Since we want to pass in the value into our FilteredList that the user passes in is looking for the TextField’s text property to add an ActionListener to it. This way the search can be instantaneous, as soon as the user types something it will be searched. This is one of the areas where your code may be different than mine. I wanted to be able to use the same search method with different TextFields, so my method takes in a TextField that we will name TextField as an argument. It then adds an ActionListener to the TextField’s textProperty. We will pass three values into our lambda expression as predicates. A predicate is kind of like an argument to a method but it works differently. It comes from Formal Logic, and what it does is it takes in a value, evaluates it based on a criteria that you make and it returns true if it makes that criteria. We’re passing in three predicates into our lambda expression, observable, which will be the TextField textProperty that we will be observing, newValue that will evaluate the new text that user will type into the search bar, and the old value which is the value that was previously in the TextField. Now, we’re going to work with our FilteredList to find parts based on the newValue that the user passed in

C482github

Before we get into the weeds, this video assumes that you have some working knowledge of Java, JavaFX, and SceneBuilder. The creating a search bar in JavaFX involves combing three separate things.

The first if statement says if newValue is null or if its empty return true. What this basically means is if the user types in NOTHING at all into the TextField, give them the whole list, or if the user types in a value, but then erases it will give them the whole list.