Skip to Main Content

Java Development Tools

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

List view example in Javafx Tutorial worked in JDK1.7 doesn't work correct in JDK1.8

Iwa Java HuangFeb 23 2014 — edited Feb 26 2014

I copied the List View example shown in Javafx Tutorial Example 11-4 Creating a Cell Factory and made several changes.

I added an Add button and a Clear Button on top of the list view. When the Add button is clicked, one color bar is added to the list view.

When the clear button is clicked, all the list view items will be cleared. The number on right side of the clear button shows how many times the Add button is clicked.

This source worked fine in JDK1.7, but it doesn't work right in JDK1.8. The problem is that double color bar will be added some times even just one clicked on the Add button.

After click the Clear button, all the list view items will be cleared, but when I click the Add button again, all the previous cleared the list view color bars are displayed again.

These color bars are not true list view item. They can not be selected. They looks like a garbage. May be I did not describe the problem clearly, you will experience the problem when you run the following source codes.

Can some one tell me what I made wrong on the source?

Any comments are very appreciated.

Best regards

Iwa

package listviewsample;

import javafx.application.Application;

import javafx.collections.FXCollections;

import javafx.collections.ObservableList;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.Label;

import javafx.scene.control.ListCell;

import javafx.scene.control.ListView;

import javafx.scene.layout.HBox;

import javafx.scene.layout.Priority;

import javafx.scene.layout.VBox;

import javafx.scene.paint.Color;

import javafx.scene.shape.Rectangle;

import javafx.stage.Stage;

import javafx.util.Callback;

public class ListViewSample extends Application {

    ListView<String> list = new ListView<String>();

    ObservableList<String> data = FXCollections.observableArrayList(

            "chocolate", "salmon", "gold", "coral", "darkorchid",

            "darkgoldenrod", "lightsalmon", "black", "rosybrown", "blue",

            "blueviolet", "brown");

    ObservableList<String> listData = FXCollections.observableArrayList();

    Integer index = 0;

    Button btnAdd;

    Label lblNum;

    @Override

    public void start(Stage stage) {

        VBox box = new VBox();

        btnAdd = new Button("Add");

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

            public void handle(ActionEvent event) {

                listData.add(data.get(index++));

                if( index > 11 )btnAdd.setDisable(true);

                lblNum.setText(index.toString());

            }

        });

        Button btnClear = new Button("Clear");

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

            public void handle(ActionEvent event) {

                listData.clear();

                index = 0;

                btnAdd.setDisable(false);

                lblNum.setText(index.toString());

            }

        });

       

        lblNum = new Label("0");

        HBox hBox = new HBox();

        hBox.getChildren().addAll(btnAdd, btnClear, lblNum);

        hBox.setSpacing(20);

       

        Scene scene = new Scene(box, 200, 500);

        stage.setScene(scene);

        stage.setTitle("ListViewSample");

        box.getChildren().addAll(hBox,list);

        VBox.setVgrow(list, Priority.ALWAYS);

        list.setItems(listData);

        list.setCellFactory(new Callback<ListView<String>,

            ListCell<String>>() {

                @Override

                public ListCell<String> call(ListView<String> list) {

                    return new ColorRectCell();

                }

            }

        );

        stage.show();

    }

    static class ColorRectCell extends ListCell<String> {

        @Override

        public void updateItem(String item, boolean empty) {

            super.updateItem(item, empty);

            Rectangle rect = new Rectangle(100, 20);

            if (item != null) {

                rect.setFill(Color.web(item));

                setGraphic(rect);

            }

        }

    }

   

    public static void main(String[] args) {

        launch(args);

    }

}

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 26 2014
Added on Feb 23 2014
4 comments
1,722 views