File: swing_callback.pl

package info (click to toggle)
libinline-java-perl 0.58~dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 964 kB
  • ctags: 684
  • sloc: perl: 4,717; java: 2,844; makefile: 35
file content (83 lines) | stat: -rw-r--r-- 1,647 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/perl
use strict;
use warnings;
use blib ;

use Inline Java => "DATA" ;


package EventHandler ;

sub new {
	my $class = shift ;
	my $max = shift ;

	return bless({max => $max, nb => 0}, $class) ;
}


sub button_pressed {
	my $this = shift ;
	my $button = shift ;

	$this->{nb}++ ;
	print "Button Pressed $this->{nb} times (from perl)\n" ;
	if ($this->{nb} > $this->{max}){
		$button->StopCallbackLoop() ;
	}
 
	return $this->{nb} ;
}


my $button = MyButton->new(new EventHandler(10));
$button->StartCallbackLoop() ;
print "loop done\n" ;



package main ;

__DATA__
__Java__

import java.util.*;
import org.perl.inline.java.*;
import javax.swing.*;
import java.awt.event.*;

public class MyButton extends    InlineJavaPerlCaller
                      implements ActionListener
{
  InlineJavaPerlObject po = null ;

  public MyButton(InlineJavaPerlObject _po) throws InlineJavaException
  {
    po = _po ;
    // create frame
    JFrame frame = new JFrame("MyButton");
    frame.setSize(200,200);

    // create button
    JButton button = new JButton("Click Me!");
    frame.getContentPane().add(button);

    // tell the button that when it's clicked, report it to
    // this class.
    button.addActionListener(this);

    // all done, everything added, just show it
    frame.show();
  }

  public void actionPerformed(ActionEvent e)
  {
    try
    {
      String cnt = (String)CallPerlMethod(po, "button_pressed", new Object [] {this});
      System.out.println("Button Pressed " + cnt + " times (from java)") ;
    }
    catch (InlineJavaPerlException pe)  { }
    catch (InlineJavaException pe) { pe.printStackTrace() ;}
  }
}