我有两列,一列是 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);
}
}
//这是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;
} }
答案 0 :(得分:0)
此代码演示了您的要求。此代码在Button's
中创建cellFactory
Controller
。 Controller
类和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>
Param班
/**
*
* @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;
}
}