Reputation: 418
I'm working on an Javafx Application for a game. I got some Data stored in an enum but can't seem to figure out how to easily add the data to JavaFX TableView, can someone help me out. I'll be using fxml to style the TableView.
I would like every enum vallue in an seperate row, in the columns i would like to have:
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;
}
}
Upvotes: 4
Views: 3468
Reputation: 209408
There's really no difference because you are using an enum
: this just works exactly the same way as it would for any other class. I.e your FXML looks like
<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>
Then in your controller you do
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
}
}
To display the icon as an image, you need a cell factory on the 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);
}
}
});
This of course assumes that the getIcon()
method is returning the appropriate URL for the image. (ICON_LOCATION
makes no sense to me: why do you have backslashes in a URL?)
Finally, you can populate the table with
table.getItems().addAll(Hiscore.values());
Upvotes: 7