Simple java interop from clojure

半腔热情 提交于 2019-12-13 20:08:50

问题


I am trying to get a simple JDatePicker to work from clojure. Following these code snipets but with the intent of getting it to work from clojure through java interop. I can't even get started.

This is the java code:

UtilDateModel model = new UtilDateModel();
JDatePanelImpl datePanel = new JDatePanelImpl(model);
JDatePickerImpl datePicker = new JDatePickerImpl(datePanel);
frame.add(datePicker);

I can't even get passed the first part in clojure

user=> (import org.jdatepicker.JDatePicker)
org.jdatepicker.JDatePicker
user=> (UtilDateModel.)

CompilerException java.lang.IllegalArgumentException: Unable to resolve classname: UtilDateModel, compiling:(/tmp/form-init7437510410318747099.clj:1:1) 
user=> 

I tried importing the class

user=> (import org.jdatepicker.JDatePicker.UtilDateModel)

ClassNotFoundException org.jdatepicker.JDatePicker.UtilDateModel  java.net.URLClassLoader$1.run (URLClassLoader.java:366)
user=> (import org.jdatepicker.UtilDateModel)

ClassNotFoundException org.jdatepicker.UtilDateModel  java.net.URLClassLoader$1.run (URLClassLoader.java:366)
user=> 

user=> (import '(org.jdatepicker.JDatePicker.UtilDateModel as um))

ClassNotFoundException org.jdatepicker.JDatePicker.UtilDateModel.as  java.net.URLClassLoader$1.run (URLClassLoader.java:366)
user=> (import '(org.jdatepicker.UtilDateModel as um))

ClassNotFoundException org.jdatepicker.UtilDateModel.as  java.net.URLClassLoader$1.run (URLClassLoader.java:366)
user=> (import '(org.jdatepicker.JDatePicker UtilDateModel as um))

ClassNotFoundException org.jdatepicker.JDatePicker.UtilDateModel  java.net.URLClassLoader$1.run (URLClassLoader.java:366)
user=> (import '(org.jdatepicker.JDatePicker UtilDateModel))

ClassNotFoundException org.jdatepicker.JDatePicker.UtilDateModel  java.net.URLClassLoader$1.run (URLClassLoader.java:366)
user=> 

======================= At least I am able to do this but I am not able to instantiate a JDatePicker yet.

user=> (import '(org.jdatepicker.JDatePicker))
nil
user=> (JDatePicker.)
CompilerException java.lang.IllegalArgumentException: No matching ctor found for interface org.jdatepicker.JDatePicker, compiling:(/tmp/form-init7437510410318747099.clj:1:1) 
user=> (doc JDatePicker)

CompilerException java.lang.RuntimeException: Expecting var, but JDatePicker is mapped to interface org.jdatepicker.JDatePicker, compiling:(/tmp/form-init7437510410318747099.clj:1:1) 

========================= Here, I see that JDatePicker wants to be instantiated using DefaultComponentFactory

public class TestJDatePicker {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) { }
JFrame testFrame = new JFrame();
testFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});
testFrame.setSize(500, 500);
JPanel jPanel = new JPanel();
JDatePicker picker = new DefaultComponentFactory().createJDatePicker();
....

Getting closer

user=> (import '(javax.swing JComponent JFrame JPanel UIManager))
javax.swing.UIManager
user=> (UIManager/setLookAndFeel "com.sun.java.swing.plaf.windows.WindowsLookAndFeel")
UnsupportedLookAndFeelException [The Microsoft Windows Look and Feel - com.sun.java.swing.plaf.windows.WindowsLookAndFeel] not supported on this platform  javax.swing.UIManager.setLookAndFeel (UIManager.java:523)

This appears the preferred way to import things:

(import '(org.jdatepicker JDatePicker JDatePanel))
org.jdatepicker.JDatePanel
user=>

来源:https://stackoverflow.com/questions/27304476/simple-java-interop-from-clojure

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!