Saturday, November 7, 2015

JavaFx Tutorial For Beginners 12 - JavaFX ComboBox













package application;
 
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;


public class Main extends Application {
 @Override
 public void start(Stage primaryStage) {
  try {
   Parent root = FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
   Scene scene = new Scene(root);
   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);
 }
}








package application;

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;

public class MainController implements Initializable{
 
 @FXML
 public Label myLabel;
 @FXML
 public ComboBox<String> combobox;
 
 ObservableList<String> list = FXCollections.observableArrayList("Mark","Tom", "John","Jack");
 @Override
 public void initialize(URL location, ResourceBundle resources) {
  // TODO Auto-generated method stub
  //combobox.setItems(list);
  
 }
 
 public void comboChanged(ActionEvent event) {
  combobox.getItems().addAll("Ram","Ben", "STEVE","Ma");
  //myLabel.setText(combobox.getValue());
 }
 
 public void buttenAction(ActionEvent event) {
  combobox.getItems().addAll("Ram","Ben", "STEVE","Ma");
  //myLabel.setText(combobox.getValue());
 }

}












<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<?import javafx.collections.*?>

<AnchorPane prefHeight="300.0" prefWidth="300.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.40" fx:controller="application.MainController">
   <children>
      <ComboBox fx:id="combobox" layoutX="75.0" layoutY="108.0" onAction="#comboChanged" prefHeight="51.0" prefWidth="172.0" promptText="Select Name" >
      <items>
      <FXCollections fx:factory="observableArrayList">
          <String fx:value="Item 1" />
          <String fx:value="Item 2" />
          <String fx:value="Item 3" />
          <String fx:value="Item 4" />
      </FXCollections>
      </items>
      </ComboBox>
      <Label fx:id="myLabel" layoutX="75.0" layoutY="44.0" prefHeight="51.0" prefWidth="172.0" text="Label" />
      <Button layoutX="109.0" layoutY="203.0" mnemonicParsing="false" onAction="#buttenAction" text="Button" />
   </children>
</AnchorPane>

















No comments:

Post a Comment