我在使用JGraph(T)库时遇到了一些问题。我需要更改默认选择视图,例如:默认背景为橙色,如果顶点被选中,则添加绿色边框,我是否可以更改此可视化策略以在所选元素上将背景更改为Color.BLUE。我尝试执行以下代码:
GraphSelectionModel graphSelectionModel = new DefaultGraphSelectionModel(jGraph);
graphSelectionModel.setSelectionMode(GraphSelectionModel.MULTIPLE_GRAPH_SELECTION);
graphSelectionModel.addGraphSelectionListener(new GraphSelectionListener()
{
HashMap oldestCellsAndAttrs = new HashMap();
@Override
public void valueChanged(GraphSelectionEvent e)
{
jGraph.getModel().beginUpdate();
m_jgAdapter.edit(oldestCellsAndAttrs, null, null, null);
oldestCellsAndAttrs.clear();
Map cellAndAttrs = new HashMap();
for (Object obj : e.getCells())
{
DefaultGraphCell cell = (DefaultGraphCell) obj;
oldestCellsAndAttrs.put(cell, JGraphModelAdapter.createDefaultVertexAttributes());
Map attrs = cell.getAttributes();
GraphConstants.setBackground(attrs, Color.BLUE);
cellAndAttrs.put(cell, attrs);
}
m_jgAdapter.edit(cellAndAttrs, null, null, null);
jGraph.getModel().endUpdate();
}
});
fillGraph(tree, g);
layout(g, m_jgAdapter, jGraph);
setSize(3 * width / 4, height);
jGraph.setSelectionModel(graphSelectionModel);此更改在相同的选定对象上进行bkg,但不会在未选择后返回。exists是否可以默认解决此问题?
发布于 2012-02-23 19:45:27
我用愚蠢的代码来解决问题:
@Override
public void valueChanged(GraphSelectionEvent e)
{
Object[] cells = e.getCells();
HashMap<DefaultGraphCell, AttributeMap> cellsAndAttrs = new HashMap<DefaultGraphCell, AttributeMap>();
for (Object c : cells)
{
DefaultGraphCell cell = (DefaultGraphCell) c;
AttributeMap cellAttrs = cell.getAttributes();
if (jGraph.isCellSelected(cell))
GraphConstants.setBackground(cellAttrs, SELECTED_COLOR);
else
GraphConstants.setBackground(cellAttrs, NON_SELECTED_COLOR);
cellsAndAttrs.put(cell, cellAttrs);
}
m_jgAdapter.edit(cellsAndAttrs, null, null, null);
}https://stackoverflow.com/questions/9392278
复制相似问题