JavaFX - 如何让TableView显示数组中的字符串?

时间:2014-11-04 16:40:51

标签: java javafx javafx-8 fxml

在尝试了许多不同的方法之后,我无法让这个TableView显示任何字符串。最初,数据应该来自ResultSet对象,但是我无法完成这项工作。我最近的尝试(下面)似乎是显示数据最直接的方式,但字符串仍然不会显示。

如您所见,TableView和ListView在FXML文档中描述。 ListView显示它的字符串没有任何问题。我错过了什么吗?

public class AudioPlayerFXMLController implements Initializable {

@FXML
private ListView playList;
@FXML
private TableView playListTableView;

@FXML
private TableColumn pathColumn, artistColumn, titleColumn, albumColumn,
        trackNumberColumn, lengthColumn, descriptionColumn,
        ratingColumn, dateColumn, settingColumn, URLColumn, languageColumn,
        nowPlayingColumn, publishedByColumn, encodedByColumn, artworkURLColumn,
        trackIDColumn;

private static final String NEW_PLAYLIST = "New Playlist";
private static final String FRIEND_PLAYLIST = "Friend's Playlist";
private static final String LIBRARY = "Library";
private static ButtonCell bc;
ObservableList<String> observablePlayList;
ObservableList<String> observableListForTableView;

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

    observablePlayList = FXCollections.observableArrayList();
    observablePlayList.add(NEW_PLAYLIST);
    observablePlayList.add(FRIEND_PLAYLIST);
    observablePlayList.add(LIBRARY);

    observableListForTableView = FXCollections.observableArrayList();
    observableListForTableView.add(NEW_PLAYLIST);
    observableListForTableView.add(FRIEND_PLAYLIST);
    observableListForTableView.add(LIBRARY);

    List<String> values = Arrays.asList("one", "two", "three", "three", "three",
            "three", "three", "three", "three",
            "three", "three", "three", "three", ""
            + "three", "three", "three", "three");

    System.out.println(FXCollections.observableArrayList(values));
    playListTableView.setItems(FXCollections.observableArrayList(values));
    playList.setItems(observablePlayList);

    playList.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
        @Override
        public ListCell<String> call(ListView<String> list) {
            bc = new ButtonCell();
            return bc;
        }
    });

    playList.setEditable(true);
    playList.getSelectionModel().select(2);

    playList.setOnEditCommit(new EventHandler<ListView.EditEvent<String>>() {

        public void handle(ListView.EditEvent<String> event) {

            System.out.println(event.getEventType());
            System.out.println(event.getNewValue());
        }
    });

}

class ButtonCell extends ListCell<String> {

    private TextField fieldForEditingCell;

    @Override
    public void startEdit() {

        if (!isEditable() || !getListView().isEditable()) {
            return;
        }

        super.startEdit();

        if (isEditing()) {
            fieldForEditingCell = new TextField("Enter your edit here");
            fieldForEditingCell.setOnKeyPressed(new EventHandler<KeyEvent>() {

                public void handle(KeyEvent keyPress) {
                    if (keyPress.getCode() == KeyCode.ENTER) {
                        String nameOfnewPlayList = fieldForEditingCell.getText();
                        observablePlayList.add(nameOfnewPlayList);
                        playList.setItems(null);
                        playList.setItems(observablePlayList);
                        playList.getSelectionModel().select(nameOfnewPlayList);

                    } else if (keyPress.getCode() == KeyCode.ESCAPE) {
                        cancelEdit();
                    }
                }
            });

            setGraphic(fieldForEditingCell);
            fieldForEditingCell.requestFocus();
            fieldForEditingCell.setEditable(true);

            fieldForEditingCell.focusedProperty().addListener(new ChangeListener<Boolean>() {
                @Override
                public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                    if (!newValue && fieldForEditingCell != null) {
                        commitEdit(fieldForEditingCell.getText());
                    }
                }
            });

            fieldForEditingCell.selectAll();

        }
    }

    @Override
    public void cancelEdit() {
        super.cancelEdit();
        ImageView addSymbol;
        addSymbol = ImageViewBuilder.create().image(new Image("/images/ic_add_grey600_15dp.png")).build();
        addSymbol.fitHeightProperty();
        setText(getItem());
        setGraphic(addSymbol);
    }

    @Override
    public void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);

        if ((this.getIndex() == 0) || (this.getIndex() == 1)) {
            ImageView addSymbol;
            addSymbol = ImageViewBuilder.create().image(new Image("/images/ic_add_grey600_15dp.png")).build();
            addSymbol.fitHeightProperty();
            setText(item);
            setGraphic(addSymbol);

            getStyleClass().add("custom_list-cell");
            //setStyle("-fx-background-color: #F5F5F5");
            //setStyle("-fx-hover-color: yellow");
        } else {
            setText(item);
            setGraphic(null);
        }
    }

}
//begin TableViewCell

class playListTableViewCellFactory extends TableCell<String, String> {

}

//begin tableView methods...
public void tableDraggedOVer(DragEvent event) {
    Dragboard db = event.getDragboard();
    if (db.hasFiles()) {
        event.acceptTransferModes(TransferMode.COPY);
    }
    event.consume();

}

public void dropCompleted(DragEvent event) {
    Dragboard db = event.getDragboard();
    boolean success = false;

    if (db.hasFiles()) {
        event.acceptTransferModes(TransferMode.COPY);
        DBUtils.addToPLaylistTableFromDragAndDrop(db.getFiles());
        success = true;
    }

    event.setDropCompleted(success);
    event.consume();
}

}

0 个答案:

没有答案