0

I was want have a table column with a button labeled "Add to Playlist". The row in the column represents a song. I have the following class:

private class ButtonCell extends TableCell<Record, Boolean> {
    final Button cellButton = new Button("Add to PlayList");

    ButtonCell(){

        cellButton.setOnAction(new EventHandler<ActionEvent>(){

            @Override
            public void handle(ActionEvent t) {
                // do something when button clicked
                //playList.add(this.getTableRow().getItem());
            }
        });
    }

    //Display button if the row is not empty
    @Override
    protected void updateItem(Boolean t, boolean empty) {
        super.updateItem(t, empty);
        if(!empty){
            setGraphic(cellButton);
        }
    }
}

EventHandler<ActionEvent> btnNewHandler = 
        new EventHandler<ActionEvent>(){

    @Override
    public void handle(ActionEvent t) {


    }
};

And I want to be able to do this 'playList.add(this.getTableRow().getItem());' Is there a way to do this?

The gist of the question is how do I get the information of a cell and add that information to an observable list?

Thanks

jaronoff97
  • 366
  • 3
  • 15

1 Answers1

0

For some reason, getTableRow() returns a raw TableRow object, instead of a generically typed one. In other words the method signature is

public class TableCell<S,T> {
    public TableRow getTableRow();
}

when I think it should be

public class TableCell<S,T> {
    public TableRow<S> getTableRow();
}

Because of this, the expresison getTableRow().getItem() has a compile-time type of Object, instead of the same type as the table (Record in your example), so you need a downcast. This should work (assuming you have everything else set up as I expect you do):

playList.add((Record) this.getTableRow().getItem() );

A different technique entirely is to make the column type Record, so you can just call getItem() directly on the cell. The answer to this question uses this approach.

Community
  • 1
  • 1
James_D
  • 201,275
  • 16
  • 291
  • 322
  • ha i keep solving some of my own issues, but I don't know how to solve the following: 'error: no suitable method found for add(Object) MusicPlayer.playlist.add(getTableRow().getItem() ); ^ method Collection.add(Song) is not applicable (argument mismatch; Object cannot be converted to Song) method List.add(Song) is not applicable (argument mismatch; Object cannot be converted to Song)' – jaronoff97 Jan 17 '15 at 21:35