简体   繁体   English

如何处理表格视图中的按钮单击事件以创建超链接

[英]How to handle Button click event from Table view to create Hyperlink

I have two columns, one is Button which is the one that I click and the other is Hyperlink which should update after the Link created Button action event. 我有两列,一列是Button ,这是我单击的一列,另一列是“ 超链接” ,应在“ 链接创建了 Button”操作事件后更新。 I was try with method 我尝试过方法

  Param item = (Param) getTableRow().getItem();
                item.setHyperlink("Link is created");

But it doesn't work. 但这是行不通的。

Please help me how to create hyperlink into Hyperlink column after the Button same row action event. 请帮助我如何在Button相同行操作事件之后将超链接创建为“ 超链接”列。 It look like as below enter image description here 如下图所示 在此处输入图片说明

//Here is Class Controller //这里是类控制器

public class SampleController {

@FXML
private TableColumn<Param, Hyperlink> hyperlinkcol;

@FXML
private TableView<Param> table;

@FXML
private TableColumn<Param, String> buttoncol;

@FXML
private TableColumn<Param, String> descriptioncol;
public static ObservableList<Param> data = FXCollections.observableArrayList();
@FXML
private void initialize() {
    hyperlinkcol.setCellValueFactory(new PropertyValueFactory<>("hyperlink"));
    buttoncol.setCellValueFactory(new PropertyValueFactory<>("hyperlink"));
    descriptioncol.setCellValueFactory(new PropertyValueFactory<>("Description"));
    buttoncol.setCellFactory(new Buttoncell());
    data.add(new Param("David"));
    data.add(new Param("Marry"));
    table.setItems(data);
} }

//Here is Class Param //这里是Class Param

public class Param {
private String Description;
private Hyperlink hyperlink;
public Param(String description) {
    super();
    Description = description;

}
public String getDescription() {
    return Description;
}
public void setDescription(String description) {
    Description = description;
}
public Hyperlink getHyperlink() {
    return hyperlink;
}
public void setHyperlink(String hyperlink) {
    this.hyperlink = new Hyperlink(hyperlink);
}
}

// Here is Class Buttoncell //这是Class Buttoncell

 public class Buttoncell implements Callback<TableColumn<Param, String>, 
TableCell<Param, String>> {

@Override
public TableCell<Param, String> call(TableColumn<Param, String> arg) {

    TableCell<Param, String> cell = new TableCell<Param, String>() {

        private final Button button;

        {
            button = new Button();
            button.setOnAction(evt -> {
                Param item = (Param) getTableRow().getItem();
                item.setHyperlink("Link is created");
            });
        }

        @Override
        protected void updateItem(String item, boolean empty) {
            if (empty) {

                setGraphic(null);
            } else {

                if (item != null && !item.isEmpty()) {

                    button.setText("Click");
                } else {
                    button.setText("Link created");
                }
                setGraphic(button);
            }
        }
    };
    return cell;
} }

This code demos what you are asking. 该代码演示了您的要求。 This code creates the Button's cellFactory in the Controller . 这段代码在Controller创建Button's cellFactory New Updates to the Controller class and the Param class! Controller类和Param类的新更新!

Full Code: 完整代码:

Main Class 主班

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication215 extends Application
{

    @Override
    public void start(Stage stage) throws Exception
    {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        Scene scene = new Scene(root);

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

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}

Controller 控制者

import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.util.Callback;

/**
 *
 * @author blj0011
 */
public class FXMLDocumentController implements Initializable
{

    @FXML
    private TableColumn<Param, String> hyperlinkcol, descriptioncol, buttoncol;
    @FXML
    private TableView<Param> table;

    @Override
    public void initialize(URL url, ResourceBundle rb)
    {
        // TODO

        descriptioncol.setCellValueFactory(new PropertyValueFactory<>("description"));
        hyperlinkcol.setCellValueFactory(new PropertyValueFactory<>("hyperlink"));

        Callback<TableColumn<Param, String>, TableCell<Param, String>> cellFactory0
                = (final TableColumn<Param, String> entry) -> {
                    final TableCell<Param, String> cell = new TableCell<Param, String>()
            {

                Hyperlink hyperlink = new Hyperlink();

                @Override
                public void updateItem(String item, boolean empty)
                {
                    super.updateItem(item, empty);
                    if (empty) {
                        setGraphic(null);
                        setText(null);
                    }
                    else {
                        Param tempParam = table.getItems().get(getIndex());
                        if (tempParam.isBtnState()) {
                            System.out.println("set hyperlink");
                            hyperlink.setText(item);
                            hyperlink.setOnAction((event) -> {
                                System.out.println("Go to URL");
                            });
                            setGraphic(hyperlink);
                            setText(null);
                        }
                        else {
                            hyperlink.setText("");
                            setGraphic(null);
                            setText(null);
                        }
                    }
                }
            };
                    return cell;
                };
        hyperlinkcol.setCellFactory(cellFactory0);

        Callback<TableColumn<Param, String>, TableCell<Param, String>> cellFactory
                = (TableColumn<Param, String> param) -> {
                    final TableCell<Param, String> cell = new TableCell<Param, String>()
            {
                final Button btn = new Button();

                @Override
                public void updateItem(String item, boolean empty)
                {
                    super.updateItem(item, empty);
                    if (empty) {
                        setGraphic(null);
                        setText(null);
                    }
                    else {
                        int currentIndex = getIndex();
                        System.out.println("current index: " + currentIndex);
                        Param param = table.getItems().get(currentIndex);
                        btn.setText(param.getButtonText());

                        btn.setOnAction(event -> {
                            param.setBtnState(true);
                            param.setButtonText(param.getHyperlink());
                            table.getItems().set(currentIndex, param);
                        });
                        setText(null);
                        setGraphic(btn);

                    }
                }
            };

                    return cell;
                };

        buttoncol.setCellFactory(cellFactory);

        ObservableList<Param> data = FXCollections.observableArrayList();
        data.add(new Param("description 1", "www.google.com", false, "Button 1"));
        data.add(new Param("description 2", "www.facebook.com", false, "Button 2"));
        data.add(new Param("description 3", "www.yahoo.com", false, "Button 3"));
        data.add(new Param("description 4", "www.amazon.com", false, "Button 4"));

        table.setItems(data);
    }

}

FXML XML文件

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

<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.VBox?>

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication215.FXMLDocumentController">
   <children>
      <TableView fx:id="table" prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
        <columns>
          <TableColumn fx:id="buttoncol" prefWidth="130.0" text="Button" />
          <TableColumn fx:id="hyperlinkcol" prefWidth="252.0" text="Link" />
            <TableColumn fx:id="descriptioncol" prefWidth="217.0" text="Description" />
        </columns>
      </TableView>
   </children>
</VBox>

Param Class 参数类

/**
 *
 * @author sedri
 */
public class Param
{

    private String buttonText;
    private boolean btnState;
    private String hyperlink;
    private String description;

    public Param(String description, String hyperlink, boolean btnState, String buttonText)
    {
        this.hyperlink = hyperlink;
        this.description = description;
        this.btnState = btnState;
        this.buttonText = buttonText;
    }

    public String getButtonText()
    {
        return buttonText;
    }

    public String getDescription()
    {
        return description;
    }

    public void setButtonText(String buttonText)
    {
        this.buttonText = buttonText;
    }

    public void setDescription(String description)
    {
        this.description = description;
    }

    public String getHyperlink()
    {
        return hyperlink;
    }

    public void setHyperlink(String hyperlink)
    {
        this.hyperlink = hyperlink;
    }

    public boolean isBtnState()
    {
        return btnState;
    }

    public void setBtnState(boolean btnState)
    {
        this.btnState = btnState;
    }
}

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM