Search code examples
javaswingjlabeljung

How do I add a JLabel to a JUNG graph?


I'm trying to add a label to my graph, but can't get it out of the default center position. This is what I have right now in my update method (it refreshes the display):

Graph<String, String> graph = getGraph();
BasicVisualizationServer<String, String> vv = getViewer(graph);
frame.getContentPane().removeAll();
frame.getContentPane().add(vv);
JLabel label = new JLabel("<html>ndp: blue<br>mdp: dark green</html>", JLabel.LEFT);
vv.add(label);
frame.pack();

It shows the label, but won't put it on the left no matter what I attach the label to or how I specify that it should be left... What am I doing wrong?


Solution

  • It's ugly, but I fixed it by just adding lots of non-breaking spaces to one row of the label content (Alt+0160), which expanded the width of the Label to be roughly the size of the window so when it is centered, the text is left-aligned. setSize() didn't work.