Greetings!
I sure hope this is the appropriate place to ask (perldoc Java::Swing at
least says so), if it's not, please tell me so.
The Story:
I'm pretty used to creating my GUI's via Inline::Java. Recently I've
discovered Java::Swing, which I now prefer (out of lazyness, of course
;-> ), because my mind doesn't have to switch between Java and Perl anymore.
What I like most about Java::Swing is that it allows me to code
something like this:
my $label = JLabel->new(
{
Text => "Hello World",
HorizontalTextPosition => 0,
HorizontalAlignment => 0,
}
);
and all the Hashkeys get translated to Setters on the Object:
setText("Hello World"), setHorizontalTextPosition(0), and so on.
The Concern:
If i want my $label to hold a picture ( for example ), things start to
get difficult.
I can no longer call JLabel->new() with an anonnymous hash (hm, ok, I
can, but then my $label doesn't know about the $picture it's supposed to
hold, which is bad, because I can't add() it afterwards). I have to call
JLabel->new($picture), and then I have to call each and every Setter
manually ($label->setText("Buhu"),...). I admit, that's not as bad as a
Shot-in-the-Foot(TM), but... you know... lazyness, again.
And now for
The Solution (maybe):
After looking into Swing.pm and studying the code therein, I've decided
to make some minor tweaks (which you will find ( original Code & my
changes ) after __END__ ). These changes allow me to code like this from
now on:
my $top_label = JLabel->new(
{
Object => $picture,
Text => "Hello World",
HorizontalTextPosition => 0,
HorizontalAlignment => 0,
}
);
Now, after a somewhat lengthy & boring post ( If you are still reading
this and I haven't bored you off until now, many, many thanks!), finally
my questions:
Does this make any sense?
Am I the only one that lazy that I feel a need for this?
Will it break things I'm not aware of? ( Don't think so, but, hey, what
do I know?!)
Is there a way to achieve the same that I did oversee?
Many Thanks then for your patience, Opinions and Comments very welcome,
Andreas Pürzer
__END__
sub gen_generic_new {
my $package_name = shift;
$package_name =~ s/\./::/g;
return sub {
my $class = shift;
my $inline_class = "Java\::Swing\::$package_name\::$class";
# if we were passed a hash reference, construct a default
# object, then
# call set on each hash key with the value
if (ref($_[0]) =~ /HASH/) { # call no arg
constructor and # accessors
my $attributes = shift;
# __CHANGES__
#my $retval = $inline_class->new();
my $retval;
if ( defined $attributes->{Object} ) {
$retval = $inline_class->new($attributes->{Object});
delete $attributes->{Object};
}
else {
$retval = $inline_class->new();
}
# __END_CHANGES__
foreach my $attribute (keys %$attributes) {
my $setter = "set" . ucfirst($attribute);
eval { $retval->$setter($attributes->{$attribute}); };
if ($@) {
croak "Error: '$attribute' is not an attribute of
$class";
}
}
return $retval;
}
else { # pass args directly to constructor
return $inline_class->new(@_);
}
}
}
--
perl -mAcme::JAPH