M
M
MarkusEfr2019-02-08 17:50:53
Java
MarkusEfr, 2019-02-08 17:50:53

JavaFX how to keep inner elements across scene transitions?

I am writing a chat application in javafx and faced the task of preserving the internal elements of the container during transitions between scenes. In my case it is as follows : after creating a chat room (Class Room)

public class Room {
  public int id;
  public String roomName;
  public static int lastId = 0;

  public Room(String roomName) {
    this.id = lastId;
    this.setRoomName(roomName);
    lastId++;
  }
  public Room(int id, String roomName) {
    this.id = id;
    this.setRoomName(roomName);
    lastId++;
  }
  public Room() {
    this.id = lastId;
    this.setRoomName(roomName);
    lastId++;
  }
  @Override
  public String toString() {
    return this.getRoomName();
  }
  
  public int getId() {
    return id;
  }

  public String getRoomName() {
    return roomName;
  }

  public void setRoomName(String roomName) {
    this.roomName = roomName;
  }
}

(creation method)
@FXML
  public void createRoom() {
    Room room = new Room("Room #");
    room.setRoomName(room.roomName + room.getId());
    rooms.getItems().add(room);
        Connect con = new Connect();
        con.createRoom(room.roomName);
  }

5c5d95a26ef5c566091753.jpeg
I put Room objects on creation in a given ListView
@FXML
  public ListView<Room> rooms = new ListView<Room>();
after going to the chat scene (by clicking on a room from the list) 5c5d962a54dfd906349721.jpegand back to the menu where the rooms are - they disappear from the list. I tried to put them back during the transition and store them in the database and then take them from it, but the attempts failed.
How can we solve this problem of missing rooms?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MarkusEfr, 2019-02-09
@MarkusEfr

I decided by saving the names of the rooms in a separate list and displaying them back after switching with the room update button, but this solution is clearly not the best

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question