Help with referencing the top-pane from within an action event

Brand new to this, there is obviously an easy solution, I just can't find it.

import javax.swing.AbstractButton;

import javax.swing.JButton;

import javax.swing.JPanel;

import javax.swing.JFrame;

import javax.swing.ImageIcon;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import java.awt.event.MouseListener;

import java.awt.event.MouseEvent;

import java.awt.Dimension;

import java.awt.Toolkit;

import java.awt.*;

import java.util.Vector;

import java.util.Random;

import javax.swing.*;

publicclass BasicDrawextends JPanel

implements ActionListener{

protected JButton b1;

public BasicDraw()

{

b1 =new JButton("Disable middle button");

b1.addActionListener(this);

add(b1);

}

publicvoid actionPerformed(ActionEvent e)

{

JLabel x =new JLabel("sadf");

main().frame.getContentPane().add(x);

}

publicstaticvoid main(String[] args){

JFrame frame =new JFrame("BasicDraw");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.

BasicDraw newContentPane =new BasicDraw();

newContentPane.setOpaque(true);//content panes must be opaque

frame.setContentPane(newContentPane);

//Display the window.

frame.setSize(300,300);

frame.setVisible(true);

}

}

I know this is probably a really odd bit of code, basically I am just trying to work out how to reference the pane so I can add things to it, or draw an object or something, when the button is pressed (or mouse click would be even better).

Thanks a lot for any help, I'm finding java thouroughly confusing :)

Alex

[3335 byte] By [xanderbeedlea] at [2007-11-26 20:57:56]
# 1

Because the component you are trying to get is the BasicDraw instance itself, you can simply call this.add(x); in the actionPerformed method. However, look at the javadoc for the add method; there is one more thing you need to do or your label will not display when you click your button. Yes, I could just tell you what to do but finding it yourself is the best way to learn :-)

smalinowa at 2007-7-10 2:27:32 > top of Java-index,Desktop,Core GUI APIs...
# 2

Ok, so changing that to this.add(x); and then including a JLabel.RIGHT field in the JLabel creator, it now compiles, but pressing the button does nothing.

Also, on a slightly unrelated topic, how would I go about getting frame by frame animation (of something simple, like a dot moving across the screen)? I can't seem to get what I can find working, it just freezes.

Alex

xanderbeedlea at 2007-7-10 2:27:32 > top of Java-index,Desktop,Core GUI APIs...
# 3

> Ok, so changing that to this.add(x); and then

> including a JLabel.RIGHT field in the JLabel creator,

> it now compiles, but pressing the button does

> nothing.

As I said in my previous post, look at the javadoc for the add method. It explains why the label is not showing up when you click your button.

> Also, on a slightly unrelated topic, how would I go

> about getting frame by frame animation (of something

> simple, like a dot moving across the screen)? I can't

> seem to get what I can find working, it just

> freezes.

I know you can add animated gifs to a JLabel but have not done it myself. Someone else may be able to answer that but is should be a different post - try to keep a single post to a single topic. You'll have better luck getting an answer that way.

smalinowa at 2007-7-10 2:27:32 > top of Java-index,Desktop,Core GUI APIs...
# 4
How exactly do i use javadoc? I tried called javadoc -private BasicDraw.java, doesn't give me any details about add()Alex
xanderbeedlea at 2007-7-10 2:27:32 > top of Java-index,Desktop,Core GUI APIs...
# 5
> How exactly do i use javadoc? http://java.sun.com/j2se/1.5.0/docs/api/
camickra at 2007-7-10 2:27:33 > top of Java-index,Desktop,Core GUI APIs...
# 6
? Still, can't find anything on the add method?
xanderbeedlea at 2007-7-10 2:27:33 > top of Java-index,Desktop,Core GUI APIs...
# 7
The add() method is defined in the Container class.
camickra at 2007-7-10 2:27:33 > top of Java-index,Desktop,Core GUI APIs...
# 8

I'm obviously overlooking something,

I've tried adding an int to add() for the position, not sure how to use the contraint... tried calling

add(x,this);

I appreciate you are only trying to help by getting me to work this out and I really appreciate your help, but something less cryptic please?

Alex

Message was edited by:

xanderbeedle

xanderbeedlea at 2007-7-10 2:27:33 > top of Java-index,Desktop,Core GUI APIs...
# 9

Calling this.add(x) is fine. You do not need to use a different add method. We are trying to get you to realize there is another step you need to perform after you call this.add(x). Go to http://java.sun.com/j2se/1.5.0/docs/api. Find Container in the bottom left frame and click on it. Scroll down the Container javadoc page until you see the add(Component comp) method under Method Summary. Click the add link and read what it says. Especially the "Note" section.

I'm sure it may be frustrating not being given the exact answer. I gave you part of it but the only way to go from a junior Java developer to a senior Java developer is not just to learn the language but to learn how to find your own answers. Give a man a fish and he eats for a day, teach a man to fish and he eats for a lifetime.

smalinowa at 2007-7-10 2:27:33 > top of Java-index,Desktop,Core GUI APIs...