00001 /* Example for Java-side UI construction, with event handler setup on the Prolog side 00002 */ 00003 package com.declarativa.interprolog.examples; 00004 import com.declarativa.interprolog.*; 00005 import javax.swing.*; 00006 import java.awt.*; 00007 import java.awt.event.*; 00008 // Hello World, Prolog-biased implementation; the Java part knows nothing about event handling 00009 public class HelloWindow2 extends JFrame{ 00010 public HelloWindow2(PrologEngine pe /* this argument not really needed, cf. comments below */){ 00011 super("Java-Prolog-Java call test2"); 00012 JTextField text = new JTextField(15); 00013 text.setBorder(BorderFactory.createTitledBorder("text")); 00014 JButton button = new JButton("Greet"); 00015 Box box = new Box(BoxLayout.Y_AXIS); 00016 box.add(text); box.add(button); 00017 getContentPane().add(box); 00018 setSize(200,100); validate(); show(); 00019 // The following 2 lines are not strictly necessary, as long as someone is able to register 00020 // button and text with an engine of choice, hence the constructor argument above 00021 // Concluding, pe and these 2 messages below are just for the benefit of dynamically introspecting 00022 // into 2 graphical objects, telling our engine about them, and letting the Prolog 00023 // programmer know their allocated IDs, nothing more 00024 System.out.println("Button ID:"+pe.registerJavaObject(button)); 00025 System.out.println("Text ID:"+pe.registerJavaObject(text)); 00026 } 00027 } 00028 /* 00029 To create the window, in Prolog call: 00030 ipPrologEngine(Engine), javaMessage('HelloWindow2','HelloWindow2'(Engine)). 00031 00032 watch the console window and write down the Text and Button IDs 00033 00034 Then, assuming we have defined in Prolog an event handler predicate, like: 00035 assert( ( greetat(TextID) :- javaMessage( TextID, setText( string('Hello world!')) )) ). 00036 00037 ...we can now call: 00038 ipPrologEngine(Engine), buildTermModel(greetat(TextID),TM), 00039 javaMessage('com.declarativa.interprolog.PrologEventBroker',R,'PrologEventBroker'(Engine,TM)), 00040 javaMessage(ButtonID,addActionListener(R)). 00041 00042 */