Adding JPanel to JLayeredPane - Please help!  
Author Message
krn





PostPosted: 2004-4-26 6:35:00 Top

java-programmer, Adding JPanel to JLayeredPane - Please help! Hi

I have a problem adding a JPanel to a JLayeredPane.
The class ChartGraphics paints a system of coordinats / Chart on a
JPanel
using the paint() metode. When I add the ChartGraphics object directly
to the Contentpanel of the frame the Chart is shown without any
problems. But as I want to add some buttons on top of the Chart, I
want to place it in a JLayeredPanel, and afterwards add the Jbuttons.
The problem is that the Chart is'nt shown if it is placed in the
JLayeredpane, why? The Button is displayed nicely.

Can anybody tell me how to display my JPanel background from the
JlayeredPane?

/Thanks
Kristian

******************ShowTrendsTest *******************

public class ShowTrendsTest {

public static void main(String[] args) {

JFrame f = new JFrame("ShowTrends");
ChartGraphics Chart = new ChartGraphics();//extends JPanel
JButton button = button("TST"); //Just a JButton
JLayeredPane layeredPane = new JLayeredPane();

layeredPane.add(Chart, new Integer(0));
layeredPane.add(button, new Integer(1));

f.getContentPane().add(layeredPane);

f.pack();
f.show();
}

public static JButton button(String arg){
JButton b1 = new JButton(arg);
b1.setVerticalAlignment(JButton.TOP);
b1.setHorizontalAlignment(JButton.CENTER);
b1.setOpaque(true);
b1.setBackground(Color.red);
b1.setForeground(Color.black);
b1.setBounds(100, 100, 140, 140);
return b1;
}
}

********************************************
 
ak





PostPosted: 2004-4-26 6:53:00 Top

java-programmer >> Adding JPanel to JLayeredPane - Please help! > Can anybody tell me how to display my JPanel background from the
> JlayeredPane?
You just forgot to call ChartGraphics#setBounds(x, y, w, h);

____________

http://reader.imagero.com the best java image reader.


 
Michael Rauscher





PostPosted: 2004-4-26 7:06:00 Top

java-programmer >> Adding JPanel to JLayeredPane - Please help! Hi Kristian,

email***@***.com schrieb:
> Hi
>
> I have a problem adding a JPanel to a JLayeredPane.
> The class ChartGraphics paints a system of coordinats / Chart on a
> JPanel
> using the paint() metode. When I add the ChartGraphics object directly
> to the Contentpanel of the frame the Chart is shown without any
> problems. But as I want to add some buttons on top of the Chart, I
> want to place it in a JLayeredPanel, and afterwards add the Jbuttons.
> The problem is that the Chart is'nt shown if it is placed in the
> JLayeredpane, why? The Button is displayed nicely.

The difference is, that the content pane has a border layout by default
whereas a layered pane has no layout manager assigned. Therefore the
preferred size of your component is used if you add it to the content
pane and it is ignored if you add it to the layered pane.

Since the size of a component is 0,0 by default, your chart isn't shown.
>
> Can anybody tell me how to display my JPanel background from the
> JlayeredPane?

Set the size (or bounds) of your chart.

Michael

 
 
krn





PostPosted: 2004-4-26 17:08:00 Top

java-programmer >> Adding JPanel to JLayeredPane - Please help! Hi
Yep, that took care of my problem.
Thanks!
/Kristian