GlassPane on label

Hello!

I'm trying to create a GUI interface. I use NetBeans, often in the Design view.

I have a jFrame. Inside of it I have a jLabel lbl1 and a jPanel.

I wish to add a jGlassPane on lbl1, in order to draw other labels on it.

I don't have the slightest idea about the way to realise that.

I also read lots of tutorials about glassPane but I can't make head or tail of them. They're too complex for my purpouse, and I can't find a way to use them.

Can someone please help me?

Thank you!

Message was edited by:

YngwieM

[582 byte] By [YngwieMa] at [2007-11-27 6:13:20]
# 1
Maybe it would be better to use [url http://java.sun.com/docs/books/tutorial/uiswing/components/layeredpane.html]Layered Panes[/url] instead.
camickra at 2007-7-12 17:21:39 > top of Java-index,Desktop,Core GUI APIs...
# 2

import java.awt.*;

import javax.swing.*;

public class LabelOverlay {

private JPanel getContent() {

JLabel label = new JLabel("hello world", JLabel.CENTER);

//label.setBorder(BorderFactory.createEtchedBorder());

JPanel labelPanel = new JPanel(new BorderLayout());

labelPanel.add(label);

OverlayComponent component = new OverlayComponent();

JPanel panel = new JPanel();

OverlayLayout overlay = new OverlayLayout(panel);

panel.setLayout(overlay);

panel.add(component);

panel.add(labelPanel);

return panel;

}

public static void main(String[] args) {

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(new LabelOverlay().getContent());

f.setSize(400,400);

f.setLocation(200,200);

f.setVisible(true);

}

}

class OverlayComponent extends JComponent {

protected void paintComponent(Graphics g) {

// This is non-opaque so there is no need

// to call super or clear the background.

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

int w = getWidth();

int h = getHeight();

int R = Math.min(w,h)/8;

int[][] xy = generateShapeArrays(w/2, h/2, R, 3);

Polygon triangle = new Polygon(xy[0], xy[1], 3);

g2.setPaint(Color.red);

g2.draw(triangle);

}

private int[][] generateShapeArrays(int cx, int cy, int R, int sides) {

int radInc = 0;

if(sides % 2 == 0)

radInc = 1;

int[] x = new int[sides];

int[] y = new int[sides];

for(int i = 0; i < sides; i++) {

x[i] = cx + (int)(R * Math.sin(radInc*Math.PI/sides));

y[i] = cy - (int)(R * Math.cos(radInc*Math.PI/sides));

radInc += 2;

}

// keep base of triangle level

if(sides == 3)

y[2] = y[1];

return new int[][] { x, y };

}

}

crwooda at 2007-7-12 17:21:39 > top of Java-index,Desktop,Core GUI APIs...
# 3

Using crwood's example you can add the OverlayComponent directly to the label without worrying about a parent panel:

//f.getContentPane().add(new LabelOverlay2().getContent());

JLabel label = new JLabel("hello world", JLabel.CENTER);

label.setLayout( new BorderLayout() );

label.add( new OverlayComponent());

f.getContentPane().add(label);

camickra at 2007-7-12 17:21:39 > top of Java-index,Desktop,Core GUI APIs...
# 4
Thanks for your replies!I used the jLayeredPanel option.
YngwieMa at 2007-7-12 17:21:39 > top of Java-index,Desktop,Core GUI APIs...