Putting custom widget to the Eclipse palette - java

I am fully aware, this is a very trivial question for many. I have made my research on the web, skimmed hundreds of pages to find a very trivial example, but no success.
All I want is make this simple widget an put it on the eclipse palette.
import javax.swing.JSpinner;
import org.jdom.Element;
public class JxmlJSpinner extends JSpinner{
Element element;
public JxmlJSpinner(Element Aelement) {
super ();
element = Aelement;
}
}
when I export it in the .jar file and try to load it in the "Custom" palette, it throws an exception (some error in class loader).
May anybody take a swift look and correct code above, to make it loadable?
Thanx a lot...

Related

How to use JLex.CAlloc.class

I downloaded and imported xalan-2.5.0.jar to my netbeans project in order to use the CAlloc.class function. But i cant figure out how to use the CAlloc function.
i imported the package by doing this,
import JLex.*;
i wanted to import specifically the calloc class but i get error when i tried this
import JLex.CAlloc;
the error says "CAlloc is not public in JLex; cannot be accessed from outside package" , i opened and checked the CAlloc.class and it not public but i cant edit it, may be if there is a way to edit the class ???

cannot resolved to a type error in java

I'm new in java, please help me to understand this.
I can see there is ReadHtml class and defined with one public method. But when i put this code in ecplise, it shows red mark under WebClient with tag that "this cannot resolved to a type". May I know what does it mean? Gone through all about method definition but couldn't find any remedy to understand this.
Can I get any help ?
public class ReadHtml {
public static LinkedList<String> readJacksonCounty(String urlName, String pStartDate,String pFinishDate)
{
LinkedList<String> xmlListReturn=new LinkedList<String>();
System.getProperties().put("org.apache.commons.logging.simplelog.defaultlog", "error");
final WebClient webClient1 = new WebClient(BrowserVersion.CHROME);
webClient1.setJavaScriptTimeout(60000);
webClient1.getCookieManager().setCookiesEnabled(true);//enable cookies
webClient1.getCache().clear();
You are missing an import of this library:
import com.gargoylesoftware.htmlunit.WebClient;
Add this to the top of your file (and read dsp_user's comment for future reference).
Basically "...cannot be resolved to a type" means that type is not available on the class path. If you're just using eclipse refere to How to import a jar in Eclipse.
If you already added the needed jar onto your class path, you are missing the import statement. Imports just make it so that you dont have to use a class's fully qualified name. (you can type
MyClass myClass;
as opposed to
com.some.package.MyClass myClass;
if you add
import com.some.package.MyClass;
at the top of your file.
Note that if you want to build a jar from your project you'll need some kind of build tool. If you choose to use Maven, which is very common, just read any tutorial on how to get started and manage dependencies.

JDiagram older version throwing StackOverflowError with JRE 8 at ExtendedArrayList.sort

I'm using JDiagram JAR like below
Diagram myDigram = new Diagram();
myDigram.routeAllLinks();
This code works fine when run with JRE 7 however when it is run with JRE 8, following error is being thrown:
java.lang.StackOverflowError
at java.util.Collections.sort(Unknown Source)
at com.mindfusion.common.ExtendedArrayList.sort(Unknown Source)
at java.util.Collections.sort(Unknown Source)
at com.mindfusion.common.ExtendedArrayList.sort(Unknown Source)
at java.util.Collections.sort(Unknown Source)
at com.mindfusion.common.ExtendedArrayList.sort(Unknown Source)
I followed the stack trace to JDiagram decompiled code. Observed that routeAllLinks() calls RouteLinks() on another object (say router) and at one more level deep ExtendedArrayList.sort() which is appeared in error stack trace is called. The "ExtendedArrayList" in JDiagram extends ArrayList and contains a method named "sort()" which has following definition.
public void sort(Comparator<? super T> paramComparator)
{
Collections.sort(this, paramComparator);
}
On Google I found out that JRE 8 has introduced List.sort() and delegates the Collections.sort() calls to collection's (ExtendedArrayList in my case) sort method. And so library ExtendedArrayList.sort() became an override. And it creates an infinite recursion which results in stackoverflow. I could reproduce this issue even with small piece of code as well now.
Also
Our original class which creates JDiagram object, is being loaded at runtime by some other component in our product. We have very little control over the loading of our program.
We have found out that latest version of JDiagram has fixed this issue by replacing sort() with sortJ7() method. However, we cannot upgrade the library at this moment. JDiagram is a licensed API.
ExtendedArrayList is being instantiated by JDiagram internally and so we cannot alter it from our code.
We have tried following solutions which didn't work so far
Java Proxy: Because our code does not call ExtendedArrayList directly
and also 'Diagram' does not have any interface.
Spring AOP: We are
not using spring and also our program is loaded runtime by other
component.
AspectJ: By now, this was apparently a solution. However,
it also didn't work as we are not able to weave our program at
runtime. Not sure if someone could make it work.
Kindly let me know if any point needs elaboration.
Any help is welcome. Thanks.
UPDATE
So far, javassist is the best approach however there JDiagram obfuscation is preventing the solution to work correctly. We have kind of assumed that it is impossible (have to say) to fix considering our release date on our head. We have started process to upgrade library. And meanwhile removed a small feature from our application which was being provided by routeAllLinks() method.. :-(
thanks everyone for your help. I'll be continuing my research on this issue as I found it really intriguing and challenging.. I'll update the post if I could resolve it.. And I'll be giving bounty to #gontard for his javassist approach as I'm continuing my research with it. Thanks.
I have reproduced your problem with a basic example:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class ExtendedArrayList<E> extends ArrayList<E> {
#Override
public void sort(Comparator<? super E> c) {
Collections.sort(this, c);
}
}
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws Exception {
ExtendedArrayList<String> arrayList = new ExtendedArrayList<String>();
arrayList.addAll(Arrays.asList("z", "y", "x"));
arrayList.sort(String::compareTo); // -> java.lang.StackOverflowError
}
}
I was able to bypass the java.lang.StackOverflowError by renaming the method using javassist:
import java.util.Arrays;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
public class Main {
public static void main(String[] args) throws Exception {
ClassPool pool = ClassPool.getDefault();
CtClass ctClass = pool.get("ExtendedArrayList");
CtClass[] sortParams = new CtClass[]{ pool.get("java.util.Comparator")};
CtMethod sortMethod = ctClass.getDeclaredMethod("sort", sortParams);
sortMethod.setName("sortV7"); // rename
ctClass.toClass();
ExtendedArrayList<String> arrayList = new ExtendedArrayList<String>();
arrayList.addAll(Arrays.asList("z", "y", "x"));
System.err.println(arrayList); // print [z, y, x]
arrayList.sort(String::compareTo);
System.err.println(arrayList); // print [x, y, z]
}
}
I have not tried with your version of JDiagram because I get only the last (Java 8 compatible) version on their website.
Think about decompiling the library and fixing the issue by yourself. You could use this fixed package as a workaround.
An alternative would be, to place a fixed version of the class in your code. Same package like in the library and same class name of course:
com.mindfusion.common.ExtendedArrayList
Maybe you have to configure the classloader to load your class instead of looking up the faulty class in the library first. Options like "parent first" or simply accessing the class from your code once before calling the library could make the deal.
I see that the loading of your program is not controlled by you.
Perform the following steps:
Include the jar(javassist) of the program that could change the "sort" method name to any other name that could avoid overriding the sorting method
Load the jar's (javassist) main class by reflection class.forName(""); in the beginning of your program's main method
Call the jar's (javassist) method to perform the required changes you need on the methods
This way you could be sure that any jar (javassist)is loaded and is ready to be used.

Java import enumeration

Hi I had a program which worked fine in a .jar file.
Basically all the classes were part of the same package called : "eu.floraresearch.lablib.gui.checkboxtree"
They ALL are located in the SAME "checkboxTree" folder. Each file has a line stating
package eu.floraresearch.lablib.gui.checkboxtree;
I need to modify the code and to integrate it in another project.
So I took all the .java files, copied them in my Eclipse project folder (no package anymore), and got rid of the "package eu.floraresearch.lablib.gui.checkboxtree;" line in each files. Everything is fine except for one file comprising an enumeration.
Originally the file looked like this:
package eu.floraresearch.lablib.gui.checkboxtree;
public class QuadristateButtonModel extends DefaultButtonModel {
public enum State {
CHECKED, GREY_CHECKED, GREY_UNCHECKED, UNCHECKED
}
...
}
The problem is, there is another class which originally imported the above class enumeration:
import eu.floraresearch.lablib.gui.checkboxtree.QuadristateButtonModel.State;
public class QuadristateCheckbox extends JCheckBox {
public QuadristateCheckbox() {
this(null);
}
public QuadristateCheckbox(String text) {
this(text, State.UNCHECKED);
}
...
}
First of all I find it weird how enumerations can be imported.. But it worked fine when everything was inside the package.
Since all my .java files are in the same folder now, I just removed the package line.
However I have this issue with the QuadristateCheckbox class which imports "QuadristateButtonModel.State".
If I change the import line with
import QuadristateButtonModel.State;
it states
The import QuadristateButtonModel cannot be resolved
I tried various things I found on the internet like
import static QuadristateButtonModel.State.*;
or
import QuadristateButtonModel.State.*;
but the same error messages occur:
The import QuadristateButtonModel cannot be resolved
On top of that, in the above code from QuadristateCheckbox class:
public QuadristateCheckbox(String text) {
this(text, State.UNCHECKED);
}
an error message
State cannot be resolved to a variable
which is understandable given the fact that I fail to import the State enumeration.
What can I do? Please explain to me what is wrong
PS: The code was taken from this site: http://www.javaworld.com/article/2077762/core-java/swing-based-tree-layouts-with-checkboxtree.html
The authors provide classes to build checkable trees.

Saving objects in files produces error: "FileOutputStream cannot be resolved to a type"

I'm trying to save an object, I googled the HowTo and got a tutorial on this issue. However since I never worked before with this I'm experiencing problems I can't resolve.
So I have a huge class Course (contains all sort of things, variables, collectors...) which I'm trying to save in a file.
import java.io.Serializable;
class Person implements Serializable {
... }
Now, I send an object to a class Save.java and try to save it :
class Save {
protected void saveCourse (Course course) {
FileOutputStream courseFile = new FileOutputStream("course.data");
ObjectOutputStream courseObj = new ObjectOutputStream(courseFile);
courseObj.writeObject(course);
}
}
When I try to compile it FileOutputStream and ObjectOutputStream "cannot be resolved to a type". Aren't they suppose to be predefined. How can I fix this
Tutorial where I got this can be found here.
You need to import FileOutputStream and ObjectOutputStream.
Since they are both in the java.io package, that means that you'll need to add this to the top of your file (under the package declaration, if you have one):
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
Also be careful about choosing your Java tutorials: there are lots of bad tutorials out there. The official Java tutorials from Oracle are pretty good (at least they are much better than most other stuff out there) and should cover everything you need for quite some time.
For example there's a nice tutorial about using ObjectOutputStream and related classes.
More details about packages and importing can be found in this tutorial.
To handle exceptions, normally put your code in a try-catch block

Categories

Resources