atom feed5 messages in org.codehaus.jruby.userRe: [jruby-user] problem with Swing E...
FromSent OnAttachments
Markus JaisNov 15, 2006 4:31 am 
Charles Oliver NutterNov 15, 2006 5:11 am 
Markus JaisNov 15, 2006 5:52 am 
Thomas E EneboNov 15, 2006 7:05 am 
Markus JaisNov 15, 2006 12:14 pm 
Subject:Re: [jruby-user] problem with Swing Example
From:Thomas E Enebo (tom.@gmail.com)
Date:Nov 15, 2006 7:05:07 am
List:org.codehaus.jruby.user

I got your first example to work. In addition to Charles super comment you had two typos. Here is something that will work:

require 'java'

include_class "java.util.HashMap" include_class "java.awt.event.ActionListener" include_class "java.awt.FlowLayout" include_class ["JButton", "JFrame", "JLabel", "JOptionPane", "JTextField"].map {|e| "javax.swing." + e}

class SpanishDictionary def initialize @words = HashMap.new @words["eins"] = "uno" @words["zwei"] = "dos" @words["drei"] = "tres" end

def translate(german_word) !@words[german_word].nil? ? @words[german_word] : "keine ahnung" end end

class DictGui < ActionListener

def actionPerformed(evt) command = evt.getActionCommand() result = "" if command == "translate" result = dict.translate(@textField.getText()) else puts "should never happen" exit(0) end JOptionPane.showMessageDialog(null, "<html>Translation: " + result); end

def initialize super @dict = SpanishDictionary.new @textField = JTextField.new(20)

frame = JFrame.new("Hello Swing") frame.setSize(250, 100); frame.setLayout(FlowLayout.new); translateButton = JButton.new("translate me") translateButton.addActionListener(self); frame.add(@textField); frame.add(translateButton); frame.setDefaultCloseOperation(JFrame::EXIT_ON_CLOSE); frame.setVisible(true); end end

gui = DictGui.new

On 11/15/06, Markus Jais <mark@yahoo.de> wrote:

hello

I am playing with JRuby and Swing but I can not get the example running. I have 0.9.1. on Linux with Java 5.

the first example:

====================

require 'java'

include_class "java.util.HashMap" include_class "java.awt.event.ActionListener" include_class "java.awt.FlowLayout" include_class ["JButton", "JFrame", "JLabel", "JOptionPane", "JTextField"].map {|e| "javax.swing." + e}

class SpanishDictionary def initialize @words = HashMap.new @words["eins"] = "uno" @words["zwei"] = "dos" @words["drei"] = "tres" end

def translate(german_word) !@words[german_word].nil? ? @words[german_word] : "keine ahnung" end end

class DictGui < ActionListener

def actionPerformed(evt) command = evt.getActionCommand() result = "" if command == "translate" result = dict.translate(@textField.getText()) else puts "should never happen" exit(0) end JOptionPane.showMessageDialog(null, "<html>Translation: " + result); end

def initialize @dict = SpanishDictionary.new @textfield = JTextField.new(20)

frame = JFrame.new("Hello Swing") frame.setSize(250, 100); frame.setLayout(FlowLayout.new); translate_button = JButton.new("translate me") translate_button.addActionListener(self); frame.add(@textField); frame.add(translateButton); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); end end

gui = DictGui.new ====================== this is the error:

$ jruby swing.rb C:\jruby-0_9_1\src\builtin\javasupport.rb:339:in `method_missing': undefined method `java_class' for nil:NilClass (NoMethodError) from C:\jruby-0_9_1\src\builtin\javasupport.rb:339:in `matching_method' from swing.rb:44:in `collect' from swing.rb:44:in `matching_method' from swing.rb:44:in `addActionListener' from swing.rb:44:in `initialize' from swing.rb:53:in `new' from swing.rb:53

this is the 2nd attempt: ==========================

require 'java'

include_class "java.util.HashMap" include_class "java.awt.event.ActionListener" include_class "java.awt.FlowLayout" include_class ["JButton", "JFrame", "JLabel", "JOptionPane", "JTextField"].map {|e| "javax.swing." + e}

class SpanishDictionary def initialize @words = HashMap.new @words["eins"] = "uno" @words["zwei"] = "dos" @words["drei"] = "tres" end

def translate(german_word) !@words[german_word].nil? ? @words[german_word] : "keine ahnung" end end

class DictGui

class ClickAction < ActionListener def actionPerformed(evt) command = evt.getActionCommand() result = "" if command == "translate" result = dict.translate(@textField.getText()) else puts "should never happen" exit(0) end JOptionPane.showMessageDialog(null, "<html>Translation: " + result); end end

def initialize @dict = SpanishDictionary.new @textfield = JTextField.new(20)

frame = JFrame.new("Hello Swing") frame.setSize(250, 100); frame.setLayout(FlowLayout.new); translate_button = JButton.new("translate me") translate_button.addActionListener(ClickAction.new); frame.add(@textField); frame.add(translateButton); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); end end

gui = DictGui.new

=======================

Error: java.awt.Container.addImpl(Container.java:1015): java.lang.NullPointerException: null (NativeException) from java.awt.Container.add(Container.java:928) from javax.swing.JFrame.addImpl(JFrame.java:479) from java.awt.Container.add(Container.java:351) from sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) from sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) from sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) from java.lang.reflect.Method.invoke(Method.java:585) from org.jruby.javasupport.JavaMethod.invokeWithExceptionHandling(JavaMethod.java:180) ... 96 levels... from swing3.rb:48:in `initialize' from swing3.rb:56:in `new' from swing3.rb:56 Complete Java stackTrace java.lang.NullPointerException at java.awt.Container.addImpl(Container.java:1015) at java.awt.Container.add(Container.java:928) at javax.swing.JFrame.addImpl(JFrame.java:479) at java.awt.Container.add(Container.java:351) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at org.jruby.javasupport.JavaMethod.invokeWithExceptionHandling(JavaMethod.java:180) at org.jruby.javasupport.JavaMethod.invoke(JavaMethod.java:156) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at org.jruby.runtime.callback.ReflectionCallback.execute(ReflectionCallback.java:140) at org.jruby.internal.runtime.methods.CallbackMethod.internalCall(CallbackMethod.java:79) at org.jruby.internal.runtime.methods.AbstractMethod.call(AbstractMethod.java:58) at org.jruby.RubyObject.callMethod(RubyObject.java:379) at org.jruby.RubyObject.callMethod(RubyObject.java:323) at org.jruby.javasupport.JavaClass$1.execute(JavaClass.java:261) at org.jruby.internal.runtime.methods.CallbackMethod.internalCall(CallbackMethod.java:79) at org.jruby.internal.runtime.methods.AbstractMethod.call(AbstractMethod.java:58) at org.jruby.RubyObject.callMethod(RubyObject.java:379) at org.jruby.RubyObject.callMethod(RubyObject.java:331) at org.jruby.evaluator.EvaluationState.eval(EvaluationState.java:305) at org.jruby.evaluator.EvaluationState.eval(EvaluationState.java:933) at org.jruby.evaluator.EvaluationState.eval(EvaluationState.java:222) at org.jruby.RubyObject.eval(RubyObject.java:453) at org.jruby.internal.runtime.methods.DefaultMethod.internalCall(DefaultMethod.java:112) at org.jruby.internal.runtime.methods.AbstractMethod.call(AbstractMethod.java:58) at org.jruby.RubyObject.callMethod(RubyObject.java:379) at org.jruby.RubyObject.callMethod(RubyObject.java:323) at org.jruby.RubyObject.callInit(RubyObject.java:461) at org.jruby.RubyClass.newInstance(RubyClass.java:174) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at org.jruby.runtime.callback.ReflectionCallback.execute(ReflectionCallback.java:140) at org.jruby.internal.runtime.methods.CallbackMethod.internalCall(CallbackMethod.java:79) at org.jruby.internal.runtime.methods.AbstractMethod.call(AbstractMethod.java:58) at org.jruby.RubyObject.callMethod(RubyObject.java:379) at org.jruby.RubyObject.callMethod(RubyObject.java:331) at org.jruby.evaluator.EvaluationState.eval(EvaluationState.java:305) at org.jruby.evaluator.EvaluationState.eval(EvaluationState.java:869) at org.jruby.evaluator.EvaluationState.eval(EvaluationState.java:933) at org.jruby.evaluator.EvaluationState.eval(EvaluationState.java:222) at org.jruby.Ruby.eval(Ruby.java:235) at org.jruby.Main.runInterpreter(Main.java:177) at org.jruby.Main.runInterpreter(Main.java:146) at org.jruby.Main.run(Main.java:111) at org.jruby.Main.main(Main.java:86)

this is probably just some stupid error I made, but I can not see the solution.

any help welcome.

Markus

-- Markus Jais http://www.mjais.de

___________________________________________________________ Telefonate ohne weitere Kosten vom PC zum PC: http://messenger.yahoo.de

--------------------------------------------------------------------- To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email