GUI in Swing for JRuby


#1

Hi,
I’m trying to integrate Java GUI with Ruby code. This is my Java which I
exported to .jar and Ruby code : https://gist.github.com/2968288. After
that I tried to run this in jruby by and I got error(which is attached
to gist).


#2

Michał K. wrote in post #1065577:

Hi,
I’m trying to integrate Java GUI with Ruby code. This is my Java which I
exported to .jar and Ruby code : https://gist.github.com/2968288. After
that I tried to run this in jruby by and I got error(which is attached
to gist).

I made a small JRuby GUI program recently using Miglayout. I wrote it
all in JRuby. I think I had a similar problem to you until I figured out
the correct way to reference the Mig jar. My code has
require ‘./jars/miglayout-4.0.jar’
and I have a vague recollection that the ./ is important.

I also have
java_import ‘net.miginfocom.swing.MigLayout’
but that may only be necessary if I am referring to MigLayout from JRuby


#3

Thanks,
My code looks now:

require ‘java’
require ‘./forms-1.3.0.jar’
require ‘./miglayout15-swing.jar’
require ‘./gui.jar’
java_import ‘net.miginfocom.swing.MigLayout’
Java::Gui.main([])

and I still getting

Exception in thread “AWT-EventQueue-0” java.lang.NoClassDefFoundError:
net/miginfocom/swing/MigLayout
at Gui.initialize(Gui.java:67)
at Gui.(Gui.java:49)
at Gui$1.run(Gui.java:36)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:682)
at java.awt.EventQueue.access$000(EventQueue.java:85)
at java.awt.EventQueue$1.run(EventQueue.java:643)
at java.awt.EventQueue$1.run(EventQueue.java:641)
at java.security.AccessController.doPrivileged(Native Method)
at
java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:652)
at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Caused by: java.lang.ClassNotFoundException:
net.miginfocom.swing.MigLayout


#4

Michael,
I don’t know enough about Java to comment on your code. It seems to me
you have all the miglayout stuff in your Java program so the error seems
likely to be there rather than in the JRuby bit. I don’t think there is
any need to put a “require” in JRuby code for stuff that is only used in
Java code. So I suspect you have an error / something missing in the
Java bit.

Can you run the Java stuff without JRuby?

If it’s any help the following works in conjunction with another short
.rb file that sets the values of @message, @t and @d and “requires” the
file with this code (Ignore the stuff that is commented out - it’s from
a different project).

this file holds the display code

class Hello

require ‘java’
java_import ‘javax.swing.JFrame’

java_import ‘javax.swing.JScrollPane’

java_import ‘javax.swing.JButton’
java_import ‘javax.swing.JPanel’
java_import ‘javax.swing.JLabel’
java_import ‘javax.swing.WindowConstants’

java_import ‘javax.swing.ImageIcon’

java_import ‘java.awt.image.BufferedImage’

java_import ‘java.awt.Graphics2D’

java_import ‘javax.imageio.ImageIO’

require ‘./jars/miglayout-4.0.jar’
java_import ‘net.miginfocom.swing.MigLayout’

#=====================

def initialize
# set up the frame to hold everything
startpanel = JPanel.new
@frame = JFrame.new(“helloGUI”)
@frame.setSize(400, 300)
@frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE)
@frame.add(startpanel)

@frame.setVisible(true)

end
#===================

def mainView

layout = MigLayout.new("", "", "[]10[]")

panel = JPanel.new(layout)
# the message
panel.add(JLabel.new(@message), "cell 0 1, wrap")
panel.add(JLabel.new("Date #{@d}"), "wrap")
panel.add(JLabel.new("Time #{@t}"), "wrap")
# the buttons

b1 = JButton.new("Update")
panel.add(b1, "cell 0 4")
b1.setActionCommand("update")
b1.addActionListener do |event|
    btnpress(event.getActionCommand)
end

b2 = JButton.new("Quit")
panel.add(b2, "cell 1 5")
b2.setActionCommand("quit")
b2.addActionListener do |event|
    btnpress(event.getActionCommand)
end


# and place everything in the frame
@frame.getContentPane.removeAll
@frame.add(panel)


@frame.setVisible(true)

end # mainview

#=====================

end # class