Skip to main content

Adding jlabel to canvas3d

1 reply [Last post]
neethu
Offline
Joined: 2009-11-25

I tried to add label to the canvas. But I couldn't able to increasse the size of the label. Please help me.

Code for label creation:

public class label extends JPanel {

JLabel tooltip;
JLabel legend;

public label()
{
tooltip = new JLabel("ToolTip data \n test \n");
legend = new JLabel("Legend");

setLayout(new BorderLayout(5,10));
setSize(200, 100);
this.add(tooltip);
this.add(legend);
}

@Override
public void paintComponent(Graphics g )
{
g.drawString("Ore",20,50);
g.drawString("Waste",20,70);
g.fillRect(80,60,10,10);
}

Adding to canvas.

setLayout(new BorderLayout());
Canvas3D canvas = createCanvas();
canvas.setSize(180, 180);
canvas.addMouseMotionListener( new ToolTipGenerator(this, 500) );
canvas.setMonoscopicViewPolicy(View.CYCLOPEAN_EYE_VIEW);
add("Center", canvas);

add("East",new label());

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
chrismcband
Offline
Joined: 2006-07-10

Your post title is confusing as the code example you give is not adding a jlabel to the canvas, but anyway...

As you might know already you cannot add swing components onto a canvas3d. In your code example you are actually adding a JPanel. I am assuming that the code below 'Adding to canvas' is in a separate class, maybe a JFrame or another JPanel. It should be ok to add a canvas3d as the center panel using BorderLayout and other swing panels for the other locations. I can't be sure but I think your code is not working because your label class is using BorderLayout but not assigning and locations for the components. Also avoid using setSize unless you have to, try setPreferredSize instead.