Этот код демонстрирует то, что вы спрашиваете. Этот код создает 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 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;
}
}