How do I set the component background to a specific color in JGraphX?

2.1k views Asked by At

I want to make the background of a JGraphX graph component (https://github.com/jgraph/jgraphx) a specific color all over. I tried the standard call for any Swing component:

graphComponent.setBackground(Color.BLACK);

but this has no effect. I tried forcing a repaint of the component also, no luck. Is the call incorrect, or is there some specific way to force a refresh?

1

There are 1 answers

0
tenorsax On BEST ANSWER

Since mxGraphComponent extends JScrollPane change the background of the view port:

graphComponent.getViewport().setOpaque(true);
graphComponent.getViewport().setBackground(Color.BLACK);

From JScrollPane docs:

This can be accomplished by setting the background color of the viewport, via scrollPane.getViewport().setBackground(). The reason for setting the color of the viewport and not the scrollpane is that by default JViewport is opaque which, among other things, means it will completely fill in its background using its background color. Therefore when JScrollPane draws its background the viewport will usually draw over it.