HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"A Return to Reflection"
Volume: 4 Issue: 10, p. 54

	

Listing 1:

 public class MyLabel extends JLabel implements MethodListener{
  ...//code
  public void methodAction(ActionEvent e){
    this.setText( e.getActionCommand() );
  }
}


...//more code
MyLabel aLabel = new MyLabel();


JTextArea editor = new JTextArea( new CodeDocument() );
CodeDocument doc =  (CodeDocument)control.getDocument();
doc.addMethodListener(aLabel);

Listing 2:

    SimpleAttributeSet inputAttributes = new SimpleAttributeSet();
..//more code...
try{
  JComboBox c = new JComboBox();
  c.addActionListener(this);


  StyleConstants.setComponent(inputAttributes, c);
  super.insertString(currentPos + 1, " ", inputAttributes);
}
catch (Exception ex){
  ex.printStackTrace();
}


Listing 3:

    public class CodeDocument extends DefaultStyledDocument implements
ActionListener{


..//more code


  public void actionPerformed(ActionEvent e) {
    try{
      JComboBox comp = (JComboBox)e.getSource();
      ClassElement elem = (ClassElement) comp.getSelect-
      edItem();
      String item = elem.getElementValue();
      this.remove(listOffs, 1);
      super.insertString(listOffs, item, null);
      currentMethod = item;
    }
    catch (Exception ex){
      ex.printStackTrace();
    }
  }
    package CodeEditor;


import com.sun.java.swing.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;


public class Application1 {



    public static void main(String[] args) {
       TestFrame frame = new TestFrame();
       TextPane editor = new JTextPane();
       CodeDocument doc = new CodeDocument();


       Vector keywords = new Vector();


       keywords.addElement("abstract");
       keywords.addElement("boolean");
       keywords.addElement("break");
       keywords.addElement("byte");
       keywords.addElement("byvalue");
       keywords.addElement("case");
       keywords.addElement("cast");
       keywords.addElement("catch");
       keywords.addElement("char");
       keywords.addElement("class");
       keywords.addElement("const");
       keywords.addElement("continue");
  keywords.addElement("default");
  keywords.addElement("do");
  keywords.addElement("double");
  keywords.addElement("extends");
  keywords.addElement("else");
  keywords.addElement("false");
  keywords.addElement("final");
  keywords.addElement("finally");
  keywords.addElement("float");
  keywords.addElement("for");
  keywords.addElement("future");
  keywords.addElement("generic");
  keywords.addElement("if");
  keywords.addElement("implements");
  keywords.addElement("import");
  keywords.addElement("inner");
  keywords.addElement("instanceof");
  keywords.addElement("int");
  keywords.addElement("interface");
  keywords.addElement("long");
  keywords.addElement("native");
  keywords.addElement("new");
  keywords.addElement("null");
  keywords.addElement("operator");
  keywords.addElement("outer");
  keywords.addElement("package");
  keywords.addElement("private");
  keywords.addElement("protected");
  keywords.addElement("public");
  keywords.addElement("rest");
  keywords.addElement("return");
  keywords.addElement("short");
  keywords.addElement("static");
  keywords.addElement("super");
  keywords.addElement("switch");
  keywords.addElement("synchronized");
  keywords.addElement("this");
  keywords.addElement("throw");
  keywords.addElement("throws");
  keywords.addElement("transient");
  keywords.addElement("true");
  keywords.addElement("try");
  keywords.addElement("var");
  keywords.addElement("void");
  keywords.addElement("volatile");
  keywords.addElement("while");


  doc.setKeywords(keywords);
  doc.addMethodListener(frame);


  editor.setDocument(doc);


  frame.setSize(400,400);
  frame.getContentPane().add(editor);
  frame.show();
    }
}


class TestFrame extends JFrame imple-
ments MethodListener{
   public void methodAction(ActionEvent
  e){
   this.setTitle( e.getActionCommand()
   );
  }
}


      
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.