簡體   English   中英

JAVAFX:如何在 TableView 中的帶有數據的單元格上設置 MouseEvent 左鍵單擊操作

[英]JAVAFX: How to set a MouseEvent leftclick action on a cell with data in TableView

我是一名初學 Java 的學生,希望有一天可以稱自己為開發人員。 我一直在研究stackoverflow,找到了許多解決方案和有用的信息。 這是我的第一篇文章,所以請不要對我太苛刻。 我試圖自己找到解決問題的方法,但失敗了。

我試圖讓我的帶有數據的 TableView 單元格對 MouseEvent 左鍵單擊做出反應(當我左鍵單擊特定的 TableView 單元格時,我希望它在帶有數據的表格下方的 Imageviewer 中打開不同的圖像)。

這是一個 Windows 應用程序。 我的主控制器:

package main;

import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

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

public class SpinChartsController implements Initializable {

    @FXML
    MenuItem close;

    @FXML
    TableView<TableValues> tableView;

    @FXML
    TableColumn<TableValues, String> btn;

    @FXML
    TableColumn<TableValues, String> sBvsBtnLimp;

    @FXML
    TableColumn<TableValues, String> sBvsBtn;

    @FXML
    TableColumn<TableValues, String> sBvsBB;

    @FXML
    TableColumn<TableValues, String> bBvsBtn;

    @FXML
    TableColumn<TableValues, String> bBvsBtnLimp;

    @FXML
    TableColumn<TableValues, String> bBvs2pp;

    @FXML
    TableColumn<TableValues, String> bBvs2ppLimp;

    @FXML
    TableColumn<TableValues, String> bBvsSB;

    @FXML
    TableColumn<TableValues, String> bBvsSBLimp;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

        //ustawia kolumny w tabeli
        btn.setCellValueFactory(new PropertyValueFactory<TableValues, String>("Btn"));
        sBvsBtnLimp.setCellValueFactory(new PropertyValueFactory<TableValues, String>("sBvsBtnLimp"));
        sBvsBtn.setCellValueFactory(new PropertyValueFactory<TableValues, String>("SBvsBtn"));
        sBvsBB.setCellValueFactory(new PropertyValueFactory<TableValues, String>("SBvsBB"));
        bBvsBtn.setCellValueFactory(new PropertyValueFactory<TableValues, String>("BBvsBtn"));
        bBvsBtnLimp.setCellValueFactory(new PropertyValueFactory<TableValues, String>("BBvsBtnLimp"));
        bBvs2pp.setCellValueFactory(new PropertyValueFactory<TableValues, String>("BBvs2pp"));
        bBvs2ppLimp.setCellValueFactory(new PropertyValueFactory<TableValues, String>("BBvs2ppLimp"));
        bBvsSB.setCellValueFactory(new PropertyValueFactory<TableValues, String>("BBvsSB"));
        bBvsSBLimp.setCellValueFactory(new PropertyValueFactory<TableValues, String>("BBvsSBLimp"));

        //dodanie danych do tabeli (dodawane wierszami)
        tableView.setItems(getDataForTheTable());

    }

    //metoda dodajaca dane do tabeli wierszami
    public ObservableList<TableValues> getDataForTheTable() {
        ObservableList<TableValues> rowsValues = FXCollections.observableArrayList();
        rowsValues.add(new TableValues("", "", "vs AI", "", "vs AI", "",
                "vs AI", "", "vs AI", ""));
        rowsValues.add(new TableValues("25+", "", "", "", "", "",
                "", "", "25+", "25+"));
        rowsValues.add(new TableValues("20-25", "20+", "20+", "20+", "20+", "20+",
                "20+", "20+", "20-25", "20-25"));
        rowsValues.add(new TableValues("16-20", "16-20", "16-20", "16-20", "16-20", "16-20",
                "16-20", "16-20", "16-20", "16-20"));
        rowsValues.add(new TableValues("13-16", "13-16", "13-16", "13-16", "13-16", "13-16",
                "13-16", "13-16", "13-16", "13-16"));
        rowsValues.add(new TableValues("10-13", "10-13", "10-13", "10-13", "10-13", "10-13",
                "10-13", "10-13", "10-13", "10-13"));
        rowsValues.add(new TableValues("9", "8-10", "", "8-10", "8-10", "8-10",
                "8-10", "8-10", "8-10", "8-10"));
        rowsValues.add(new TableValues("8", "6-8", "", "6-8", "vs 3x 20-25", "",
                "", "", "vs 2.5x-3x 20-25", "6-8"));
        rowsValues.add(new TableValues("<8", "", "", "4-6", "", "",
                "", "", "", "4-6"));
        rowsValues.add(new TableValues("", "", "", "", "", "",
                "", "", "", ""));
        rowsValues.add(new TableValues("ODDS", "", "", "", "", "",
                "", "", "", ""));

        return rowsValues;
    }

    public void closeProgram() {
        Platform.exit();
        System.exit(0);
    }
}

我的 TableValues 類:

package main;

import javafx.beans.property.SimpleStringProperty;
public class TableValues {

    //uzywam SimpleStringProperty do TableView bo ma listenery i moge potem z tym cos zrobic
    private SimpleStringProperty Btn;
    private SimpleStringProperty SBvsBtnLimp;
    private SimpleStringProperty SBvsBtn;
    private SimpleStringProperty SBvsBB;
    private SimpleStringProperty BBvsBtn;
    private SimpleStringProperty BBvsBtnLimp;
    private SimpleStringProperty BBvs2pp;
    private SimpleStringProperty BBvs2ppLimp;
    private SimpleStringProperty BBvsSB;
    private SimpleStringProperty BBvsSBLimp;

    //taki konstruktor by bylo latwiej, beda podawane Stringi a on bedzie sobie sam zamienial
    public TableValues(String btn, String SBvsBtnLimp, String SBvsBtn, String SBvsBB, String BBvsBtn,
                       String BBvsBtnLimp, String BBvs2pp, String BBvs2ppLimp, String BBvsSB, String BBvsSBLimp) {
        this.Btn = new SimpleStringProperty(btn);
        this.SBvsBtnLimp = new SimpleStringProperty(SBvsBtnLimp);
        this.SBvsBtn = new SimpleStringProperty(SBvsBtn);
        this.SBvsBB = new SimpleStringProperty(SBvsBB);
        this.BBvsBtn = new SimpleStringProperty(BBvsBtn);
        this.BBvsBtnLimp = new SimpleStringProperty(BBvsBtnLimp);
        this.BBvs2pp = new SimpleStringProperty(BBvs2pp);
        this.BBvs2ppLimp = new SimpleStringProperty(BBvs2ppLimp);
        this.BBvsSB = new SimpleStringProperty(BBvsSB);
        this.BBvsSBLimp = new SimpleStringProperty(BBvsSBLimp);
    }

    public String getBtn() {
        return Btn.get();
    }

    public void setBtn(String btn) {
        this.Btn.set(btn);
    }

    public String getSBvsBtnLimp() {
        return SBvsBtnLimp.get();
    }

    public void setSBvsBtnLimp(String SBvsBTNLimp) {
        this.SBvsBtnLimp.set(SBvsBTNLimp);
    }

    public String getSBvsBtn() {
        return SBvsBtn.get();
    }

    public void setSBvsBtn(String SBvsBtn) {
        this.SBvsBtn.set(SBvsBtn);
    }

    public String getSBvsBB() {
        return SBvsBB.get();
    }

    public void setSBvsBB(String SBvsBB) {
        this.SBvsBB.set(SBvsBB);
    }

    public String getBBvsBtn() {
        return BBvsBtn.get();
    }

    public void setBBvsBtn(String BBvsBtn) {
        this.BBvsBtn.set(BBvsBtn);
    }

    public String getBBvsBtnLimp() {
        return BBvsBtnLimp.get();
    }

    public void setBBvsBtnLimp(String BBvsBtnLimp) {
        this.BBvsBtnLimp.set(BBvsBtnLimp);
    }

    public String getBBvs2pp() {
        return BBvs2pp.get();
    }

    public void setBBvs2pp(String BBvs2pp) {
        this.BBvs2pp.set(BBvs2pp);
    }

    public String getBBvs2ppLimp() {
        return BBvs2ppLimp.get();
    }

    public void setBBvs2ppLimp(String BBvs2ppLimp) {
        this.BBvs2ppLimp.set(BBvs2ppLimp);
    }

    public String getBBvsSB() {
        return BBvsSB.get();
    }

    public void setBBvsSB(String BBvsSB) {
        this.BBvsSB.set(BBvsSB);
    }

    public String getBBvsSBLimp() {
        return BBvsSBLimp.get();
    }

    public void setBBvsSBLimp(String BBvsSBLimp) {
        this.BBvsSBLimp.set(BBvsSBLimp);
    }
}

我的 fxml 文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.StackPane?>

<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0"
           prefWidth="1000.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1"
           fx:controller="main.SpinChartsController">
    <children>
        <MenuBar StackPane.alignment="TOP_LEFT">
            <menus>
                <Menu mnemonicParsing="false" text="File">
                    <items>
                        <MenuItem fx:id="close" mnemonicParsing="false" onAction="#closeProgram" text="Close"/>
                    </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Help">
                    <items>
                        <MenuItem mnemonicParsing="false" text="About"/>
                    </items>
                </Menu>
            </menus>
        </MenuBar>
        <TableView fx:id="tableView" maxHeight="-Infinity" maxWidth="-Infinity"
                   minHeight="-Infinity" minWidth="-Infinity" prefHeight="331.0" prefWidth="884.0"
                   StackPane.alignment="TOP_LEFT">
            <columns>
                <TableColumn fx:id="btn" prefWidth="67.99999463558197" sortable="false" text="BTN"/>
                <TableColumn fx:id="sBvsBtnLimp" prefWidth="102.40003204345703" sortable="false" text="SB vs BTN Limp"/>
                <TableColumn fx:id="sBvsBtn" prefWidth="76.00001525878906" sortable="false" text="SB vs BTN"/>
                <TableColumn fx:id="sBvsBB" prefWidth="72.79998779296875" sortable="false" text="SB vs BB"/>
                <TableColumn fx:id="bBvsBtn" prefWidth="84.0" sortable="false" text="BB vs BTN"/>
                <TableColumn fx:id="bBvsBtnLimp" prefWidth="102.4000244140625" sortable="false" text="BB vs BTN Limp"/>
                <TableColumn fx:id="bBvs2pp" prefWidth="84.80001831054688" sortable="false" text="BB vs 2pp"/>
                <TableColumn fx:id="bBvs2ppLimp" prefWidth="101.5999755859375" sortable="false" text="BB vs 2pp Limp"/>
                <TableColumn fx:id="bBvsSB" prefWidth="79.19989013671875" sortable="false" text="BB vs SB"/>
                <TableColumn fx:id="bBvsSBLimp" prefWidth="108.800048828125" sortable="false" text="BB vs SB Limp"/>
            </columns>
            <StackPane.margin>
                <Insets left="50.0" right="50.0" top="50.0"/>
            </StackPane.margin>
        </TableView>
    </children>
</StackPane>

我的主課:

package main;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class SpinCharts extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(this.getClass().getResource("fxmlfiles/spinCharts.fxml"));

        StackPane stackPane = loader.load();
        Scene scene = new Scene(stackPane);

        primaryStage.getIcons().add(new Image("main/diamond.png"));
        primaryStage.setOnCloseRequest(event -> {
            SpinChartsController spinCC = new SpinChartsController();
            spinCC.closeProgram();
        });
        primaryStage.setHeight(800d);
        primaryStage.setWidth(1000d);
        primaryStage.setMinHeight(600d);
        primaryStage.setMinWidth(800d);
        primaryStage.setScene(scene);
        primaryStage.setTitle("SpinCharts");
        primaryStage.show();
    }

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

請幫助我解決我丟失的代碼並盡可能具體。 對不起,如果我的后期編輯不太好,正在努力,將來會改進。 謝謝。

您可以設置一個單元工廠,然后為每個單元設置鼠標單擊偵聽器:

column.setCellFactory(param -> new TableCell<TableValues, String>() {
    @Override
    public void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);
        if (item == null || empty) {
            setText(null);
            setCursor(Cursor.DEFAULT);
            setOnMouseClicked(null);
        } else {
            setText(item);
            setCursor(Cursor.CLOSED_HAND);
            setOnMouseClicked(event -> {
                System.out.println("Clicked on " + item);
                // do what you want to do on click
            });
        }
    }
});

您必須將單元工廠設置為表格中的每一列,就像您已經使用PropertyValueFactory 在該示例代碼中,如果單元格可點擊,我還將光標設置為CLOSED_HAND ,但您不必這樣做。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM