custom events

O

Oxnard

I am trying to use custom events. The custom events do nothing more than
write to a JTextArea. It is a log of sorts.

After much struggling I was able to get a single custom event from a class
which extends a JFrame to write to the JTextArea. I now, however want to
have more than one message from the JFrame to be written to the based upon
what occurs. I am not clear what is the best way to go about this.

It seems that maybe the best way is to "look inside" of my custom event
which I have named JFrameEvent. It seems like this is what you do when you
do a JComponent.getName().

Another thought I had was to do something with AWTEvent.RESERVED_ID_MAX but
again I'm not sure.

My question is how (and if) I can expand what I have to have the handler
class look into the JFrameEvent and figure out which JFrameEvent has
occured?

I have marked in the jFrame class where I want the next event to occur.
Here's a summary of the code I have:



public class jFrame extends JFrame{

public jFrame() {
h = new Handler(this);
setTitle("crap");
setSize(new Dimension(100,100));

setupMenu();

jp1 = new jPanel1();
getContentPane().add(jp1);

this.canLoadDriver();
chooser = new JFileChooser();
}

public void canLoadDriver(){
try{
ociCONN = new ociConn();
}
catch(ClassNotFoundException e){
// this is the first event I got to work!
JFrameEvent jFrameEvent = new JFrameEvent(this);

Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(jFrameEvent);

// in looking at this I noted it returns JFrameEvent[] maybe I have an array
of JFrameEvents ?? not sure how to work with it.
System.out.println(jFrameEvent.toString() + " " +
jFrameEvent.getID());
}
}

public void processEvent(AWTEvent e){
if(e instanceof JFrameEvent){
listener.myListenerMethod((JFrameEvent) e);
}else{
super.processEvent(e); // there are other events which must be handled
such as Mouse Windows etc.
// Can use
System.out.println(e.getClass().getName());
}
}

public void addMyListener(MyListener listener){
this.listener=listener;
}


/**
named inner class
*/
private class copyTypeAction implements ActionListener{
public void actionPerformed(ActionEvent event){

if (dialog == null) dialog = new copyType(); // where the dialog is
created
if(dialog.showDialog(jFrame.this," ")){

// I want to have another event here.!!!!!!!!!!!!!!!!!

chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int result = chooser.showOpenDialog(jFrame.this);

}
}
} // end of named inner class copyTypeAction

MyListener listener = null;
Handler h = null;
private jPanel1 jp1;
private ociConn ociCONN;
private copyType dialog = null;
private JFileChooser chooser;
}

public class Handler implements MyListener, ActionListener{

public Handler(jFrame j){
j.addMyListener(this);
this.j = j;
}

public Handler(JTextField tf){
String s = tf.getName();
if (s.equals("tf1")){
tf1 = tf;
}
}

public Handler(jPanel1 jp1){
this.jp1 = jp1;
}

public void myListenerMethod(JFrameEvent e) {
//System.out.println("in handler");
jp1.setText("here it is");

/** I really want something like:
String s = jp1.getName();
if (s.equals("the class not found")){
jp1.setText("class not found");
else if (s.equals("the class not found")){
jp1.setText("my JFrameEvent just before the chooser")
}
}
*/

public void actionPerformed(ActionEvent e) {
}

private static jFrame j;
private static jPanel1 jp1;
// private static JTextArea jta;
private static JTextField tf1;
}

public class JFrameEvent extends AWTEvent{

/** Creates a new instance of JFrameEvent */
public JFrameEvent(Object source) {
super(source, JFRAME_EVENT);
// System.out.println("in JFrameEvent");
}
public static final int JFRAME_EVENT=AWTEvent.RESERVED_ID_MAX + 5555;
}


public interface MyListener extends EventListener{
public void myListenerMethod(JFrameEvent e);
}

public class jPanel1 extends JPanel{

public jPanel1() {
h = new Handler(this);
taA = new JTextArea(8, 40);
taA.setEditable(false);
taA.setName("taA");
JScrollPane scrollPane = new JScrollPane(taA);
add(scrollPane); // add the JScrollPane NOT the JTextArea as the
// JScrollPane incapulates the JTextArea
}

public void setText(String s){
taA.append(s);
}

Handler h = null;
private JTextField tf1;
private JTextArea taA;
}




Thank you very much
Ox
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top