【问题标题】:How to handle Button click event from Table view to create Hyperlink如何处理表格视图中的按钮单击事件以创建超链接
【发布时间】:2018-12-05 10:35:27
【问题描述】:

我有两列,一列是 Button,这是我点击的那一列,另一列是 Hyperlink,它应该在 Link 创建后更新 按钮动作事件。我尝试了方法

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

但它不起作用。

请帮助我如何在 Button 同行操作事件之后创建到 Hyperlink 列的超链接。如下图所示 enter image description here

//这里是类控制器

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

//这里是类参数

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

// 这里是类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;
} }

【问题讨论】:

    标签: java javafx javafx-8 javafx-2


    【解决方案1】:

    此代码演示了您的要求。此代码在Controller 中创建Button's cellFactoryController 类和 Param 类的新更新!

    完整代码:

    主类

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

    控制器

    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 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>
    

    参数类

    /**
     *
     * @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;
        }
    }
    

    【讨论】:

    • @Sedrich。谢谢你的回答我正在尝试你的代码但是你能分享我你的类参数吗? My Class Param 是否有任何修改?
    • 对不起,我以为我发了。
    • 更新了答案。
    • @Sedrich 也许你误会了。让我们看看你发布的图片。在第一行,我希望在单击同一行按钮后显示 www.google.com 超链接(非字符串) 其他以下链接不显示。在单击同一行按钮后,我想要 www.facbook.com 的第二行显示超链接(非字符串)其他下面的链接不显示...看起来我在stackoverflow.com/questions/50815571/…
    • 我假设你可以用给定的代码来做这个逻辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    • 2010-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多