我通过更改DefaultEdgeStyle创建不同类型的边缘。每种样式都取决于在UI中单击的按钮,并且通过从一个顶点拖动到另一个顶点而不是插入边缘方法来添加边缘。
我的问题是,当我想要获得边缘样式时,返回null。
为什么会这样?我正在使用JgraphX(v6)
private void applyEdgeNew2(mxStylesheet stylesheet) {
// Settings for edges
Map<String, Object> edge = new HashMap<String, Object>();
edge.put(mxConstants.STYLE_ROUNDED, true);
edge.put(mxConstants.STYLE_ORTHOGONAL, false);
edge.put(mxConstants.STYLE_EDGE, "elbowEdgeStyle");
edge.put(mxConstants.STYLE_SHAPE, mxConstants.SHAPE_CONNECTOR);
edge.put(mxConstants.STYLE_ENDARROW, mxConstants.ARROW_CLASSIC);
edge.put(mxConstants.STYLE_VERTICAL_ALIGN, mxConstants.ALIGN_MIDDLE);
edge.put(mxConstants.STYLE_ALIGN, mxConstants.ALIGN_CENTER);
edge.put(mxConstants.STYLE_STROKECOLOR, "#FF0000"); // default is #6482B9
edge.put(mxConstants.STYLE_FONTCOLOR, "#446299");
stylesheet.putCellStyle("edge2", edge);
stylesheet.setDefaultEdgeStyle(edge);
}
private void applyEdgeNew3(mxStylesheet stylesheet) {
// Settings for edges
Map<String, Object> edge = new HashMap<String, Object>();
edge.put(mxConstants.STYLE_EDGE, mxConstants.EDGESTYLE_SEGMENT);
edge.put(mxConstants.STYLE_SHAPE, mxConstants.SHAPE_CONNECTOR);
edge.put(mxConstants.STYLE_ENDARROW, mxConstants.ARROW_CLASSIC);
edge.put(mxConstants.STYLE_STROKECOLOR, "#6482B9"); // default is #6482B9
stylesheet.putCellStyle("edge3", edge);
stylesheet.setDefaultEdgeStyle(edge);
}
graphComponent.getGraphControl().addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
mxCell cell =(mxCell) graphComponent.getCellAt(e.getX(), e.getY());
if(cell != null)
{
System.out.println("Edge:"+cell.isEdge());
System.out.println("Vertex:"+cell.isVertex());
if (cell.isEdge()){
System.out.println(cell.getStyle()); //Here is the problem(It return null value)
System.out.println(cell.getId());
System.out.println(cell.getSource());
System.out.println(cell.getTarget());
}
if (cell.isVertex()){
System.out.println(cell.getStyle());
System.out.println(cell.getId());
}
}
}
答案 0 :(得分:1)
null
,因为每条边都使用mxStylesheet
中定义的defaultEdgeStyle。
之前创建的边缘根本没有在视图中更新。如果你搞砸了一下,你会发现他们会将他们的风格改为新定义的defaultEdgeStyle。
如果您需要不同的样式,则必须创建边并为其指定style
参数。这可以是您在图表的mxStylesheet
对象中添加的条目的关键字,也可以是全新的风格,例如: strokeColor=#0ff00f