Question

Hello I am using jruby to write an app and I am currently having problems implementing a DocumentSizeFilter. There are currently two problems I am trying to figure out.

  1. Calling the super.insertString ( or super.repalce which I tried earlier) keeps triggering a method not found in JRuby yet the the method is being called.
  2. Also why is it that upon insertion replace is being called rather than insert?

P.S: Both files are being built using

jruby 1.7.1 (1.9.3p327) 2012-12-03 30a153b on OpenJDK 64-Bit Server VM 1.7.0_09-b30 [linux-amd64]

Here are the test files I am currently using:

#JavaUtilities\DocumentSizeFilter.rb
module JavaUtilities
    class DocumentSizeFilter < DocumentFilter
        def initialize max_char
            super()
            @MAX_CHAR = max_char
        end
        def insertString(fb, offset, string, attrs) 
            puts 'doing insert'
            super.insertString(fb, offset, string, attrs)
        end
        def remove(fb, offset, length) 
            puts 'doing remove'
        end

        def replace(fb,  offset, length, text, attrs) 
            puts 'doing replace'
            super.insertString(fb, offset, text, attrs)
        end
    end
end


    #test.rb
include Java
java_import java.awt.event.WindowEvent;
java_import javax.swing.JButton;
java_import javax.swing.border.Border;
java_import java.awt.Graphics;
java_import javax.swing.JFrame
java_import javax.swing.text.DocumentFilter;
java_import javax.swing.JTextArea;
require 'JavaUtilities/DocumentSizeFilter'
class JFrames < JFrame
    def initialize
        super "HI"
        self.setSize 500,500
        self.setDefaultCloseOperation JFrame::EXIT_ON_CLOSE
        self.setLayout nil
        add(area = (JTextArea.new ))
        area.setBounds 30,30,100,40
        area.getDocument.setDocumentFilter(JavaUtilities::DocumentSizeFilter.new 150) 
        self.setVisible true
    end
end

JFrames.new
Was it helpful?

Solution

Your confusion is what super is in Ruby: it's not a reference, it's a keyword.

All you need to do is call super, e.g.:

def replace(fb,  offset, length, text, attrs) 
  puts 'doing replace'
  super
end

super with no args calls the superclass method with the current method's args. You can call with args, e.g., super foo, bar, or with no args (explicitly requires parens) e.g., super().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top