Skip to content

Instantly share code, notes, and snippets.

@sharifulislam52
Created May 4, 2018 17:51
Show Gist options
  • Save sharifulislam52/269074a1dbcd08203521abdb4c2689ba to your computer and use it in GitHub Desktop.
Save sharifulislam52/269074a1dbcd08203521abdb4c2689ba to your computer and use it in GitHub Desktop.
javafx : ComboBox and Listview
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
Scene scene = new Scene(root,300,300);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="300.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
<children>
<ComboBox fx:id="combobox" layoutX="14.0" layoutY="42.0" onAction="#comboChange" prefHeight="41.0" prefWidth="152.0" promptText="Select Name" />
<Label fx:id="combo_selected_item" alignment="CENTER" layoutX="14.0" layoutY="14.0" prefHeight="28.0" prefWidth="152.0" />
<Button layoutX="14.0" layoutY="92.0" mnemonicParsing="false" onAction="#comboAddElement" text="Add New Items" />
<ListView fx:id="listview" layoutX="10.0" layoutY="133.0" prefHeight="157.0" prefWidth="280.0" />
</children>
</AnchorPane>
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
public class MainController implements Initializable{
@FXML
public Label combo_selected_item;
@FXML
public ComboBox<String> combobox;
ObservableList<String> list = (ObservableList<String>) FXCollections.observableArrayList("Fahim", "Nava", "Sabbir", "Prinka", "Mim");
@FXML
public ListView<String> listview;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
combobox.setItems(list);
listview.setItems(list);
listview.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
}
public void comboChange(ActionEvent event) {
combo_selected_item.setText(combobox.getValue());
}
public void comboAddElement(ActionEvent event) {
// Add item on combo box and listview
//combobox.getItems().addAll("Tumpa", "Mithila","Afsana","Bristy");
//listview.getItems().addAll("Tumpa", "Mithila","Afsana","Bristy");
// Show All Selected Items from listview
ObservableList<String> names;
names = listview.getSelectionModel().getSelectedItems();
StringBuffer all_name = new StringBuffer();
for(String name:names) {
all_name.append(name);
}
combo_selected_item.setText(all_name.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment