Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9012759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:05:23+00:00 2026-06-16T03:05:23+00:00

Is there a convenient way to add a filter to input to a scala.swing.TextComponent,

  • 0

Is there a convenient way to add a filter to input to a scala.swing.TextComponent, so that the user can only input integers/floats/whatever? Especially, is there a way to filter pastes into a text field? In Java, I’ve used a DocumentFilter to do that. I’ve tried a few variations on this:

object inputField extends TextField{

   peer.getDocument.asInstanceOf[AbstractDocument].setDocumentFilter(new DocumentFilter{

      def insertString(fb: FilterBypass, offs: Integer, str: String, a: AttributeSet){
         if(str.forall((c)=>c.isDigit)) super.insertString(fb, offs, str, a)
      }

      def replace(fb: FilterBypass, offs: Integer, l: Integer, str: String, a: AttributeSet){
         if(str.forall((c)=>c.isDigit)) super.replace(fb, offs, l, str, a)
      }
   })
}

Which isn’t working. Am I doing that wrong, or does Scala ignore document filters? Is there another way to do this? I might be ok with using an entirely java.swing GUI if that’s what it takes.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T03:05:24+00:00Added an answer on June 16, 2026 at 3:05 am

    You can achieve this using reactions:

    import swing._
    import swing.event._
    
    object SwingApp extends SimpleSwingApplication {
      def top = new MainFrame {
        contents = new TextField {
          listenTo(keys)
          reactions += { case e: KeyTyped =>
            if (!e.char.isDigit) e.consume
          }
        }
      }
    }
    

    Update:
    Oh, I agree, that fits only relatively trivial cases. I’ve found problem with your original solution: you use Integers where Ints needed, so your methods are just new ones, not implementations of methods of abstract class. Here’s how it works for me:

    contents = new TextField {
      object IntegralFilter extends DocumentFilter {
        override def insertString(fb: FilterBypass, offs: Int, str: String, a: AttributeSet){
           if(str.forall((c)=>c.isDigit)) super.insertString(fb, offs, str, a)
        }    
        override def replace(fb: FilterBypass, offs: Int, l: Int, str: String, a: AttributeSet){
           if(str.forall((c)=>c.isDigit)) super.replace(fb, offs, l, str, a)
        }
      }
    
      peer.getDocument().asInstanceOf[AbstractDocument].setDocumentFilter(IntegralFilter)
    }
    

    Notice the use of “override”. That’s how I got compiler error and corresponding IDE tip, that if I really want to override something with this, I have to change signature.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way I can do git add -A git commit -m commit
I have seen that there is a very convenient way to reverse ArrayList like
Is there a convenient way to write a regex that will try to match
Is there a convenient way to add an event handler to a web control
Is there a convenient way to calculate percentiles for a sequence or single-dimensional numpy
Is there a convenient way to do this for my extension? I know I
Is there any convenient way to type in braces in Math in Latex? Especially
Is there any convenient way to take an array/set of objects and create a
in languages like PHP or Python there are convenient functions to turn an input
I'd like to have a convenient way to add Phone objects to a Phone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.