Creating a vertex of the desired color is easy:

mxgraph.insertVertex(mxDefaultParent, null, "", xCor, yCor, width , height ,"fillColor=blue");

but how does one change the color of a vertex after is has been created? I tried for example:\

mxgraph.getModel().beginUpdate();
try
{
  mxgraph.setCellStyles(mxConstants.STYLE_FILLCOLOR, "red", new Object[]{vertexMap.get(vertex)});
}
finally
{
   mxgraph.getModel().endUpdate();
}

but this simply doesn't do anything? I also tried adding a mxgraph.refresh() at the end, but still my vertex didn't get a nice red color.

asked 11 Aug '11, 07:32

joris's gravatar image

joris
10128
accept rate: 50%

edited 15 Aug '12, 04:58

David's gravatar image

David
4.9k21831


Ok I finally found the solution. Here is a small code snippet

//Build a frame, create a graph, and add the graph to the frame so you can actually see the graph\
frame = new JFrame("Branching graph");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

mxGraph mxgraph= new mxGraph();
mxGraphComponent graphComponent= new mxGraphComponent(mxgraph);

frame.add(graphComponent, BorderLayout.CENTER);
frame.setVisible(true);

//No clue what this does but it is needed
Object mxDefaultParent = mxgraph.getDefaultParent();

//Add a vertex to the graph in a transactional fashion. The vertex is actually a 'cell' in jgraphx terminology
Object vertex=null;
mxgraph.getModel().beginUpdate();
try{
  double xCor=100.0;
  double yCor=100.0;
  double width=50;
  double height=50;
  vertex=mxgraph.insertVertex(mxDefaultParent, null, "", xCor, yCor, width , height ,"fillColor=blue"); //creates a blue vertex 
}
finally{
  mxgraph.getModel().endUpdate();
}

/*Given a cell, we can change it's style attributes, for example the color. NOTE that you have to call the graphComponent.refresh() function, otherwise you won't see the difference!*/
mxgraph.setCellStyles(mxConstants.STYLE_FILLCOLOR, "green", new Object[]{vertex}); //changes the color to red
graphComponent.refresh();\

The last 2 lines of the above code are actually the answer to my question :)

link

answered 12 Aug '11, 00:06

joris's gravatar image

joris
10128
accept rate: 50%

edited 15 Aug '12, 05:01

David's gravatar image

David
4.9k21831

Actually the last line would have been enough :)

(27 Oct '12, 10:09) fachexot
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×66
×50
×6
×1

Asked: 11 Aug '11, 07:32

Seen: 7,821 times

Last updated: 27 Oct '12, 10:09