JavaFX ComboBox CRUD

0
User Management System
Learn Kotlin, Retrofit, MVVM and MySQL using this all-in-one app. It is designed to be beginner friendly.

JavaFX ComboBox CRUD Example

 

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.control.TextField;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class ComboBoxCRUD extends Application {
    /*
    Our data Object
    */
    public static class SpiritualTeacher {

        private final SimpleStringProperty name;

        private SpiritualTeacher(String name) {
            this.name = new SimpleStringProperty(name);
        }
        public String getName() {
            return name.get();
        }
        public void setName(String mName) {
            name.set(mName);
        }
        @Override
        public String toString() {
            return getName(); 
        }
    }
    /*
    ObservableList. Our data source.
    */
    private final ObservableList<SpiritualTeacher> spiritualTeachers =
            FXCollections.observableArrayList(
                    new SpiritualTeacher("Sedata"),
                    new SpiritualTeacher("Meister Eckhart"),
                    new SpiritualTeacher("Confucius"),
                    new SpiritualTeacher("Rumi"),
                    new SpiritualTeacher("Ramana Maharshi"));
    private int selectedIndex=-1;
    /*
    JavaFx Start Method
    */
    @Override
    public void start(Stage stage) throws Exception {
        Scene scene = new Scene(new Group());
        stage.setTitle("JavaFX ComboBox CRUD - ADD UPDATE DELETE CLEAR");
        stage.setWidth(450);
        stage.setHeight(550);

        Label nameLabel = new Label("Spiritial Teachers");
        nameLabel.setFont(new Font("Arial", 20));

        TextField nameTxt = new TextField();
        nameTxt.setPromptText("Name");

        ComboBox myComboBox=new ComboBox();
        myComboBox.setItems(spiritualTeachers);

        myComboBox.setOnAction(event -> {
            String selectedItem = myComboBox.getSelectionModel().getSelectedItem().toString();
            selectedIndex=myComboBox.getSelectionModel().getSelectedIndex();
            nameTxt.setText(selectedItem);
        });

        Button addButton = new Button("Add");
        addButton.setOnAction((ActionEvent e) -> {
            spiritualTeachers.add(new SpiritualTeacher(nameTxt.getText()));
            nameTxt.clear();
        });
        Button updateBtn = new Button("Update");
        updateBtn.setOnAction((ActionEvent e) -> {
            spiritualTeachers.remove(selectedIndex);
            spiritualTeachers.add(selectedIndex, new SpiritualTeacher(nameTxt.getText()));
            nameTxt.clear();
        });
        Button deleteBtn = new Button("Delete");
        deleteBtn.setOnAction((ActionEvent e) -> {
            spiritualTeachers.remove(selectedIndex);
            nameTxt.clear();
        });
        Button clearBtn = new Button("Clear");
        clearBtn.setOnAction((ActionEvent e) -> {
            spiritualTeachers.clear();
            nameTxt.clear();
        });

        HBox myHBox = new HBox();
        myHBox.getChildren().addAll(nameTxt, addButton,updateBtn,deleteBtn,clearBtn);
        myHBox.setSpacing(3);

        VBox myVBox = new VBox();
        myVBox.setSpacing(5);
        myVBox.setPadding(new Insets(10, 0, 10, 10));
        myVBox.getChildren().addAll(nameLabel, myComboBox, myHBox);

        ((Group) scene.getRoot()).getChildren().addAll(myVBox);

        stage.setScene(scene);
        stage.show();
    }
    /*
    Main method
    */
    public static void main(String[] args) {
        Application.launch(args);
    }
}
Android MySQL Retrofit2 Multipart CRUD,Search,Pagination rating

When I was a 2nd year Software Engineering student, I buillt a now defunct online tool called Camposha(from Campus Share) using my then favorite language C#(ASP.NET) to compete OLX in my country(Kenya). The idea was to target campus students in Kenya. I got a few hundred signups but competing OLX proved too daunting. I decided to focus on my studies, learning other languages like Java,Python,Kotlin etc while meanwhile publishing tutorials at my YouTube Channel ProgrammingWizards TV which led to this site(camposha.info). Say hello or post me a suggestion: oclemmi@gmail.com . Follow me below; Github , and on my channel: ProgrammingWizards TV

We will be happy to hear your thoughts

Leave a reply

seventy − = 69

Reset Password
Compare items
  • Total (0)
Compare
0
Shopping cart