How do I use Swing with Scala 2.11 in Eclipse?

3.2k views Asked by At

I just upgraded Scala to 2.11, and now I can't import scala.swing. How do I recover from this? Can I just download a jar file, or something simple like that? I hope I don't have to build anything.

Edit: the solution should be usable in Eclipse without building anything from source, if possible.

4

There are 4 answers

5
Hugh On

Here is an example of how to update your dependencies for this.

In 2.11 many parts of the standard lib were split out into their own modules, including the Swing stuff.

0
Martin Quinson On

Simply add scala-swing.jar to the dependencies of your project.

You can retrieve the pre-compiled file from the project webpage. On top of the README file, you have blue icons pointing to the existing releases. Right now, the current release is for Scala v2.11. On this page, the link to download the jar file is on the right.

Once you've downloaded the jarfile, open eclipse and get to the properties of your project (right-click on project root / Properties). In the Java Build Path tab, add the path to the file that you just downloaded.

Note that in my case, I had to also add a scala-library.jar (found in the zip file that you can download from the Scala main page). The Problem tab in Eclipse helped me to debug that issue.

0
Dhara N. On

I have same Problem,But after follow some step ,I got success to use Scala.swing.

Here are some Steps that I follow,hope it will help you.

1)add external jar to javaBuildPath(Right click on Project ->Proprties-> javaBuildPath->add external Jars-> add scala.swing.x)

2)after do this I Over come typeNotFound errors

3) create class in that you want to do code for swing

4) create object and in main() you have to create above class object by new keyword

5)After doing this, it allows you to access methods of SimpleSwingApplication, so call startup().

6) Thats all,

Consider following example if might be helps you to understand what i mean to say,

 class FirstUI  extends SimpleSwingApplication
{

       def top= new MainFrame {
       title = "GUI Program #1"
       preferredSize = new Dimension(320, 240)
       contents = new Label("Here is the contents!")

  }

}

    object First
    {
      def main(str: Array[String])
      {
            val f =new FirstUI
            f.startup(str)
      }
    }

save this file as First.scala and Run it.Hope this will help you.

0
zimon On

I struggled a lot with this mess. I also needed both scala-swing.jar and scala-library.jar from /usr/share/java/scala/ to be included as External JARs to the project. (In properties > Java Build Path > Libraries)

Then also JRE System Library couldn't be just any available. For example "Workspace default: JRE (java-1.8.0-openjdk-1.8.0.212.b04-0.fc29.x86)" did not work, but "Execution environment: JavaSE-1.8 (java-1.8.0-openjdk-1.8.0.212.b04-0.fc29.x86_64)" did work. I do not know what is the difference adding it as "workspace default" vs "execution environment"

Also at least in Fedora 29, Eclipse got errors if I didn't add "Scala Library container", although I already had scala-library.jar as an External JAR.

But then got errors when compiling scala: "The version of scala library found in the build path of plotchartlib (2.10.6) is prior to the one provided by scala IDE (2.12.3). Setting a Scala Installation Choice to match."

What I needed still do, is in properties > Java Build Path > Order and Export, had to put the "Scala library container [2.12.3]" to the bottom and have scala-library.jar above it.

I have no idea why it was so difficult, is it because how Fedora 29 has Eclipse configured, but took me 2 working days to get it working.