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 6015157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:47:44+00:00 2026-05-23T02:47:44+00:00

I tried scala> class Foo extends Component { var font = new java.awt.Font(Helvetica, java.awt.Font.BOLD,

  • 0

I tried

scala> class Foo extends Component { var font = new java.awt.Font("Helvetica", java.awt.Font.BOLD, 12) }

and I got:

<console>:10: error: overriding variable font in class Component of type java.awt.Font;
 variable font needs `override' modifier
       class Foo extends Component { var font = new java.awt.Font("Helvetica", java.awt.Font.BOLD, 12) }
                                         ^ 

So I tried

scala> class Foo extends Component { override var font = java.awt.new Font("Helvetica", java.awt.Font.BOLD, 12) }

but this didn’t help at all:

<console>:10: error: overriding variable font in class Component of type java.awt.Font;
 variable font has incompatible type
       class Foo extends Component { override var font = new java.awt.Font("Helvetica", java.awt.Font.BOLD, 12) }
                                                  ^

What’s the reason behind this error and how should it be done correctly?

EDIT: Sorry, didn’t saw that scala also has Component. The Component in question is the one from java.awt.Component!

  • 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-05-23T02:47:45+00:00Added an answer on May 23, 2026 at 2:47 am

    Component contains getters and setters for font, so the canonical way to set the font is:

    class Foo extends Component {
      font = new java.awt.Font(/* blah blah */)
    }
    

    If you want to override the font defs to use a var, you have to use something compatible with scala.swing.Font, which wraps the Java fonts (normally done with implicit conversions). Like so:

    class Foo extends Component {
      override var font: scala.swing.Font =
        new java.awt.Font("Helvetica", java.awt.Font.BOLD, 12)
    }
    

    I’m not sure this will do what you want. (Edit: tested in a REPL session with too much junk loaded; you don’t actually need to do this! The simple override var font = new java.awt.Font thing works.) The normal getter/setter is designed to pass the information on through to the javax.swing peer. If you do it this way, you’ll probably break that forwarding. So use the first method.


    Edit if you are trying to do this in a java.awt.Component: Font font; is a private field in java.awt.Component. You can’t override fields in Java, and you can’t do anything with private fields in Java. So trying to override it with a new var is guaranteed to not work. (The compiler could certainly give a more informative error message, however!) Scala can only override fields because they’re not really fields–they’re a getter/setter pair referring to a hidden underlying field.

    You should use (or override) getFont and setFont instead.

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

Sidebar

Related Questions

I'm trying to use some Java-classes with Clojure. I've tried it with Scala with
I tried to shrink and obfuscate my Scala/Java program using Proguard. While I was
I have defined a class in Scala (2.9.1) as follows: class A(val neighbors: Array[Option[A]])
I tried to combine implicit arguments with case classes, but I got stuck. case
I'm new to web development but have plenty of Scala experience and am trying
I've tried the code below (the equal method is written after Programming in Scala
I tried to play with scala and somethings wrong here. Maybe because its too
I am trying to implement C#'s ExpandoObject -like class in Scala. This is how
I would like to call the following java method from scala: protected final FilterKeyBindingBuilder
I am using Scala combinatorial parser by extending scala.util.parsing.combinator.syntactical.StandardTokenParser . This class provides following

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.