jodekpotasu
jodekpotasu

Reputation: 515

JavaFX hide id column

My problem is that i need that user can't know "id_column" from database in tableview but i need it to be able to delete/update car from selected row.

Example:

  • User is selecting some row with car in tableview and push "delete_button". Now i have problem because i need id to delete that car from database (here can be more than one car with same attributes) but user can't be allow to see that column.
id || name || cost
1     car1    34.30
2     car1    34.30
3     car2    34.30
4     car3    55.00

to

name || cost
car1    34.30
car1    34.30
car2    34.30
car3    55.00

any ideas?

EDIT

import java.net.URL;
import java.util.ResourceBundle;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

public class HomeController implements Initializable {
    private Student studentToDelete = null;

    @FXML private TableView<Student> table;
    @FXML private TableColumn<Student, Integer> id;
    @FXML private TableColumn<Student, String> name;
    @FXML private TableColumn<Student, String> surname;
    @FXML private TableColumn<Student, Integer> age;


    public ObservableList<Student> list = FXCollections.observableArrayList(
            new Student(1,"dupa","nazwisko",32),
            new Student(2,"afuj","nazwisko",23)
            );
    DataBase db = new DataBase();
    public ObservableList<Student> lista = FXCollections.observableArrayList(db.getAllStudents());



    @Override
    public void initialize(URL location, ResourceBundle resources) {
        id.setCellValueFactory(new PropertyValueFactory<Student, Integer>("id"));
        name.setCellValueFactory(new PropertyValueFactory<Student, String>("name"));
        surname.setCellValueFactory(new PropertyValueFactory<Student, String>("surname"));
        age.setCellValueFactory(new PropertyValueFactory<Student, Integer>("age"));
        table.setItems(lista);

    }

    @FXML
    private void initialize() {

    // Listen for selection changes
    table.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Student>() {



            @Override
            public void changed(ObservableValue<? extends Student> observable, Student oldValue, Student newValue) {

                studentToDelete = newValue;
            }

        });
     }

    @FXML
    private void deleteClicked () // or whatever your onAction handler is
    {
       if (studentToDelete != null)
       {
           System.out.println("test");
       }
    }

}

Now it should look like this?

Upvotes: 1

Views: 1548

Answers (2)

supp.setOnAction(e-> {

  table.getColums.remove(tableColumn);

});

Upvotes: 1

Michael Markidis
Michael Markidis

Reputation: 4191

1) Create a reference to the car you want to delete in your table controller class:

private CarObject carToDelete = null;

2) Add a selection listener to your table like so:

@FXML
private void initialize() {

// Listen for selection changes
yourTable.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<CarObject>() {

        @Override
        public void changed(ObservableValue<? extends CarObject> observable,
                CarObject oldValue, CarObject newValue)
        {
                 carToDelete = newValue;
        }
    });
}  

3) Now, when they click the delete button, just access the carToDelete reference:

@FXML
private void deleteClicked () // or whatever your onAction handler is
{
   if (carToDelete != null)
   {
       // get the id from carToDelete and delete it...
   }
}

Upvotes: 0

Related Questions