使用Enum数据填充JavaFX TableView

时间:2016-02-29 16:36:34

标签: java javafx javafx-2 tableview

我正在为游戏开发Javafx应用程序。 我得到了一些存储在枚举中的数据,但似乎无法弄清楚如何轻松地将数据添加到JavaFX TableView,有人可以帮助我。 我将使用fxml来设置TableView的样式。

我希望每个enum vallue在一个单独的行中,在我想要的列中:

  1. 图标为图像。
  2. 排名为整数。
  3. 等级为整数。
  4. 作为整数体验。
  5. The Enum:

    package org.fryslan.ec.hiscore;
    
    import org.fryslan.ec.Core;
    
    public enum Hiscore {
    
    OVERALL(0, "Overall", null),
    ATTACK(1, "Attack", "skill_icon_attack.gif"),
    DEFENCE(2, "Defence", "skill_icon_defence.gif"),
    STRENGTH(3, "Strength", "skill_icon_strength.gif"),
    HITPOINTS(4, "Hitpoints", "skill_icon_hitpoints.gif"),
    RANGED(5, "Ranged", "skill_icon_ranged.gif"),
    PRAYER(6, "Prayer", "skill_icon_prayer.gif"),
    MAGIC(7, "Magic", "skill_icon_magic.gif"),
    COOKING(8, "Cooking", "skill_icon_cooking.gif"),
    WOODCUTTING(9, "Woodcutting", "skill_icon_woodcutting.gif"),
    FLETCHING(10, "Fletching", "skill_icon_fletching.gif"),
    FISHING(11, "Fishing", "skill_icon_fishing.gif"),
    FIREMAKING(12, "Firemaking", "skill_icon_firemaking.gif"),
    CRAFTING(13, "Crafting", "skill_icon_crafting.gif"),
    SMITHING(14, "Smithing", "skill_icon_smithing.gif"),
    MINING(15, "Mining", "skill_icon_mining.gif"),
    HERBLORE(16, "Herblore", "skill_icon_herblore.gif"),
    AGILITY(17, "Agility", "skill_icon_agility.gif"),
    THIEVING(18, "Thieving", "skill_icon_thieving.gif"),
    SLAYER(19, "Slayer", "skill_icon_slayer.gif"),
    FARMING(20, "Farming", "skill_icon_farming.gif"),
    RUNECRAFTING(21, "Runecrafting", "skill_icon_runecraft.gif"),
    HUNTER(22, "Hunter", "skill_icon_hunter.gif"),
    CONSTRUCTION(23, "Construction", "skill_icon_construction.gif");
    
    private int id;
    private String name;
    private String icon;
    private final String ICON_LOCATION = Core.class.getResource("\\resources\\hiscore\\").getPath();
    public static String hiscoreData;
    
    Hiscore(int id, String name, String icon) {
        this.id = id;
        this.name = name;
        this.icon = icon;
    }
    
    public int getId() {
        return id;
    }
    
    public String getName() {
        return name;
    }
    
    public int getRank() {
        String data = hiscoreData.split(" ")[id];
        return Integer.parseInt(data.split(",")[0]);
    }
    
    public int getLevel() {
        String data = hiscoreData.split(" ")[id];
        return Integer.parseInt(data.split(",")[1]);
    }
    
    public int getExperience() {
        String data = hiscoreData.split(" ")[id];
        return Integer.parseInt(data.split(",")[2]);
    }
    
    public String getIcon() {
        return ICON_LOCATION + icon;
    }
    }
    

1 个答案:

答案 0 :(得分:4)

因为你使用的是enum,所以没有什么区别:这与其他任何类的工作方式完全相同。即你的FXML看起来像

<TableView fx:id="table">
  <columns>
    <TableColumn fx:id="iconCol" text="Icon"/>
    <TableColumn fx:id="rankCol" text="Rank"/>
    <TableColumn fx:id="levelCol" text="Level"/>
    <TableColumn fx:id="experience" text="Experience"/>
  </columns>
</TableView>

然后在您的控制器中执行

public class Controller {

    @FXML
    private TableView<Hiscore> table ;
    @FXML
    private TableColumn<Hiscore, String> iconCol ;
    @FXML
    private TableColumn<Hiscore, Number> rankCol ;
    @FXML
    private TableColumn<Hiscore, Number> levelCol ;
    @FXML
    private TableColumn<Hiscore, Number> experienceCol ;

    public void initialize() {
        iconCol.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getIcon()));
        rankCol.setCellValueFactory(cellData -> new SimpleIntegerProperty(cellData.getValue().getRank()));
        // etc etc
    }
}

要将图标显示为图像,您需要iconCol上的单元工厂:

iconCol.setCellFactory(col -> new TableCell<Hiscore, String>() {
    private final ImageView imageView = new ImageView();
    @Override 
    protected void updateItem(String imageURL, boolean empty) {
        super.updateItem(image, empty) ;
        if (empty) {
            setGraphic(null);
        } else {
            Image image = new Image(imageURL, true);
            imageView.setImage(image);
            setGraphic(imageView);
        }
    }
});

这当然假设getIcon()方法正在返回图像的相应URL。 (ICON_LOCATION对我没有意义:你为什么在URL中有反斜杠?)

最后,您可以使用

填充表格
table.getItems().addAll(Hiscore.values());