Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Rendering a simple graphic hello world in Java (using Perl)

by metaperl (Curate)
on Jul 27, 2011 at 21:24 UTC ( #917139=perlquestion: print w/replies, xml ) Need Help??

metaperl has asked for the wisdom of the Perl Monks concerning the following question:

It appears that Inline::Java is the best maintained and robust way to manipulate Java classes from Perl, whether those classes are built on-the-fly or already in Java classes.

Let's be literal

The Clojure website shows a one-line Java Swing example

(javax.swing.JOptionPane/showMessageDialog nil "Hello World")
that I would like to get working in Inline::Java

However, my attempt at doing so:

use Inline ( Java => 'STUDY', STUDY => ['javax.swing.JOptionPane'] ); javax::swing::JOptionPane->showMessageDialog("Hello World");
leads to the error:
In method showMessageDialog of class javax::swing::JOptionPane: Can't +find any signature that matches the arguments passed (Hello World). Available signatures are: static showMessageDialog(java.awt.Component, java.lang.Object, jav +a.lang.String, int) error was: Wrong number of arguments at C:/strawberry/perl/sit +e/lib/Inline/Java/Object.pm line 107 static showMessageDialog(java.awt.Component, java.lang.Object, jav +a.lang.String, int, javax.swing.Icon) error was: Wrong number of arguments at C:/strawberry/perl/sit +e/lib/Inline/Java/Object.pm line 107 static showMessageDialog(java.awt.Component, java.lang.Object) error was: Wrong number of arguments at C:/strawberry/perl/sit +e/lib/Inline/Java/Object.pm line 107 at C:\Users\thequietcenter\prg\t +mp\inline\try2.pl line 6

Apparently some closure magick prepared two other args before making the call. Since I know ZERO java, I have no idea what those

Phase 2: let's try something else

When the message approach failed, I did not give up. Instead, I googled for java swing hello world and came up with this beauty:
package start; /* * HelloWorldSwing.java requires no other files. */ import javax.swing.*; public class HelloWorldSwing { /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("HelloWorldSwing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Add the ubiquitous "Hello World" label. JLabel label = new JLabel("Hello World"); frame.getContentPane().add(label); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
(sans 20 lines of copyright notices)

Now, I almost got it working in Inline::Java

use Inline ( Java => 'STUDY', STUDY => [ qw(javax.swing.JFrame javax.swing.JLabel java.awt.Contain +er) ] ); my $f = javax::swing::JFrame->new; my $l = javax::swing::JLabel->new("Hello World"); $f->getContentPane->add($l); $f->pack; #$f->setVisible
but now I get the error
Can't call method 'add' on an object (Inline::Java::Object) that is not bound to Perl at C:\Users\thequietcenter\prg\tmp\inline\try2.pl line 13

lets get this done

So any help on a graphic hello world in Perl using Java is requested

Replies are listed 'Best First'.
Re: Rendering a simple graphic hello world in Java (using Perl)
by pobocks (Chaplain) on Jul 28, 2011 at 01:49 UTC

    I would suggest checking out the documentation for JOptionPane. If you'll look, you'll see that JOptionPane has a required first argument which specifies a parent frame - if you pass it a null, I think this should work. It's been roughly FOREVER since I did any Java, so check it for yourself.

    Docs appear to be here, based on lazy googling. http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/JOptionPane.html

    for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";

      That sounds good

      nil means null :)

      (javax.swing.JOptionPane/showMessageDialog nil "Hello World")

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://917139]
Approved by Tanktalus
Front-paged by Tanktalus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2023-02-03 22:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (30 votes). Check out past polls.

    Notices?