Writing custom action events

This is probably an easy problem but I would really appreciate some help. I have read the java tutorials on Event Listeners but can't seem to find how to write a custom action event.

I'm having trouble updating my GUI. It consists of a JFrame with two JPanels.

When I change something on the one JPanel I want to get the JFrame to redraw both of the panels.

To do this I'm trying to use an action listener on the JFrame that will wait for a change in one of the JPanels. When the JPanel changes its state it will create an action event and trigger the JPanel to update.

The actual program is a lot more complicated than this but the example below illustrates what I'm trying to do.

class Testextends JFrameimplements ActionListener

{

TestPanel p1;

TestPanel p2;

public Test()

{

p1 =new TestPanel();

p1.addActionListener(this);

p2 =new TestPanel();

p2.addActionListener(this);

drawPanels(p1,p2);

}

public drawPanels()

{

//code to add and draw panels goes here

}

publicvoid actionPerformed(ActionEvent e){

drawPanels();

}

then

class TestPanelextends JPanel

{

public TestPanel()

{

setSize(400,300);

String someProperty ="1";//this represents a property that will be changed

someProperty ="2";//property is changed

//create action event to trigger JFrame above

//? How do I do this

}

}

I've tried just passing the JFrame into the JPanels but this causes problems when compiling and I assume it's bad programming practice. I've also thought of using the Observer and Observable method but because Observable needs to extend the JFrame which obviously doesnt work.

Any input would be greatly appreciated.

[2802 byte] By [pturbo911a] at [2007-11-27 2:40:22]
# 1

Hi,

I'm not sure if I understood correctly, so please clarify if my response is not helpful.

Why not create a new class that extends the JPanel (call it "MyPanel" for now)? This class can then have your integers keeping track of states, and you can even go so far as to pass panels as constructors to the MyPanel class. That way, you can have each MyPanel object keep track of a second MyPanel object, and as soon as one is changed or experiences an ActionEvent, automatically calls some function in the second MyPanel object.

Let me know if you need clarification.

Thanks,

Wojciech

VoytekGa at 2007-7-12 3:03:13 > top of Java-index,Java Essentials,New To Java...
# 2
Thanks for the reply Wojciech. I tried that method but couldn't get it to work so I'm just using a Observer-Observable structure to update stuff- James
pturbo911a at 2007-7-12 3:03:13 > top of Java-index,Java Essentials,New To Java...