JavaFX ListView (Tweets in a ListView)
JavaFX ListView
I was browsing the javafx api 2.2 and came along the Cell class got a little bit more interested with this statement "An item within a Cell may be represented by text or some other control such as a
CheckBox
, ChoiceBox
or any other Node
such as a HBox
, GridPane
, or even a Rectangle
."
I decided to create a listview populated by tweets ,courtesy of twitter4j api.
My TweetCell Class
class TwitterCell extends ListCell<String> {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if(item!=null){
StackPane st=new StackPane();
st.setId("tweet_pane");
ImageView image=new ImageView("file:bg.png");
image.setFitHeight(64);
image.setFitWidth(64);
image.setSmooth(true);
StackPane stack_pane=new StackPane();
stack_pane.setMaxWidth(64);
stack_pane.setMaxHeight(64);
stack_pane.setId("image_pane");
stack_pane.getChildren().add(image);
Text name_txt=new Text();
name_txt.setFill(Color.DEEPSKYBLUE);
Reflection ref=new Reflection();
ref.setFraction(0.8);
ref.setTopOffset(0.5);
name_txt.setEffect(ref);
name_txt.setFont(Font.font("castellar",FontWeight.BOLD,18));
name_txt.setText(item);
Text status_txt=new Text();
status_txt.setFill(Color.BLACK);
status_txt.setFont(Font.font("arial",15));
status_txt.setWrappingWidth(500);
status_txt.setText(item);
StackPane.setAlignment(stack_pane, Pos.TOP_LEFT);
StackPane.setMargin(stack_pane,new Insets(5,0,0,5));
StackPane.setAlignment(name_txt, Pos.TOP_CENTER);
StackPane.setMargin(name_txt,new Insets(15,0,0,5));
StackPane.setAlignment(status_txt, Pos.TOP_LEFT);
StackPane.setMargin(status_txt,new Insets(75,0,0,5));
st.getChildren().addAll(stack_pane,name_txt,status_txt);
setGraphic(st);
}
}
}
populate your listview
listView.setItems("observableList");
set the cellfactory of the listView
listView.setCellFactory(new Callback<ListView<String>,
ListCell<String>>() {
@Override
public ListCell<String> call(ListView<String> list) {
return new TwitterCell();
}
}
);
That's a nice use of cell factory. Its one of my favorite things to play with in javafx
ReplyDelete