Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate TableView with ObservableMap JavaFX

I wanted to know if it is possible to use a ObservableMap to populate a TableView ? I use ObservableMap instead of ObservableList because I need to add and delete often, so I need to minimize the cost.

My hashMap use an BigInteger as key field and a type with many properties as value field. In my tableView I just want to display the values with a column per properties. I hope that is clear

Thanks

like image 714
Baya Avatar asked Jun 05 '13 14:06

Baya


1 Answers

I've been trying to do this. I guess the post is old but I don't see any answers anywhere on the net. The examples use the map key for columns and then a list of maps for every row. I'd like to see the rows as keys and their associated values. It's a long example.

package tablemap;

import static java.lang.Math.random;
import java.util.Map;
import java.util.TreeMap;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
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.TableColumn;
import javafx.scene.control.TableColumn.CellEditEvent;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TableMap extends Application {

    @Override
    public void start(Stage primaryStage) {
        VBox root = new VBox();
        Map<String,LineItem> mapData = new TreeMap<>();
        for (int i = 0; i < 3; i++)
            mapData.put(String.valueOf(random()), new LineItem(String.valueOf(i),"i"));

        ObservableList<Map.Entry<String,LineItem>> listData = 
                FXCollections.observableArrayList(mapData.entrySet());
        TableView<Map.Entry<String,LineItem>> tv = new TableView(listData);

        TableColumn<Map.Entry<String,LineItem>,String> keyCol = new TableColumn("Key");
        keyCol.setCellValueFactory(
            (TableColumn.CellDataFeatures<Map.Entry<String,LineItem>, String> p) -> 
                new SimpleStringProperty(p.getValue().getKey()));

        TableColumn<Map.Entry<String,LineItem>,String> lineNoCol = new TableColumn("Line No");
        lineNoCol.setCellValueFactory(
            (TableColumn.CellDataFeatures<Map.Entry<String,LineItem>, String> p) -> 
                new SimpleStringProperty(p.getValue().getValue().getLineNo()));

        TableColumn<Map.Entry<String,LineItem>,String> descCol = new TableColumn("Desc");
        descCol.setCellValueFactory(
            (TableColumn.CellDataFeatures<Map.Entry<String,LineItem>, String> p) -> 
                new SimpleStringProperty(p.getValue().getValue().getDesc()));

        descCol.setCellFactory(TextFieldTableCell.forTableColumn());

        descCol.setOnEditCommit((CellEditEvent<Map.Entry<String,LineItem>, String> t) -> {
             t.getTableView().getItems().get(t.getTablePosition().getRow())
                     .getValue().setDesc(t.getNewValue());
        });

        tv.getColumns().addAll(keyCol,lineNoCol, descCol);
        tv.setEditable(true);
        tv.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

        Button btnOut = new Button("out");
        btnOut.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                for (Map.Entry<String,LineItem> me : mapData.entrySet()){
                    System.out.println("key "+me.getKey()+" entry "+me.getValue().toCSVString());
                }
                for (Map.Entry<String,LineItem> me : listData){
                    System.out.println("key "+me.getKey()+" entry "+me.getValue().toCSVString());
                }
            }
        });

        root.getChildren().addAll(tv,btnOut);
        Scene scene = new Scene(root, 300, 200);

        primaryStage.setTitle("Map Table Test");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

And the LineItem Class Code

package tablemap;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

/* LineItem class */

public class LineItem {
    private final StringProperty lineNo = new SimpleStringProperty();
    private final StringProperty desc = new SimpleStringProperty();

    public LineItem(String ln, String dsc) {
        lineNo.set(ln); desc.set(dsc);
    }

    public String getLineNo() {return (lineNo.getValue() != null) ?lineNo.get():"";}
    public void setLineNo(String lineNo) {this.lineNo.set(lineNo);}
    public StringProperty lineNoProperty() {return lineNo;}

    public String getDesc() {return (desc.getValue() != null) ?desc.get():"";}
    public void setDesc(String desc) {this.desc.set(desc);}
    public StringProperty descProperty() {return desc;}

    public String toCSVString(){
        return lineNo.getValueSafe()+","+
                desc.getValueSafe()+"\n"; 
    }
}

You can see after editing data and clicking out that changes in the list are reflected in the map. I still have to check the other way and handle insertions and deletions but that shouldn't be to hard.

like image 183
brian Avatar answered Oct 20 '22 12:10

brian