Created
June 23, 2021 13:51
-
-
Save Da9el00/cde4381b8f17299693bab886ec700a78 to your computer and use it in GitHub Desktop.
JavaFX and Scene Builder - Basic Java Search bar setup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import javafx.event.ActionEvent; | |
import javafx.fxml.FXML; | |
import javafx.fxml.Initializable; | |
import javafx.scene.control.ListView; | |
import javafx.scene.control.TextField; | |
import java.net.URL; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.ResourceBundle; | |
import java.util.stream.Collectors; | |
public class Controller implements Initializable { | |
ArrayList<String> words = new ArrayList<>( | |
Arrays.asList("test", "dog","Human", "Days of our life", "The best day", | |
"Friends", "Animal", "Human", "Humans", "Bear", "Life", | |
"This is some text", "Words", "222", "Bird", "Dog", "A few words", | |
"Subscribe!", "SoftwareEngineeringStudent", "You got this!!", | |
"Super Human", "Super", "Like") | |
); | |
@FXML | |
private TextField searchBar; | |
@FXML | |
private ListView<String> listView; | |
@FXML | |
void search(ActionEvent event) { | |
listView.getItems().clear(); | |
listView.getItems().addAll(searchList(searchBar.getText(),words)); | |
} | |
@Override | |
public void initialize(URL url, ResourceBundle resourceBundle) { | |
listView.getItems().addAll(words); | |
} | |
private List<String> searchList(String searchWords, List<String> listOfStrings) { | |
List<String> searchWordsArray = Arrays.asList(searchWords.trim().split(" ")); | |
return listOfStrings.stream().filter(input -> { | |
return searchWordsArray.stream().allMatch(word -> | |
input.toLowerCase().contains(word.toLowerCase())); | |
}).collect(Collectors.toList()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import javafx.application.Application; | |
import javafx.fxml.FXMLLoader; | |
import javafx.scene.Parent; | |
import javafx.scene.Scene; | |
import javafx.stage.Stage; | |
public class Main extends Application { | |
@Override | |
public void start(Stage primaryStage) throws Exception{ | |
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); | |
primaryStage.setTitle("Hello World"); | |
primaryStage.setScene(new Scene(root)); | |
primaryStage.show(); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<?import javafx.scene.control.Button?> | |
<?import javafx.scene.control.ListView?> | |
<?import javafx.scene.control.TextField?> | |
<?import javafx.scene.layout.AnchorPane?> | |
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller"> | |
<children> | |
<TextField fx:id="searchBar" layoutX="24.0" layoutY="24.0" prefHeight="35.0" prefWidth="475.0" /> | |
<ListView fx:id="listView" layoutX="24.0" layoutY="69.0" prefHeight="309.0" prefWidth="553.0" /> | |
<Button layoutX="509.0" layoutY="24.0" mnemonicParsing="false" onAction="#search" prefHeight="35.0" prefWidth="68.0" text="Search" /> | |
</children> | |
</AnchorPane> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment