JRuby Swing NoMethodError

Hey, guys. I am new to ruby, and am trying to make a swing application
in
it. The code is kind of a mess; I designed the GUI in java using
NetBeans
and am attempting to translate the code to ruby. I’ve imported
javax.swing.GroupLayout.Alignment, but get an error at the import.

The error is:
NoMethodError: undefined method ‘Alignment’ for
Java::JavaxSwing::GroupLayout:Class

But javax.swing.GroupLayout does have an Alignment method, right? Can
anyone
point out the mistake I am making?

Thanks,
Jake

Hi,

some sample code could help to identify the problem :wink:

BTW: I would suggest to rebuild the GUI with a decent layout manager
like MigLayout.

Regards
Roger

Am 19.06.2011 um 16:52 schrieb Jake J.:

I apologize. Thanks for the response. I am looking into MigLayout. When
you
see my code you are going to puck. Anyhoo, I played with it some more a
found out I couldn’t reference constants with ‘.’ notation.

Example:

I was using *
import* javax.swing.GroupLayout.Alignment
when I should have, I believe, being using
*import javax.swing.GroupLayout::Alignment
*
I modified the code and am now getting this error:

 *TypeError: HORIZONTAL
 javax.swing.GroupLayout$SequentialGroup 1ad0839 , size=0,

alignment=null prefs
=[0 0 0]
javax.swing.GroupLayout$ParallelGroup f6f1b6 , size=0,
alignment=null
prefs=
[0 0 0]

VERTICAL
javax.swing.GroupLayout$SequentialGroup 771eb1 , size=0,
alignment=null
prefs=
[0 0 0]
javax.swing.GroupLayout$ParallelGroup 801059 , size=0,
alignment=null
prefs=
[0 0 0]
is not a class/module
initUI at CompareOperations.rb:91
initialize at CompareOperations.rb:32
(root) at CompareOperations.rb:162*

I am really not sure how to decipher that. Now, the code.

The Horrible Truth:

*#!/usr/local/bin/jruby

include Java

import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.JPanel
import java.lang.System
import java.awt.Color
import javax.swing.JLabel
import java.awt.Dimension
import javax.swing.JTextArea
import javax.swing.SwingConstants
import javax.swing.BorderFactory
import javax.swing.GroupLayout
import javax.swing.GroupLayout::Alignment
import javax.swing.JScrollPane
import java.awt.Font
import javax.swing.LayoutStyle
import java.lang.Short

class GUI < JFrame

def initialize
    super "GUI"

    self.initUI
end

def initUI

    layout = GroupLayout.new self.getContentPane
    self.getContentPane.setLayout layout
    self.setBackground Color.new 66, 66, 66

    #self.setPreferredSize Dimension.new 900,660

    #initialize components
    oldLabel = JLabel.new "Legacy"
    newLabel = JLabel.new "New"
    oldTextArea = JTextArea.new
    newTextArea = JTextArea.new
    oldScroll = JScrollPane.new
    newScroll = JScrollPane.new
    clearButton1 = JButton.new "Clear"
    clearButton2 = JButton.new "Clear"
    runButton = JButton.new "Run"
    quitButton = JButton.new "Quit"

    #set compnent attributes
    oldLabel.setFont Font.new("Tahoma", 1, 11)
    newLabel.setFont Font.new("Tahoma", 1, 11)

    oldTextArea.setEditable true
    oldTextArea.setBackground Color.new 232, 232, 234
    oldTextArea.setColumns 20
    oldTextArea.setRows 5
    oldTextArea.setBorder BorderFactory.createLineBorder Color.new 

20,
26, 40
oldScroll.setViewportView oldTextArea

    newTextArea.setEditable true
    newTextArea.setBackground Color.new 232, 232, 234
    newTextArea.setColumns 20
    newTextArea.setRows 5
    newTextArea.setBorder BorderFactory.createLineBorder Color.new 

20,
26, 40
newScroll.setViewportView newTextArea

    clearButton1.setBackground Color.new 20, 26, 40
    clearButton1.setForeground Color.white

    clearButton2.setBackground Color.new 20, 26, 40
    clearButton2.setForeground Color.white

    runButton.setFont Font.new("Tahoma", 1, 11)
    runButton.setBackground Color.new 20, 26, 40
    runButton.setForeground Color.white

    quitButton.setBackground Color.new 20, 26, 40
    quitButton.setForeground Color.white
    quitButton.add_action_listener do |e|
        System.exit 0
    end
    #end set comonent attributes

    #design layout
    layout.setHorizontalGroup(layout \
        .createParallelGroup(Alignment::LEADING)\
        .addGroup(layout.createSequentialGroup \
            .addGap(31, 31, 31) \
            .addGroup(layout.createParallelGroup(Alignment::LEADING) 


.addComponent(oldLabel)
.addComponent(oldScroll,
layout::PREFERRED_SIZE,
429,
layout::PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup
.addGap(10, 10, 10)
.addComponent(clearButton1)))
.addPreferredGap(SComponentPlacement::RELATED)
.addGroup(layout.createParallelGroup(Alignment::LEADING)

.addComponent(newScroll,
layout::PREFERRED_SIZE,
429,
layout::PREFERRED_SIZE)
.addComponent(newLabel)
.addGroup(layout.createSequentialGroup
.addGap(10,10,10)
.addComponent(clearButton2)))
.addContainerGap(32, Short::MAX_VALUE))
.addGroup(Alignment::TRAILING,
layout.createSequentialGroup
.addContainerGap(425, Short::MAX_VALUE) \

.addGroup(layout.createParallelGroup(Alignment::LEADING)
.addComponent(quitButton)
.addComponent(runButton))
.addGap(447, 447, 447)))

    layout.setVerticalGroup(layout \
        .createParallelGroup(Alignment::LEADING) \
        .addGroup(layout.createSequentialGroup \
            .addGap(17, 17, 17) \
            .addGroup(layout.createParallelGroup(Alignment::TRAILING) 


.addComponent(oldLabel)
.addComponent(newLabel))
.addPreferredGap(ComponentPlacement::RELATED)
.addGroup(layout.createParallelGroup(Alignment::LEADING,
false)
.addComponent(newScroll)
.addComponent(oldScroll,
layout::DEFAULT_SIZE,
521,
layout::MAX_VALUE)
.addPreferredGap(ComponentPlacement::RELATED) \

.addGroup(layout.createParallelGroup(Alignment::BASELINE)
.addComponent(clearButton2)
.addComponent(clearButton1))
.addGap(18, 18, 18)
.addComponent(runButton)
.addPreferredGap(ComponentPlacement::RELATED,
DEFAULT_SIZE,
MAX_VALUE)
.addComponent(quitButton)
.addContainerGap)))

    layout.linkSize SwingConstants::HORIZONTAL, quitButton,
                    runButton, clearButton1, clearButton2

    self.pack
    #end design layout

    self.setDefaultCloseOperation JFrame::EXIT_ON_CLOSE
    self.setLocationRelativeTo nil
    self.setVisible true
end

end

GUI.new
*

Hi,

first of all SComponentPlacement (or ComponentPlacement) should be
LayoutStyle::ComponentPlacement. PREFERRED_SIZE is a static int from
the GroupLayout class and should be accessed like:
GroupLayout::PREFERRED_SIZE

HTH
Roger

Am 19.06.2011 um 22:15 schrieb Jake J.:

Hi Jake,

On Mon, Jun 20, 2011 at 8:17 PM, Jake J. [email protected]
wrote:

Oh, yeah. Thanks. I hadn’t yet noticed that I missed those. I have since
moved on to trying to grok this tutorial:
http://zetcode.com/tutorials/jrubyswingtutorial/layout/ . Specifically, the
windows example. The code is cleaner, and I wont have to do as much
refactoring later when I understand everything.

I would really advise to keep miglayout instead :slight_smile:

I’m not positive that it’s correct, but I’m hoping.
Below is an ascii diagram of what I am trying to create.
(…)

I made a small pastie of this using miglayout: http://pastie.org/2100121

As you can see, miglayout code is also very clean and easy to maintain.

Remember to add miglayout.jar to your jruby/lib (easiest setup).

I did not focus on the gaps yet. It should be fairly easy to add
according to the quickstart guide of miglayout.


Christian

Thanks very much! That code does look really clean. I looked at
miglayout
the other day, but was afraid that it may take to long for me to figure
out
the library and setting it up. So all I have to do is place the jar in
jruby/lib? Awesome. That seems way better than setting everything up
with
java. Do I only need miglayout.jar? I noticed there are a lot of
miglayout
jar files for different libraries, like swing and awt. This looks like
the
route to take. I am on a deadline, so I shied away from learning
something I
was completely unfamiliar with at first.

Thanks for being patient with me guys!

On Tue, Jun 21, 2011 at 3:01 AM, Christian MICHON <

On Tue, Jun 21, 2011 at 5:19 PM, Jake J. [email protected]
wrote:

Thanks very much! That code does look really clean. I looked at miglayout
the other day, but was afraid that it may take to long for me to figure out
the library and setting it up. So all I have to do is place the jar in
jruby/lib? Awesome. That seems way better than setting everything up with
java. Do I only need miglayout.jar? I noticed there are a lot of miglayout
jar files for different libraries, like swing and awt. This looks like the
route to take. I am on a deadline, so I shied away from learning something I
was completely unfamiliar with at first.

Glad you liked it: no problem sharing it :slight_smile:

You only need to download miglayout-3.7.4.jar (200k) and put it in
jruby/lib.

You’ll still need a bit of java knowledge to bind your buttons to
events with ActionListener.

Thanks for being patient with me guys!

This is what the list is for…

moved on to trying to grok this tutorial:

Christian


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Christian

Oh, yeah. Thanks. I hadn’t yet noticed that I missed those. I have since
moved on to trying to grok this tutorial:
http://zetcode.com/tutorials/jrubyswingtutorial/layout/ . Specifically,
the
windows example. The code is cleaner, and I wont have to do as much
refactoring later when I understand everything. The part I can’t seem to
wrap my head around is that veritical part of the layout design. i.e.:

  sg1 = layout.createSequentialGroup
  sg2 = layout.createSequentialGroup
  pg1 = layout.createParallelGroup
  pg2 = layout.createParallelGroup
  sg1.addComponent windows
  pg1.addComponent area
  sg2.addComponent activateButton
  sg2.addComponent closeButton
  pg1.addGroup sg2
  sg1.addGroup pg1
  pg2.addComponent helpButton
  pg2.addComponent okButton
  sg1.addGroup pg2
  layout.setVerticalGroup sg1

I’ve come up with this for the horizontal layout:

  *sg = layout.createSequentialGroup*
  •  pg1 = layout.createParallelGroup*
    
  •  pg2 = layout.createParallelGroup*
    
  •  pg3 = layout.createParallelGroup*
    
  •  pg1.addComponent windows*
    
  •  pg1.addComponent area*
    
  •  pg1.addComponent activateButton*
    
  •  sg.addGroup pg1*
    
  •  pg2.addComponent helpButton*
    
  •  pg2.addComponent okButton*
    
  •  sg.addGroup pg2*
    
  •  pg3.addComponent windows2*
    
  •  pg3.addComponent area2*
    
  •  pg3.addComponent closeButton*
    
  •  sg.addGroup pg3*
    
  •  layout.setHorizontalGroup sg
    

I’m not positive that it’s correct, but I’m hoping.
*
Below is an ascii diagram of what I am trying to create.


| *Label 1 * Label 2 * |
| | Text Area1 | | Text Area 2 | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| |
| | *| |
| [ClearButton1] [ClearButton2] |
| |
| [ Run ] |
| [ Quit ] |
|________________________________________________________|

Any help on groking GroupLayout or how to design this GUI would be
greatly
appreciates.

Thanks again guys,
Jake