I've been trying to set different Data for each of my entries to one of my fxml's in scenebuilder that I made to look like a "tile/widget" that goes into the main menu through FXMLLoader and i have made that happen with any media I needed except redirecting to different websites. Now i added a button to my fxml which I needs to have different entry for each of the "tiles" i want to create on my main menu screen. I used this method to set my data till now but i needed a method to activate the button going to different websites depending on what i add to the main controller.
public class HotelController {
@FXML
private ImageView img;
@FXML
private Label startingprice;
@FXML
private Label hotelname;
@FXML
private Button hotelsURL;
public void setData(Hotel hotel){
Image image = new Image(getClass().getResourceAsStream(hotel.getImgSrc()));
img.setImage(image);
hotelname.setText(hotel.getName());
//days.setText(hotel.getNbDays()+"Days");
startingprice.setText(hotel.getPrice()+"€ a night");
hotelsURL.setText(hotel.getLink());
}
@FXML
void hotelURLpressed(ActionEvent event){
Desktop browser = Desktop.getDesktop();
try{
browser.browse(new URI(hotel.getLink()));
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
}
finally{}
}
}
The above code is the code I use in the controller of the tiles which has the problem in the second snippet i added at browser.browse(new URI(hotel.getLink()));. I cannot find a way other than making the whole thing static at the "scene.java" file but then the links do not change.
The code i use to change the links for every entry is:
private List<Hotel> getHotels() {
List<Hotel> ls = new ArrayList<>();
Hotel hotel = new Hotel();
hotel.setName("Hotel1");
hotel.setImgSrc("/img/image1.jpg");
hotel.setPrice(36);
hotel.setLink("www.link1.com");
//hotel.setNbDays(1);
ls.add(hotel);
hotel = new Hotel();
hotel.setName("Hotel2");
hotel.setImgSrc("/img/image2.jpg");
hotel.setPrice(45);
hotel.setLink("www.link2.com");
ls.add(hotel);
return ls ;
}
Also the code for the getters and setters at my "scene.java" is
package scenes;
public class Hotel {
private String name;
private String imgSrc;
private int price;
private String link;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImgSrc() {
return imgSrc;
}
public void setImgSrc(String imgSrc) {
this.imgSrc = imgSrc;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
}
Simply create a
hotelinstance variable in the controller, and assign it in thesetData(...)method, so it can be accessed in the event handler: