2015-05-10 21 views
0

我正在使用JDBC和MySQL。如何直接在javafx TableView中创建新记录?

我可以创建文本字段并将其内容作为记录输入MySQL表(然后填充相应的javafx TableView)。

我想知道用户是否可以通过单击TableView单元直接将新记录添加到TableView。

这样做会不会是一个好习惯? TableView显示销售发票的详细信息,如项目名称,销售数量,费率等。

+0

你会在这里找到你的答案:http://docs.oracle.com/javafx/2/ui_controls/table-view.htm –

回答

1

前段时间我发布了此示例,但现在找不到它。

这样做的一种方法是在表格项目列表的末尾放置一个“空白”项目。注册一个监听器,以便如果用户编辑该值,则会在最后添加一个新的空白项目。

下面是一个例子(它也有一些与默认不同的编辑功能)。

import java.util.function.Function; 

import javafx.application.Application; 
import javafx.beans.binding.Bindings; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.beans.property.StringProperty; 
import javafx.beans.value.ChangeListener; 
import javafx.beans.value.ObservableValue; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.TableCell; 
import javafx.scene.control.TableColumn; 
import javafx.scene.control.TableView; 
import javafx.scene.control.cell.TextFieldTableCell; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 
import javafx.util.Callback; 

public class TableViewSingleClickEditTest extends Application { 


    @Override 
    public void start(Stage primaryStage) { 


     TableView<Person> table = new TableView<>(); 
     table.setEditable(true); 

     TableColumn<Person, String> firstNameCol = createCol("First Name", Person::firstNameProperty, 150); 
     TableColumn<Person, String> lastNameCol = createCol("Last Name", Person::lastNameProperty, 150); 
     TableColumn<Person, String> emailCol = createCol("Email", Person::emailProperty, 200); 

     TableColumn<Person, Void> indexCol = new TableColumn<>(""); 
     indexCol.setCellFactory(col -> { 
      TableCell<Person, Void> cell = new TableCell<>(); 
      cell.textProperty().bind(Bindings.createStringBinding(() -> { 
       if (cell.getIndex() >= 0 && cell.getIndex() < table.getItems().size() - 1) { 
        return Integer.toString(cell.getIndex() + 1); 
       } else if (cell.getIndex() == table.getItems().size() - 1) { 
        return "*" ; 
       } else return "" ; 
      }, cell.indexProperty(), table.getItems())); 
      return cell ; 
     }); 
     indexCol.setPrefWidth(32); 

     table.getItems().addAll(
      new Person("Jacob", "Smith", "[email protected]"), 
      new Person("Isabella", "Johnson", "[email protected]"), 
      new Person("Ethan", "Williams", "[email protected]"), 
      new Person("Emma", "Jones", "[email protected]"), 
      new Person("Michael", "Brown", "[email protected]") 
     ); 

     ChangeListener<String> lastPersonTextListener = new ChangeListener<String>() { 
      @Override 
      public void changed(ObservableValue<? extends String> obs, String oldText, String newText) { 
       if (oldText.isEmpty() && ! newText.isEmpty()) { 
        Person lastPerson = table.getItems().get(table.getItems().size() - 1); 
        lastPerson.firstNameProperty().removeListener(this); 
        lastPerson.lastNameProperty().removeListener(this); 
        lastPerson.emailProperty().removeListener(this); 
        Person newBlankPerson = new Person("", "", ""); 
        newBlankPerson.firstNameProperty().addListener(this); 
        newBlankPerson.lastNameProperty().addListener(this); 
        newBlankPerson.emailProperty().addListener(this); 
        table.getItems().add(newBlankPerson); 
       } 
      } 
     }; 

     Person blankPerson = new Person("", "", ""); 
     blankPerson.firstNameProperty().addListener(lastPersonTextListener); 
     blankPerson.lastNameProperty().addListener(lastPersonTextListener); 
     blankPerson.emailProperty().addListener(lastPersonTextListener); 
     table.getItems().add(blankPerson); 

     table.getColumns().add(indexCol); 
     table.getColumns().add(firstNameCol); 
     table.getColumns().add(lastNameCol); 
     table.getColumns().add(emailCol); 

     VBox root = new VBox(15, table); 
     root.setAlignment(Pos.CENTER); 
     Scene scene = new Scene(root, 800, 600); 

     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 


    private TableColumn<Person, String> createCol(String title, 
      Function<Person, ObservableValue<String>> mapper, double size) { 

     TableColumn<Person, String> col = new TableColumn<>(title); 

     col.setCellValueFactory(cellData -> mapper.apply(cellData.getValue())); 

     Callback<TableColumn<Person, String>, TableCell<Person, String>> defaultCellFactory 
      = TextFieldTableCell.forTableColumn(); 

     col.setCellFactory(column -> { 
      TableCell<Person, String> cell = defaultCellFactory.call(column); 
      cell.setOnMouseClicked(e -> { 
       if (! cell.isEditing() && ! cell.isEmpty()) { 
        cell.getTableView().edit(cell.getIndex(), column); 
       } 
      }); 
      return cell ; 
     }); 

     col.setPrefWidth(size); 


     return col ; 
    } 

    public class Person { 
     private final StringProperty firstName = new SimpleStringProperty(this, "firstName"); 
     private final StringProperty lastName = new SimpleStringProperty(this, "lastName"); 
     private final StringProperty email = new SimpleStringProperty(this, "email"); 

     public Person(String firstName, String lastName, String email) { 
      this.firstName.set(firstName); 
      this.lastName.set(lastName); 
      this.email.set(email); 
     } 

     public final StringProperty firstNameProperty() { 
      return this.firstName; 
     } 

     public final java.lang.String getFirstName() { 
      return this.firstNameProperty().get(); 
     } 

     public final void setFirstName(final java.lang.String firstName) { 
      this.firstNameProperty().set(firstName); 
     } 

     public final StringProperty lastNameProperty() { 
      return this.lastName; 
     } 

     public final java.lang.String getLastName() { 
      return this.lastNameProperty().get(); 
     } 

     public final void setLastName(final java.lang.String lastName) { 
      this.lastNameProperty().set(lastName); 
     } 

     public final StringProperty emailProperty() { 
      return this.email; 
     } 

     public final java.lang.String getEmail() { 
      return this.emailProperty().get(); 
     } 

     public final void setEmail(final java.lang.String email) { 
      this.emailProperty().set(email); 
     } 

    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
}