给Javafx2表格中加超链接hyperlink

首先

HyperlinkCell要继承TableCell

import java.util.Map;

import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Pos;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.TableCell;

public class HyperlinkCell<S, T> extends TableCell<S, T> {
	private Hyperlink hyperlink;
	private ObservableValue<T> ov;
	private Map<String,String> vmap;
	
	public Map<String, String> getVmap() {
		return vmap;
	}

	public void setVmap(Map<String, String> vmap) {
		this.vmap = vmap;
	}

	public HyperlinkCell() {
		this.hyperlink = new Hyperlink();
		this.hyperlink.setUnderline(true);
		setAlignment(Pos.CENTER);
		setGraphic(hyperlink);
	}

	@Override
	protected void updateItem(T item, boolean empty) {
		super.updateItem(item, empty);
		if (empty) {
			setText(null);
			setGraphic(null);
		} else {
			setGraphic(hyperlink);
			ov = getTableColumn().getCellObservableValue(getIndex());
			if (ov instanceof StringProperty) {
				hyperlink.setText(ov.getValue().toString());
			}
			//得到cell那一行的数据
			vmap = (Map<String,String>)this.getTableRow().getItem();
			//给cell的字体赋值
			if(vmap.get("TEXT_COLOR")!=null){
				if(!vmap.get("TEXT_COLOR").equals("")){
					hyperlink.setStyle("-fx-text-fill: "+vmap.get("TEXT_COLOR"));
				}
			}
			//给cell的背景色赋值
			if(vmap.get("BACKGROUND_COLOR")!=null){
				if(!vmap.get("BACKGROUND_COLOR").equals("")){
					this.setStyle("-fx-background-color: "+vmap.get("BACKGROUND_COLOR"));
				}
			}
		}
	}
}

相信表格中嵌多选的例子挺多的,可以自己搜索 一下。

以下,是动态创建表格列的过程,部分代码片段如下:

tableColu = new TableColumn<>(title);
tableColu.setCellValueFactory(new MapValueFactory(id));
if("hyperlink".equalsIgnoreCase(Columntype)){
tableColu.setCellFactory(new Callback<TableColumn<String, Boolean>, TableCell<String, Boolean>>() {
              @Override
              public TableCell<String, Boolean> call(TableColumn<String, Boolean> param) {
                  final HyperlinkCell<String, Boolean> cell = new HyperlinkCell<>();
                  final Hyperlink radio = (Hyperlink) cell.getGraphic();
                  radio.setOnAction(new EventHandler<ActionEvent>() {
                      @Override
                      public void handle(ActionEvent event) {
                          Map m = new HashMap<String,String>();

                          Map<String,String> rowmap = cell.getVmap();//可以得到表格中一行的数据                                                    
                          m.put("123",rowmap.get("colname"));//
                          Dialogs d = new DialogsImpl();//自己实现弹出窗口                          
                          d.showDialog(m, w, h);
                          
                      }
                  });
                  return cell;
              }
          });
}


   

你可能感兴趣的:(tableview,TableCell,javafx2,Hyperlink)