JavaFX隐藏id列

时间:2016-05-28 00:51:07

标签: java javafx tableview

我的问题是我需要用户无法从tableview中的数据库知道“id_column”,但我需要它能够从所选行中删除/更新汽车。

示例:

  
      
  • 用户在tableview中选择带有汽车的行并按“delete_button”。现在我有问题,因为我需要id来删除它   来自数据库的汽车(这里可以有多辆具有相同属性的汽车)   但是不允许用户查看该列。
  •   
id || name || cost
1     car1    34.30
2     car1    34.30
3     car2    34.30
4     car3    55.00

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

任何想法?

修改

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");
       }
    }

}

现在看起来应该是这样吗?

2 个答案:

答案 0 :(得分:1)

supp.setOnAction(e-> {

  table.getColums.remove(tableColumn);

});

答案 1 :(得分:0)

1)在表控制器类中创建对要删除的汽车的引用:

<activity
    android:name="MainActivity"
    android:screenOrientation="portrait"
    android:configChanges="keyboardHidden|orientation|screenSize">

2)向表中添加一个选择监听器,如下所示:

private CarObject carToDelete = null;

3)现在,当他们点击删除按钮时,只需访问carToDelete参考:

@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;
        }
    });
}