Question

I have just started playing with Ceylon and I really like it...

But I ran into this problem when using Swing... I want to add components to a JPanel using a BorderLayout.

This is the code I am using:

import javax.swing {
  JLabel,
  SwingUtilities { invokeLater },
  JFrame { exitOnClose = \iEXIT_ON_CLOSE },
  JButton,
  JPanel
}
import java.lang { Runnable }
import java.awt {
  Dimension,
  BorderLayout { north = \iNORTH, center = \iCENTER }
}

class MySwingApp() satisfies Runnable {

  shared actual void run() {
    value frame = JFrame();
    frame.title = "Renato app";
    frame.defaultCloseOperation = exitOnClose;
    frame.size = Dimension(300, 200);
    frame.setLocationRelativeTo(null);

    value panel = JPanel();
    panel.layout = BorderLayout();

    frame.add(panel);

    panel.add(JLabel("Hello world"), north);
    panel.add(JButton("Click me"), center);
    frame.visible = true;
  }

}

The error is:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null)
at java.awt.BorderLayout.addLayoutComponent(BorderLayout.java:426)
at java.awt.Container.addImpl(Container.java:1120)
at java.awt.Container.add(Container.java:966)
at firstModule.MySwingApp.run(run.ceylon:52)

I run the app with:

invokeLater(MySwingApp());

This appears to me to be a problem mapping Strings in Ceylon?!? Can anyone see anything I'm doing wrong (being new to Ceylon I wouldn't be surprised)??

Was it helpful?

Solution

What's happening here is that Container.add()'s second parameter is declared to be an Object, not a java.lang.String so the Ceylon compiler doesn't realize there's a need to unbox the Ceylon String. According to the signature of the method any Object is acceptable, it's just that the implementation of the method decides it actually need a Java String.

You can use the javaString() function from the ceylon.interop.java module to convert a Ceylon String to a Java String in cases like this:

panel.add(JLabel("Hello world"), javaString(north));
panel.add(JButton("Click me"), javaString(center));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top