All,

I'd like some feedback on a new idea for calling Swing
from Perl.  While playing with gtk in Perl, I started
thinking that some people (like me) might like to have
Perl bindings for Swing like Gtk2:: provides for gtk.

The example below is running.  It's just a demo in
which you to type an expression in one text box, press
a button, and have the Perl evaluation of the
expression show up in the other box.  It shows off my
API idea.

#!/usr/bin/perl
use strict;
use warnings;

BEGIN {
    $ENV{CLASSPATH} .=
        ':/usr/src/Inline-Java-0.47/Java/classes:java'
}

use Java::Swing;

my $expression  = JTextField->new();
my $answer      = JTextField->new(
    { columns => 10 }
);
my $submit      = JButton   ->new("Evaluate");
my $frame       = JFrame    ->new();
my $root_pane   = $frame    ->getContentPane();
my $south_panel = JPanel    ->new();

$south_panel->add(JLabel->new("Answer:"), "West"  );
$south_panel->add($answer,                "Center");
$south_panel->add($submit,                "East"  );

$root_pane->add($expression,  "North");
$root_pane->add($south_panel, "South");

$frame->setSize(300, 100);
$frame->show();

my $swinger = Java::Swing->new();
$swinger->connect(
    "ActionListener",
    $submit,
    { actionPerformed => \&evaluate }
);

$swinger->connect(
    "WindowListener",
    $frame,
    { windowClosing => \&ending }
);

$swinger->start();

sub evaluate {
    my $sender_name = shift;
    my $event       = shift;

    $answer->setText(eval $expression->getText());
}

sub ending {
    $swinger->stop();
}

There are three features I like here, but I wonder
whether other people care.

1. the code is in one file which is all perl
2. listeners are hooked up to direct call backs,
   unneeded events handlers are simply omitted and
   default no-op subs are called
3. java widget constructors can take named arguments,
   in which case objects are constructed through
   the empty argument constructor, then named
   arguments are inserted via set calls (regular
   construction is also allowed)

Let me know what you think of this.

Phil Crow


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

Reply via email to