简体   繁体   中英

Generate dynamic TilePane from Objects

I want to create dynamic TilePane which displays data from Java list of objects:

import java.util.ArrayList;  
import java.util.List;  
import java.util.Random;  
import javafx.application.Application;  
import static javafx.application.Application.launch;  
import javafx.concurrent.ScheduledService;  
import javafx.concurrent.Task;  
import javafx.concurrent.WorkerStateEvent;  
import javafx.event.EventHandler;  
import javafx.geometry.Insets;  
import javafx.scene.Scene;  
import javafx.scene.layout.GridPane;  
import javafx.scene.text.Text;  
import javafx.stage.Stage;  
import javafx.util.Duration;  

public class MainApp extends Application  
{  

    final JVMDataService service = new JVMDataService();  

    Text one = new Text();  
    Text two = new Text();  
    Text three = new Text();  
    Text four = new Text();  

    @Override  
    public void start(Stage stage) throws Exception  
    {  

        Scene scene = new Scene(addGridPane());  

        datatask();  

        stage.setTitle("JavaFX and Maven");  
        stage.setScene(scene);  
        stage.show();  
    }  

    public static void main(String[] args)  
    {  
        launch(args);  
    }  

    GridPane grid = new GridPane();  

    public GridPane addGridPane()  
    {  

        grid.setHgap(10);  
        grid.setVgap(10);  
        grid.setPadding(new Insets(0, 10, 0, 10));  

        Text category = new Text("Name 1");  
        grid.add(category, 0, 0);  

        grid.add(one, 1, 0);  

        grid.add(two, 2, 0);  

        grid.add(three, 3, 0);  

        grid.add(four, 4, 0);  

//        Text categos = new Text("Name 2");  
//        grid.add(categos, 0, 1);  
//  
//        grid.add(two, 1, 1);  
//  
//        grid.add(two, 2, 1);  
//  
//        grid.add(two, 3, 1);  
//  
//        grid.add(two, 4, 1);  
//  
//        Text cww = new Text("Name 3");  
//        grid.add(cww, 0, 2);  
//  
//        grid.add(three, 1, 2);  
//  
//        grid.add(three, 2, 2);  
//  
//        grid.add(three, 3, 2);  
//  
//        grid.add(three, 4, 2);  
        grid.setPadding(new Insets(30, 30, 30, 30));  

        return grid;  
    }  

    List<DataObj> myList;  

    public void datatask()  
    {  

        service.setDelay(new Duration(300));  
        service.setPeriod(new Duration(1000));  
        service.setOnSucceeded(new EventHandler<WorkerStateEvent>()  
        {  
            @Override  
            public void handle(final WorkerStateEvent workerStateEvent)  
            {  
                List<DataObj> results = (List<DataObj>) workerStateEvent.getSource().getValue();  

                for (DataObj element : results)  
                {  
                    one.setText(Integer.toString(element.getValueOne()));  
                    two.setText(Integer.toString(element.getValueTwo()));  
                    three.setText(Integer.toString(element.getValueThree()));  
                    four.setText(Integer.toString(element.getValueFour()));  
                }  
            }  
        });  
        service.start();  

    }  

    // Service  
    class JVMDataService extends ScheduledService<List<DataObj>>  
    {  
        @Override  
        protected Task<List<DataObj>> createTask()  
        {  
            final Task<List<DataObj>> voidTask = new Task<List<DataObj>>()  
            {  
                @Override  
                protected List<DataObj> call() throws Exception  
                {  
                    return getList();  
                }  
            };  

            return voidTask;  
        }  
    }  

    public List<DataObj> getList()  
    {  
        myList = new ArrayList<>();  
        Random rand = new Random();  

        int n = rand.nextInt(50) + 1;  
        int s = rand.nextInt(50) + 1;  

        DataObj dd = new DataObj().newInstance().valueOne(n).valueTwo(s).valueThree(s).valueFour(s).valueFive(s);  
        DataObj dda = new DataObj().newInstance().valueOne(n + 2).valueTwo(s + 2).valueThree(s + 2).valueFour(s + 2).valueFive(s + 2);  
        DataObj dds = new DataObj().newInstance().valueOne(n + 22).valueTwo(s + 21).valueThree(s + 21).valueFour(s + 21).valueFive(s + 21);  
        DataObj ddd = new DataObj().newInstance().valueOne(n + 21).valueTwo(s + 23).valueThree(s + 23).valueFour(s + 23).valueFive(s + 23);  
        DataObj ddf = new DataObj().newInstance().valueOne(n + 25).valueTwo(s + 29).valueThree(s + 29).valueFour(s + 29).valueFive(s + 29);  

        myList.add(dd);  
        myList.add(dda);  
        myList.add(dds);  
        myList.add(ddd);  
        myList.add(ddf);  

        return myList;  
    }  

    public class DataObj  
    {  
        private int valueOne;  
        private int valueTwo;  
        private int valueThree;  
        private int valueFour;  
        private int valueFive;  

        public DataObj newInstance()  
        {  
            return new DataObj();  
        }  

        public DataObj()  
        {  
        }  

        public DataObj valueOne(int valueOne)  
        {  
            this.valueOne = valueOne;  
            return this;  
        }  

        public DataObj valueTwo(int valueTwo)  
        {  
            this.valueTwo = valueTwo;  
            return this;  
        }  

        public DataObj valueThree(int valueThree)  
        {  
            this.valueThree = valueThree;  
            return this;  
        }  

        public DataObj valueFour(int valueFour)  
        {  
            this.valueFour = valueFour;  
            return this;  
        }  

        public DataObj valueFive(int valueFive)  
        {  
            this.valueFive = valueFive;  
            return this;  
        }  

        public int getValueOne()  
        {  
            return valueOne;  
        }  

        public int getValueTwo()  
        {  
            return valueTwo;  
        }  

        public int getValueThree()  
        {  
            return valueThree;  
        }  

        public int getValueFour()  
        {  
            return valueFour;  
        }  

        public int getValueFive()  
        {  
            return valueFive;  
        }  

    }  

}  

Th problem is that the list of Objects is dynamic. I need to display every time TilePane with different size. Can you help me to solve this problem? Regards.

Hej Peter,

have you tried to put the TilePane into a ScrollPane , so you can scroll to the right if there are more Columns . Another idea is to increase the number of the Rows within the TilePane .

I have a small example, i used a Person class, not your DataObject .

Here is the Service class

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;

/**
 *
 * @author Patrick Ott <Patrick.Ott@professional-webworkx.de>
 * @version 1.0
 */
public class JVMDataService extends ScheduledService<List<Person>> {

    private final Random generator = new Random();

    @Override
    protected Task<List<Person>> createTask() {
        return new Task<List<Person>>() {

            @Override
            protected List<Person> call() throws Exception {
                int n = generator.nextInt(50);
                List<Person> persons = new ArrayList<>();
                for (int i = 0; i < n; i++) {
                    System.out.println("i = " + i);
                    persons.add(new Person("Max", "Mustermann", "max" + i + ".mustermann@example.com"));
                }
                return persons;
            }
        };
    };

}

And the App class

import java.util.List;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.EventHandler;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.TilePane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;

public class MainApp extends Application {

    JVMDataService service = new JVMDataService();

    @Override
    public void start(Stage stage) throws Exception {

        AnchorPane root = new AnchorPane();
        Scene scene = new Scene(root, 1280, 768);

        HBox hBox = new HBox();
        final TilePane tilePane = new TilePane(Orientation.VERTICAL, 25, 20);
        tilePane.setPrefRows(15);
        ScrollPane scrollPane = new ScrollPane(tilePane);
        scrollPane.setPrefViewportWidth(1100);
        scrollPane.setPrefViewportHeight(650);
        final VBox vBox = new VBox(scrollPane);

        service.setDelay(Duration.seconds(3));
                // time between every Period / Run
                service.setPeriod(Duration.seconds(3));
                service.setOnSucceeded(new EventHandler<WorkerStateEvent>() {

                    @Override
                    public void handle(WorkerStateEvent t) {
                        List<Person> persons = (List<Person>) t.getSource().getValue();
                        for (Person person : persons) {
                            tilePane.getChildren().add(new Text(person.getFirstName() + " " + person.getLastNameProperty().get() + ", " + person.geteMailProperty().get()));
                        }
                    }
                });
                service.start();


        hBox.getChildren().add(vBox);

        root.getChildren().add(hBox);
        stage.setTitle("JavaFX and Maven");
        stage.setScene(scene);
        stage.show();
    }

    /**
     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifacts, e.g., in IDEs with limited FX
     * support. NetBeans ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

Here is the Person class..

import java.io.Serializable;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;


public class Person implements Serializable {

    private StringProperty firstNameProperty    = new SimpleStringProperty();
    private StringProperty lastNameProperty     = new SimpleStringProperty();
    private StringProperty eMailProperty        = new SimpleStringProperty();

    public Person() {
    }

    public Person(final String firstName, final String lastName, final String email) {
        this.firstNameProperty.set(firstName);
        this.lastNameProperty.set(lastName);
        this.eMailProperty.set(email);
    }

    /**
     * @return the firstNameProperty
     */
    public StringProperty getFirstNameProperty() {
        return firstNameProperty;
    }

    /**
     * @return the firstName
     */
    public String getFirstName() {
        return firstNameProperty.get();
    }

    /**
     * @param firstNameProperty the firstNameProperty to set
     */
    public void setFirstNameProperty(StringProperty firstNameProperty) {
        this.firstNameProperty = firstNameProperty;
    }

    /**
     * @return the lastNameProperty
     */
    public StringProperty getLastNameProperty() {
        return lastNameProperty;
    }

    /**
     * @param lastNameProperty the lastNameProperty to set
     */
    public void setLastNameProperty(StringProperty lastNameProperty) {
        this.lastNameProperty = lastNameProperty;
    }

    /**
     * @return the eMailProperty
     */
    public StringProperty geteMailProperty() {
        return eMailProperty;
    }

    /**
     * @param eMailProperty the eMailProperty to set
     */
    public void seteMailProperty(StringProperty eMailProperty) {
        this.eMailProperty = eMailProperty;
    }
}

The Result should look like this:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM