Событие работающее с ListView - Java
Формулировка задачи:
Здравствуйте! Пытаюсь научиться работать с JavaFX и создавать графический интерфейс для приложений. Столкнулся с такой проблемой: мне нужно брать одно значение из ListView и выводить его параметры в Label, но я не знаю какое событие и к чему вешать. Вроде нарыл getselectionModel(), но не понимаю как его применить.
Вот мой контроллер
вот fxml
package sample;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
public class Controller implements Initializable {
@FXML
private Button button;
@FXML
private Label label1;
@FXML
private ListView myListView;
@FXML
private ListView myListView1;
protected List<String> asianCurrencyList = new ArrayList<>();
protected List<String> europeanCurrencyList = new ArrayList<>();
protected ListProperty<String> listProperty = new SimpleListProperty<>();
protected ListProperty<String> listProperty1 = new SimpleListProperty<>();
@FXML
private void handleButtonAction(ActionEvent event) {
listProperty.set(FXCollections.observableArrayList(europeanCurrencyList));
}
@Override
public void initialize(URL url, ResourceBundle rb) {
asianCurrencyList.add("CNH");
asianCurrencyList.add("JPY");
asianCurrencyList.add("HKD");
asianCurrencyList.add("KRW");
asianCurrencyList.add("SGD");
europeanCurrencyList.add("EUR");
europeanCurrencyList.add("GBP");
europeanCurrencyList.add("NOK");
europeanCurrencyList.add("SEK");
europeanCurrencyList.add("CHF");
europeanCurrencyList.add("HUF");
myListView.itemsProperty().bind(listProperty);
myListView1.itemsProperty().bind(listProperty1);
//This does not work, you can not directly add to a ListProperty
//listProperty.addAll( asianCurrencyList );
listProperty.set(FXCollections.observableArrayList(asianCurrencyList));
listProperty1.set(FXCollections.observableArrayList(europeanCurrencyList));
}
}<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="437.0" prefWidth="288.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="sample.Controller">
<children>
<Button fx:id="button" layoutX="94.0" layoutY="345.0" onAction="#handleButtonAction" prefHeight="26.0" prefWidth="100.0" text="Click Me!" />
<ListView fx:id="myListView" layoutX="14.0" layoutY="21.0" prefHeight="313.0" prefWidth="100.0" />
<ListView fx:id="myListView1" layoutX="167.0" layoutY="21.0" prefHeight="313.0" prefWidth="100.0" />
<Label fx:id="label1" layoutX="130.0" layoutY="394.0" text="Label" />
</children>
</AnchorPane>
Решение задачи: «Событие работающее с ListView»
textual
Листинг программы
myListView.getSelectionModel().selectedItemProperty().addListener(
new ChangeListener<String>(){
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue){
label.setText(newValue);
}
}