JFrame Help!

How would I get a JFrame to paint two JComponents on top of each other? I have tried this:

JFrame f =new JFrame();

JComponent grid =new Grid();

JComponent rect =new Rectangle();

f.add(grid);

f.add(rect);

f.setVisible(true);

that doesn't work (for obvious reason I should have noticed); nor does:

JFrame f =new JFrame();

JComponent grid =new Grid();

JComponent rect =new Rectangle();

JPanel p =new JPanel();

p.add(grid);

p.add(rect);

f.add(p);

f.setVisible(true);

or:

JFrame f =new JFrame();

JComponent grid =new Grid();

JComponent rect =new Rectangle();

f.getContentPane().add(grid);

f.getContentPane().add(rect);

f.setVisible(true);

How can I get a grid and a rectangle to print on top of each other?

[1286 byte] By [da_java_mana] at [2007-11-27 11:53:11]
# 1

In future Swing related questions should be posted into the Swing forum.

Your question is very odd. Why do you want to do this? Are you handcrafting a table? It would be easier just to use a JTable.

cotton.ma at 2007-7-29 18:48:51 > top of Java-index,Java Essentials,Java Programming...
# 2

you could implement a class that extends JPanel

public class MyJPanel extends JPanel

{

public myJPanel()

{

super();

}

public void paintComponent( Graphics g )

{

super.paintComponent( g );

// draw ur grid and rectangle here..

}

}

//Then for your JFrame..

JFrame frame = new JFrame();

frame.getContentPane().add( new MyJPanel() );

Hope this helps..

smithdale87a at 2007-7-29 18:48:51 > top of Java-index,Java Essentials,Java Programming...
# 3

The point is that the grid is in the background, and the user can drag and drop things on top of it. The grid is there to help out the user visualize what he's constructing.

The purpose is to allow any JComponent at any number to be dropped anywhere.

Ideas?

da_java_mana at 2007-7-29 18:48:51 > top of Java-index,Java Essentials,Java Programming...
# 4

Can anyone help me?

da_java_mana at 2007-7-29 18:48:51 > top of Java-index,Java Essentials,Java Programming...
# 5

What is a Grid? Rectangle is not a JComponent.

Learn to use Layout Managers.

bsampieria at 2007-7-29 18:48:51 > top of Java-index,Java Essentials,Java Programming...
# 6

No 2 items can ever be on top of each other. One has to be on the top of the other

</friday>

georgemca at 2007-7-29 18:48:51 > top of Java-index,Java Essentials,Java Programming...
# 7

> No 2 items can ever be on top of each other. One has

> to be on the top of the other

>

> </friday>

MC Escher could draw it so... ;-)

bsampieria at 2007-7-29 18:48:51 > top of Java-index,Java Essentials,Java Programming...
# 8

> No 2 items can ever be on top of each other. One has

> to be on the top of the other

>

> </friday>

Ha! You know what I mean, though. Whenever I try to add both to the frame, either one or neither are drawn, never both. Could a moderator move this to the Swing forum?

da_java_mana at 2007-7-29 18:48:51 > top of Java-index,Java Essentials,Java Programming...
# 9

> No 2 items can ever be on top of each other. One has

> to be on the top of the other </friday>

Huh, George, lol? You should rethink your answer.

Of course two components can overlap... how else would

LayeredPanes (and Desktops) work? ; )

But have you tried just making one component transparent

setOpaque(false) and placing it over the other with setBounds(x, y, w, l)?

z-order

http://java.sun.com/docs/books/tutorial/uiswing/components/layeredpane.html

TuringPesta at 2007-7-29 18:48:51 > top of Java-index,Java Essentials,Java Programming...