JAVAFX:如何在具有TableView中数据的单元格上设置MouseEvent左击动作

时间:2019-03-27 14:52:45

标签: java javafx

我是一名Java初学者,有一天想称自己为开发人员。我研究堆栈溢出已有一段时间,发现了许多解决方案和有用的信息。这是我的第一篇文章,所以请不要对我太苛刻。我试图自己解决问题,但失败了。

我试图使具有数据的TableView单元格对MouseEvent leftclick做出反应(当我单击特定的TableView单元格时,我希望它在具有数据的表下方的Im​​ageviewer中打开其他图像)。

这是针对Windows App的。 我的主控制器:

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

请帮助我解决我所缺少的代码,并尽可能具体。抱歉,如果我的后期编辑不太好,我会尽力而为,将来会有所改善。谢谢。

1 个答案:

答案 0 :(得分:0)

您可以设置一个单元工厂,然后将鼠标单击侦听器设置到每个单元:

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,但是您不必这样做。