1

The code below contains two Listview's, the user is to select a name from the first list view and when the add button is hit, it will move the content to an array which the 2nd List view is supposed to update and display as changes are made.

I thought we had the correct idea by converting the selection to a string then adding it to array. But when attempting to print the array for test purposes nothing appears.

any feed back or help would be greatly appreciated

package poolproject;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;

/**
 *
 * @author Alex
 */
public class FXMLDocumentController implements Initializable {

    @FXML
    private Button BtnAdd;

    @FXML
    private ListView<String> boxTeam;

    @FXML
    private ListView<String> boxPlayers;

    ArrayList<String> team= new ArrayList();
    String player;

    final ObservableList<String> playersAvailable = FXCollections.observableArrayList(
            "Kardi","Gilmore","Clark");

    final ObservableList<String> teamOutput = FXCollections.observableArrayList(team);

    @FXML
    private void deleteAction(ActionEvent action){
        int selectedItem = boxPlayers.getSelectionModel().getSelectedIndex();
        player = Integer.toString(selectedItem);
        team.add(player);

        playersAvailable.remove(selectedItem);
    }


    @Override
    public void initialize(URL url, ResourceBundle rb) {

        boxPlayers.setItems(playersAvailable);
        boxTeam.setItems(teamOutput);


    }    

}

1 Answers1

2

Adding an item to a plain list will not cause updates to be fired (the ArrayList does not have a mechanism to register any listeners). Adding an item to an ObservableList will cause listeners to be notified.

Do

String selectedItem = boxPlayers.getSelectionModel().getSelectedItem();
playersAvailable.remove(selectedItem);
teamOutput.add(selectedItem);
James_D
  • 201,275
  • 16
  • 291
  • 322