前提・実現したいこと
TableViewの編集可能にし、編集はできるのですが、他のカラムに移動するとデータが戻ってしまいます。戻らないようにすればどうすればいいのでしょうか?
該当のソースコード
package application;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.util.converter.IntegerStringConverter;
public class SampleController {
@FXML private ResourceBundle resources; @FXML private URL location; @FXML private TableView<dataClass> table; @FXML private TableColumn<dataClass, String> nameColumn; @FXML private TableColumn<dataClass, Integer> priceColumn; @FXML private TableColumn<dataClass, Integer> zaikoColumn; @FXML void initialize() { assert table != null : "fx:id=\"table\" was not injected: check your FXML file 'Sample.fxml'."; assert nameColumn != null : "fx:id=\"nameColumn\" was not injected: check your FXML file 'Sample.fxml'."; assert priceColumn != null : "fx:id=\"priceColumn\" was not injected: check your FXML file 'Sample.fxml'."; assert zaikoColumn != null : "fx:id=\"zaikoColumn\" was not injected: check your FXML file 'Sample.fxml'."; nameColumn.setCellValueFactory(new PropertyValueFactory<dataClass, String>("name")); priceColumn.setCellValueFactory(new PropertyValueFactory<dataClass, Integer>("price")); zaikoColumn.setCellValueFactory(new PropertyValueFactory<dataClass, Integer>("zaiko")); //編集可能 table.setEditable(true); nameColumn.setEditable(true); priceColumn.setEditable(true); zaikoColumn.setEditable(true); //編集可能にするためにセルにTextFieldを仕込む nameColumn.setCellFactory(TextFieldTableCell.forTableColumn()); //Integerを可能にする priceColumn.setCellFactory(TextFieldTableCell.<dataClass, Integer>forTableColumn(new IntegerStringConverter())); zaikoColumn.setCellFactory(TextFieldTableCell.<dataClass, Integer>forTableColumn(new IntegerStringConverter())); table.getItems().setAll(DB()); } public class dataClass { private StringProperty name = new SimpleStringProperty(); public StringProperty nameProperty() { return name; } public void setname(String val) { name.set(val); } // private IntegerProperty price = new SimpleIntegerProperty(); public IntegerProperty priceProperty() { return price; } public void setprice(int val) { price.set(val); } // private IntegerProperty zaiko = new SimpleIntegerProperty(); public IntegerProperty zaikoProperty() { return zaiko; } public void setzaiko(int val) { zaiko.set(val); } } private ObservableList<dataClass> DB() { Connection connection = null; PreparedStatement ps = null; ResultSet rs = null; ObservableList<dataClass> data = FXCollections.observableArrayList(); try { connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/orderdb?characterEncoding=UTF-8&serverTimezone=JST", "", ""); ps = connection.prepareStatement("select * from product"); rs = ps.executeQuery(); while(rs.next()) { //1の部分はカラム番号 String name = rs.getString(1); int price = rs.getInt(2); int zaiko = rs.getInt(3); dataClass column = new dataClass(); column.setname(name); column.setprice(price); column.setzaiko(zaiko); data.add(column); } // データベースとの接続に失敗した場合 }catch(SQLException ex) { ex.printStackTrace(); System.err.println("MySQLに接続できませんでした。"); }finally { closeSQL(connection, ps, rs); } return data; } static void closeSQL(Connection connection, PreparedStatement pre_statement, ResultSet result_set) { try { // データベースとの接続を解除 if (connection != null) { connection.close(); } if (pre_statement != null) { pre_statement.close(); } if (result_set != null) { result_set.close(); } }catch(SQLException ex) { System.err.println("Error closeSql Func"); //どこで、エラーが起こっているかが分かる ex.printStackTrace(); } }
}
あなたの回答
tips
プレビュー