Code:
package javafxtemplate1;
import java.sql.SQLException;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Tooltip;
import javafx.stage.Stage;
public class JavaFXTemplate1 extends Application {
@Override
public void start(Stage primaryStage) {
Group groot = new Group();
String str1 = "Selected ";
//
ObservableList<String> oString1 = FXCollections.<String>observableArrayList("Jan",
"FEB", "MAR");
ComboBox<String> CBox1 = new ComboBox(oString1);
//CBox1.getItems().addAll(null,"Jan", "FEB", "MAR");
CBox1.setLayoutX(100);CBox1.setLayoutY(10);
Button btn1 = new Button("list addAll");
Button btn2 = new Button("Exit Platform");
Button btn3 = new Button("Remove JAN");
Button btn4 = new Button("SubList 1,4");
btn3.setVisible(false);//true
btn4.setVisible(false);
btn1.setLayoutX(10);btn1.setLayoutY(10);
btn2.setLayoutX(10);btn2.setLayoutY(60);
btn3.setLayoutX(10);btn3.setLayoutY(90);
btn4.setLayoutX(10);btn4.setLayoutY(115);
Label label1 = new Label("add/Remove");
label1.setLayoutX(10);label1.setLayoutY(40);
//Class Implementing Event handler
btn2.setOnAction(new DemoEventHandler1(){ });
// adding more element
btn1.setOnAction((ActionEvent e) -> {
CBox1.getItems().addAll("APR", "MAY");
btn3.setVisible(true);
btn4.setVisible(true);
});
//removing element JAN only
btn3.setOnAction((ActionEvent e) -> {
CBox1.getItems().removeAll("Jan");
btn1.setVisible(false);
});
//
btn4.setOnAction((ActionEvent e) -> {
label1.setText(CBox1.getItems().subList(1, 4).toString());
//CBox1.getItems().set(1,"JAN");
btn1.setVisible(false);
});
//
CBox1.setTooltip(new Tooltip("Selet Month"));
CBox1.getSelectionModel().selectFirst();
//
CBox1.getSelectionModel().selectedItemProperty().addListener( new
ChangeListener() {
@Override
public void changed(ObservableValue observable, Object oldValue, Object
newValue) {
// throw new UnsupportedOperationException("Not supported yet."); //To
change body of generated methods, choose Tools | Templates.
//String n1 = CBox1.getFocusModel().getFocusedItem();
label1.setText((String) oldValue + " >"+(String) newValue);
}
});
//groot.getChildren().addAll(btn2,label1);
groot.getChildren().addAll(btn1,btn2,label1,btn3,btn4, CBox1);
Scene scene = new Scene(groot, 350, 150);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFx ComboBox");
primaryStage.show();
}
public static void main(String[] args) throws SQLException {
launch(args);
}
}
class DemoEventHandler1 implements EventHandler<ActionEvent>{
@Override
public void handle(ActionEvent event) {
Platform.exit();
}
}
|