lable hides image in frame

Hello

I am trying to display a label over a chart but the lable hides the chart

Can anyone help me see what parametres I have to set to stop this from happening?

super("Chart");

JLabel label;

chart ch = new chart();

add(ch);

add(new Label("Hi There!"));

setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));

setSize(400, 400);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );

setVisible(true);

Thanks :-)

Message was edited by:

concrete

[531 byte] By [concretea] at [2007-11-26 21:16:57]
# 1
Don't add components directly to frame. Usejava.awt.Container contP = this.getContentPane();and add elements to containercontP.add(ch)contP.add(new Label("Hi There!"));PS. Use code formatting
hellbindera at 2007-7-10 2:55:34 > top of Java-index,Java Essentials,New To Java...
# 2
H?br>I have tried your suggesstionBut when the code is run then the label still appears over the chart :-(
concretea at 2007-7-10 2:55:34 > top of Java-index,Java Essentials,New To Java...
# 3
paste the code
hellbindera at 2007-7-10 2:55:34 > top of Java-index,Java Essentials,New To Java...
# 4
You may add components directly to a JFrame since it forwards the add() to the contentpane. Regarding your question, see my reply in the other thread you created.#PS Do not create multiple threads for a single question
duckbilla at 2007-7-10 2:55:34 > top of Java-index,Java Essentials,New To Java...
# 5

@duckbill

try that:

import javax.swing.JButton;

import javax.swing.JFrame;

public class Forum extends JFrame {

public Forum(){

JButton b1 = new JButton("1");

JButton b2 = new JButton("2");

add(b1);add(b2);

setSize(200,200);

setDefaultCloseOperation(3);

setVisible(true);

}

public static void main(String[] args){

new Forum();

}

}

hellbindera at 2007-7-10 2:55:34 > top of Java-index,Java Essentials,New To Java...
# 6

@duckbill again

sorry! There were the times where adding to content pane was not done directly to by JFrame.add() . Now getting the content pane does not work as well.

@OP

there You go:

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class Forum extends JPanel {

public Forum(){

JButton b1 = new JButton("1");

JButton b2 = new JButton("2");

add(b1);add(b2);

}

public static void main(String[] args){

JFrame frame = new JFrame("Forum");

frame.setContentPane(new Forum());

frame.setSize(200,200);

frame.setDefaultCloseOperation(3);

frame.setVisible(true);

}

}

hellbindera at 2007-7-10 2:55:34 > top of Java-index,Java Essentials,New To Java...
# 7

>@duckbill again

>sorry! There were the times where adding to content

>pane was not done directly to by JFrame.add() . Now

>getting the content pane does not work as >well.

You CAN use either

// JFrame frame

frame.getContentPane().add(...);

//OR

frame.add(...);

Btw it is not a good idea to extend JFrame or JPanel unless you are actually extending the behaviour of the class (e.g. doing custom painting). Just use the classes as they are.

#

duckbilla at 2007-7-10 2:55:34 > top of Java-index,Java Essentials,New To Java...
# 8
> You CAN use either> > // JFrame frame > frame.getContentPane().add(...);> //OR> frame.add(...);> Some time ago it was a difference.
hellbindera at 2007-7-10 2:55:34 > top of Java-index,Java Essentials,New To Java...
# 9

> JLabel label;

> chart ch = new chart();

> add(ch);

> add(new Label("Hi There!"));

Don't mix Swing and AWT components. This will cause problems, like

components that don't layer properly, which sounds like your problem.

If this doesn't fix you problem, post a tiny example program that demonstrates

your problem.

DrLaszloJamfa at 2007-7-10 2:55:34 > top of Java-index,Java Essentials,New To Java...
# 10

H?

Yes that is my problem that i think my chart is being hidden by my label

and I am getting a little confused as to why and how to order the two items so this does not happen

Thanks for the advice

see code below

import java.awt.*;

import javax.swing.*;

public class test extends JFrame {

/** Creates a new instance of Chart */

public test() {

super("test");

JLabel label;

chart ch = new chart();

add(ch);

add(new Label("Hi There!"));

setSize(400, 400);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );

setVisible(true);

}

public static void main(String[] arguments) {

test pie = new test();

}

}

concretea at 2007-7-10 2:55:34 > top of Java-index,Java Essentials,New To Java...
# 11
Which of the previous posts You DIDN'T understand or maybe I should ask which of them HAVE YOU READ?Try to pay attention to what others are trying to tell you or You won't receive any futrher help.
hellbindera at 2007-7-10 2:55:35 > top of Java-index,Java Essentials,New To Java...
# 12
Don't mix Swing and AWT components.Do you understand that statement?
DrLaszloJamfa at 2007-7-10 2:55:35 > top of Java-index,Java Essentials,New To Java...
# 13

> Don't mix Swing and AWT components.

>

> Do you understand that statement?

No dispute there ... but that's not the pblm here - it's Layout issue. JFrame and Frame are by default BorderLayout - I know you know this BTW and so default the Label added later overlays the CENTER of the BorderLayout ...

import java.awt.*;

import javax.swing.*;

public class MixedReactionsFrame extends JFrame {

/** Creates a new instance of Chart */

public MixedReactionsFrame() {

super("MixedReactionsFrame");

Container c = getContentPane();

JLabel label;

chart ch = new chart();

ch.setPreferredSize(new Dimension(110,110));

c.add(ch);

c.add(new Label("Hi There!"),BorderLayout.NORTH);

setSize(400, 400);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );

setVisible(true);

}

private class chart extends JPanel {

public void paint(Graphics g) {

//public void paintComponent(Graphics g) {

//super.paintComponent(g);

g. setColor(Color.BLUE);

g.fillRect(0,0,100,100);

}

}

public static void main(String[] arguments) {

MixedReactionsFrame pie = new MixedReactionsFrame();

}

}

abillconsla at 2007-7-10 2:55:35 > top of Java-index,Java Essentials,New To Java...
# 14
Hello everyoneThanks for all the helpful comments and pointersAnd yes I do now understand (after some reading) the difference between AWT and SWINGAnd I now see my problem lay in my poor understanding of how to layout the frameAgainThanks :-)
concretea at 2007-7-10 2:55:35 > top of Java-index,Java Essentials,New To Java...
# 15
Sure thing.
abillconsla at 2007-7-21 18:15:07 > top of Java-index,Java Essentials,New To Java...