前提・実現したいこと
現在、TableViewにMySQLのテーブルの中身を挿入しています。
そこで私は、TableViewのカラムに削除用のチェックボックスを追加しました。
で、チェックボックスが押された行の値を取得することで、それにあたるデータの削除を実現させたいです。
そこで、チェックボックスで選択した行の値を取得する方法があれば教えていただきたいです。
色々調べてみましたが、分かりませんでした。
該当のソースコード
package application;
import java.io.IOException;
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.BooleanProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleBooleanProperty;
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.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.util.converter.IntegerStringConverter;
public class SampleController {
private Connection connection = null; private PreparedStatement ps = null; private ResultSet rs = null; @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 private TableColumn<dataClass, Boolean> discardColumn; @FXML private Button btsyukko; @FXML private Button btupdate; @FXML private Button btnyuuko; @FXML void clicknyuuko(ActionEvent event) { //入庫画面 try { Parent parent = FXMLLoader.load(getClass().getResource("Nyuuko.fxml")); Scene scene = new Scene(parent); Stage stage = new Stage(); stage.setScene(scene); // 以下の2行を加えると、モーダルopenになる stage.initModality(Modality.WINDOW_MODAL); stage.initOwner(((Node)event.getSource()).getScene().getWindow()); stage.show(); }catch(IOException e) { e.printStackTrace(); }finally { closeSQL(connection, ps, rs); } } @FXML void clicksyukko(ActionEvent event) { //出庫画面 try { Parent parent = FXMLLoader.load(getClass().getResource("Syukko.fxml")); Scene scene = new Scene(parent); Stage stage = new Stage(); stage.setScene(scene); // 以下の2行を加えると、モーダルopenになる stage.initModality(Modality.WINDOW_MODAL); stage.initOwner(((Node)event.getSource()).getScene().getWindow()); stage.show(); }catch(IOException e) { e.printStackTrace(); }finally { closeSQL(connection, ps, rs); } } //更新 @FXML void clickupdate(ActionEvent event) { table.getItems().setAll(DB()); } @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'."; assert discardColumn != null : "fx:id=\"discardColumn\" was not injected: check your FXML file 'Sample.fxml'."; assert btsyukko != null : "fx:id=\"btsyukko\" was not injected: check your FXML file 'Sample.fxml'."; assert btupdate != null : "fx:id=\"btupdate\" was not injected: check your FXML file 'Sample.fxml'."; assert btnyuuko != null : "fx:id=\"btnyuuko\" 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")); //discardColumn.setCellValueFactory(new PropertyValueFactory<dataClass, Boolean>("discard")); //編集可能 table.setEditable(true); //編集可能にするためにセルにTextFieldを埋め込む nameColumn.setCellFactory(TextFieldTableCell.forTableColumn()); //Integerを可能にする priceColumn.setCellFactory(TextFieldTableCell.<dataClass, Integer>forTableColumn(new IntegerStringConverter())); zaikoColumn.setCellFactory(TextFieldTableCell.<dataClass, Integer>forTableColumn(new IntegerStringConverter())); //チェックボックス埋め込み discardColumn.setCellFactory(CheckBoxTableCell.forTableColumn(discardColumn)); 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 BooleanProperty discard = new SimpleBooleanProperty(); public BooleanProperty discardProperty() { return discard; } public void setdiscard(Boolean val) { discard.set(val); } } private ObservableList<dataClass> DB() { 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; } 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(); } }
}
回答1件
あなたの回答
tips
プレビュー