4

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.

Martin Quinson
  • 1,347
  • 8
  • 16
David Matuszek
  • 1,263
  • 1
  • 16
  • 30

4 Answers4

4

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.

Hugh
  • 8,872
  • 2
  • 37
  • 42
  • That article looks like it would be what I need, if I could understand it. Unfortunately I've been using Scala like I use Java, that is, letting Eclipse take care of all the building for me. – David Matuszek Feb 15 '15 at 02:36
  • That article looks like it would be what I need, if I could understand it. Unfortunately I've been using Scala like I use Java, that is, letting Eclipse take care of all the building for me. I think that what this means for me, is that I can no longer use Scala with Swing until I learn SBT, Maven, and probably some other IT-level tools that I have no interest in. That's a shame, because I like Scala better than Java, but Swing in Java "just works." – David Matuszek Feb 15 '15 at 02:41
  • 1
    I'm unfamiliar with how Eclipse does dependency management, but all the article's saying is that you need to add an external dependency on the Scala Swing library in exactly the same way you'd add a dependency on an external library in Java. I guess the article only covers the normal build systems because generally people using IDE projects have the IDE take care of dependency management for them. – Hugh Feb 15 '15 at 02:58
  • Good, thanks! Yes, all I needed to do was locate the jar file in the Scala distribution and add it to the build path. Now I feel dumb for getting confused by all the stuff about modifying configuration files and using SBT. Don't serious Scala users (which I don't claim to be) use IDEs? Anyway, that was all the clue I needed to get my old programs running again, so many thanks! – David Matuszek Feb 15 '15 at 03:37
  • Generally, yeah, IDEs get used, but rather than using the IDE's native build tooling (which is inconvenient for things like continuous integration – you don't necessarily want your build server firing up Eclipse just to test your program), generally external build tools instead. SBT and Maven are I think the most common for Scala stuff, although I've heard good things about Gradle. Note this doesn't preclude using an IDE! Both IntelliJ and Eclipse will quite happily import e.g. a Maven project. – Hugh Feb 15 '15 at 08:46
1

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.

Dhara N.
  • 11
  • 1
0

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.

Martin Quinson
  • 1,347
  • 8
  • 16
0

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.

zimon
  • 171
  • 1
  • 10