Dea Alverina
Dea Alverina

Reputation: 11

Javafx TableView still selects the last row selected when the table is auto refreshed

I'm using Javafx TableView to select a row in the table and when the table is refreshed automatically (the table auto refresh per second), the row I selected is not selected.

What should I do to make the row that I select still selected even though the table is automatic refresh?

Here's my code:

Interface_adminController.java

public class Interface_adminController implements Initializable {
    @FXML
    private TableView<Antrian> antrianTable;
    @FXML
    private TableColumn<Antrian, Integer> id;
    @FXML
    private TableColumn<Antrian, String> tanggal;
    @FXML
    private TableColumn<Antrian, Integer> no_antrian;
    @FXML
    private TableColumn<Antrian, String> jam_ambil;
    @FXML
    private TableColumn<Antrian, String> jam_panggil;
    @FXML
    private TableColumn<Antrian, Boolean> boolean_antrian;
    @FXML
    private Button panggil_antrian;
    @FXML
    private Text txtHitung;
    @FXML
    private Text txtNoDipanggil;

    private Antrian antrian;

    ObservableList<Antrian> oblist = FXCollections.observableArrayList();

    Timeline updatePerdetik;
    int jumlahAntrian = 0;
    String antrianDipanggil = "";

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        oblist.clear();
        try {
            Connection conn = connection.connection();
            ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM antrian");

            while(rs.next()){
                oblist.add(new Antrian(rs.getInt("id"), 
                        rs.getString("tanggal"), 
                        rs.getInt("no_antrian"), 
                        rs.getString("jam_ambil"), 
                        rs.getString("jam_panggil"),
                        rs.getBoolean("boolean_antrian")));
            }
        } catch (SQLException ex) {
            Logger.getLogger(Interface_adminController.class.getName()).log(Level.SEVERE, null, ex);
        }

        System.out.println(no_antrian + " " + jam_ambil);
        id.setCellValueFactory(new PropertyValueFactory<>("id"));
        antrianTable.setItems(oblist);
        antrianTable.setPlaceholder(new Label("Tidak Ada Antrian"));

        antrianTable.setOnMouseClicked((MouseEvent event) -> {
            if(event.getButton().equals(MouseButton.PRIMARY)){
                antrianDipanggil = antrianTable.getSelectionModel().getSelectedItem().toString();
                txtNoDipanggil.setText(antrianDipanggil);
            }
        });

        updatePerdetik = new Timeline(new KeyFrame(Duration.seconds(1), new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                jumlahAntrian = antrianTable.getItems().size();
                txtHitung.setText(Integer.toString(jumlahAntrian));
                showItem();
                System.out.println("Jalan");
            }
        }));
        updatePerdetik.setCycleCount(Timeline.INDEFINITE);
        updatePerdetik.play();
    }
}

Upvotes: 0

Views: 638

Answers (1)

Vega
Vega

Reputation: 28738

/Posting in behalf of @Zephyr/

You can use

tableView.getSelectionModel().select() 

to re-select your row after the table refreshes. The select() method will accept a row index or Object parameter

Upvotes: 0

Related Questions