EDIT: Sorry, I just started programming in Java. It turned out to be a problem with an out of range array access... I am used to error messages about this kind of thing being automatic...
(using Netbeans 7.0.1)
I have been customizing JTextArea and JTable. I do so by adding a new Java class to my project and then declaring it extends the particular class I want (in my case, either JTextArea or JTable).
I had been using it normally, adding these new classes to JDialogs and JInternalFrames without any problem. I do so by just dragging it to my JDialog or JInternalFrame...
But recently, for some reason, I started getting this error messages "Component cannot be instantiated. Please make sure it is a JavaBeans component."
The JInternalFrames that were accepting the old customized classes still accepts them. But if I try to add the new customized class, it gives me that error message and, afterwards, it starts showing the same message to the old customized classes too...
Something really weird is going on. I copied the same code of a (previously) customized class to a new class (changing the name of the class, of course). Then I try to add this to my JInternalFrame. It gives me the error message! If, before this, I try to add the same customized class (with the original name), it adds the class normally....
This is annoying and I can't solve it. Can anyone help me please?
Thanks a lot for this answer but, if you want to know the reason here you are.
Typically this appears on two position:
an overridden method on your component.
a normal method on your component.
For example:
package UI.Components;
public class LabelComponent extends javax.swing.JLabel {
private javax.swing.JLabel label;
public TextFieldComponent() {
label = new javax.swing.JLabel(_label);
add(label);
}
#Override
public void setText(String text) {
label.setText(text);
}
}
The method setText(String text) is called say in the supper class constructor then it the overridden new method would be called in the case of the (label) variable which is used on this method still no being initialized so a java.lang.NullPointerException will be thowed.
solution:
1) try ... catch:
#Override
public void setText(String text) {
try {
label.setText(text);
} catch (Exception e) {
}
}
2) check:
use null initialization on declaration
private javax.swing.JLabel label = null;
then check on the method
#Override
public void setText(String text) {
if(label != null)
label.setText(text);
}
3)use initialization on declaration:
private javax.swing.JLabel label = label = new javax.swing.JLabel();
and then use setText method in your constructor
label.setText(_label);
note:
in the case of reason (2) a normal method on your component, it is the same as (1) but you may call the method before initialize the variable or assign null to the variable before calling the method and so on and it can being solved by the same ways.
I too faced the same problem, after some search in the web I found the solution for this problem. I don't have a deep understanding of why and how this problem occurs, but I can share with you the solution I found.
When you get such error msg, goto the menu View-->IDE Log or you can open the log from windows_user_Home\.netbeans\7.0\var\log
In that log you have to locate the error msg you got, for example,
INFO [org.netbeans.modules.form.BeanSupport]: Cannot create default instance of: test.Application1
java.lang.NullPointerException
at test.Application1.initLabel(Application1.java:906)
So the problem is in line 906 of your .java file. Open that file and comment those lines and then you will able to overcome the problem.
You can add the component to the Form or jInternalFrame or ...
After adding the component, you can again uncomment those lines. Just Clean and Build your project.
Hope this helps..
Goodluck
reachSDK
I have encountered the similar problem, however in different context.
I have two separate projects, a swing built user interface, and another one that poses as class library.
I added a class to the class library, headed over to the user interface, and implemented this newly added class from the library into the swing interface project in shape of an existing custom JFrame. So what happened to me now that the class loader of course could not find the class because the library project required compiling. The issue was fixed by compiling it.
Related
I am developing an Eclipse plug-in that shows custom multi-line markers in Eclipse's own AbstractTextEditor.
Here is what I have so far:
a custom marker with the super type "org.eclipse.core.resources.textmarker"
an annotationType (org.eclipse.ui.editors.annotationTypes)
a markerAnnotationSpecification (org.eclipse.ui.editors.markerAnnotationSpecification)
This is all working well, my markers show up in the editor. But I need to customize the way they are drawn on the VerticalRuler, so they do not only show up as an icon, but as a vertical line spanning the affected source lines.
I know, that this can be done with Annotations by implementing IAnnotationPresentation and overwriting paint().
But how can I do this for markers?
Edit: Here is a screenshot of what I am trying to achieve:
I solved this by contributing a RulerColumn (extension point org.eclipse.ui.workbench.texteditor.rulerColumns) and configuring markerAnnotationSpecification to not include verticalRulerPreferenceKey and verticalRulerPreferenceValue (so it will not be shown on the default AnnotationRulerColumn).
In case someone also finds the documentation on how to best implement IContributedRulerColumn a bit sparse: it seems the way to go is to subclass AbstractContributedRulerColumn and have the methods delegate to a subclass of AbstractRulerColumn.
For example:
public class MyRulerColumn extends AbstractContributedRulerColumn {
private IVerticalRulerColumn delegate = new AbstractRulerColumn() { … }
public void setModel(IAnnotationModel model) {
delegate.setModel(model);
}
…
}
Customizing the appearance is then as easy as overwriting one of the paint… methods in the delegate.
I have implemented a simple GWT app that uses 1 Place and 1 Activity (which I have implemented as a Presenter which extends an AbstractActivity and which contains a Composite "view" subclass). The 1 and only UI object in the view is a GWT-Bootstrap NavBar that I want presented at the very top of my "home page".
I'm running the app locally from inside Eclipse and am not getting any compiler or runtime errors. When I go to the URL that the Development Mode console points me to, I get a slight pause in the browser (I assume this is the browser "downloading" the JavaScript) and then I see a blank white screen (instead of my NavBar). The window title is correct (I set this in the module's HTML page) and when I view source I see the same HTML source, so I know that the app's JavaScript is getting to the browser properly. It's just not rendering the NavBar.
I have sprinkled System.out.println() statements throughout onModuleLoad(), my default ActivityManager, ActivityMapper, PlaceHistoryMapper, presenter and view Composite, and all these sysout statements print in the dev console; telling me that I have wired everything together correctly, and that at runtime when the PlaceHistoryHandler#handleCurrentHistory method is called (from inside onModuleLoad), I should be seeing my NavBar.
The only possibilities I can think of are:
I have configured gwt-bootstrap incorrectly; or
I'm not using UiBinder correctly
Something else is wrong with how I am using Activities and Places, or perhaps how I am attaching the UI to RootLayoutPanel inside onModuleLoad().
As for gwt-bootstrap:
I placed the JAR on my project's classpath (I know this because when I include a new UiField of type NavBar inside my widget/view, I don't get any compiler errors)
I added <inherits name="com.github.gwtbootstrap.Bootstrap"/> to my GWT module XML
So if there's anything else I have to configure, please let me know!
As for the UiBinder stuff, here's my widget/view:
public class WebRootDisplay extends BaseDisplay {
private static WebRootDisplayUiBinder uiBinder = GWT
.create(WebRootDisplayUiBinder.class);
interface WebRootDisplayUiBinder extends UiBinder<Widget, WebRootDisplay> {
}
#UiField
Navbar navBar;
public WebRootDisplay(EventBus eventBus) {
super(eventBus);
System.out.println("I get this printing to the console at runtime.");
initWidget(uiBinder.createAndBindUi(this));
System.out.println("...and this too!");
}
}
<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
xmlns:g="urn:import:com.google.gwt.user.client.ui"
xmlns:b="urn:import:com.github.gwtbootstrap.client.ui">
<g:HTMLPanel>
<b:Navbar ui:field="navBar">
<b:Nav>
<b:NavLink href="http://www.google.com">
Home
</b:NavLink>
</b:Nav>
</b:Navbar>
</g:HTMLPanel>
</ui:UiBinder>
One thing I noticed is that I've got my NavBar inside an HTMLPanel in the UiBinder XML. I did this because I used the Google-Eclipse plugin to generate a new UiBinder for me (which autogenerated both the Composite (which I then modified to extend BaseDisplay, which itself extends Composite) as well as the UiBinder snippet. I figured GWT wants me to put all the UI fields inside this HTMLPanel...(?)
If I'm missing anything here please let me know. I'm not instantiating the NavBar field because I believe that's what createAndBindUi does for me.
If both my gwt-bootstrap config and my use of UiBinder looks correct, then something else is obviously wrong and I will have to post more code. I just wanted to hold off on that initially before these first two items were ruled out. Thanks in advance!
Update
Here is onModuleLoad:
public void onModuleLoad() {
// Some homegrown DI stuff. I have verified that the injector works properly.
ApplicationScope appScope = new ApplicationScope();
setInjector(new ApplicationInjector(appScope,
InjectorProvider.newMasterProvider()));
// Add the sole composite child to the RootLayoutPanel.
// I have verified that injectWebRootDisplay returns a fully configured
// WebRootDisplay instance.
RootLayoutPanel.get().add(injector.injectWebRootDisplay());
historyHandler.register(placeController, eventBus, defaultPlace);
historyHandler.handleCurrentHistory();
}
Could you paste the onModuleLoad() part of your code please?
If you don't got any Exception and error message, I think you should check that you add the view properly to the RootPanel, or when you run the app you should check that the view is there in a div in the HTML and just unvisible or something similar.
The UiBinder part looks fine in a first look.
EDIT:
This onModuleLoad() doesn't said too much to me, but you could try something.
I always use the RootLayoutPanel.get() method in the following way:
RootLayoutPanel.get("someDivId").add(injector.injectWebRootDisplay());
So I always add a div or table to the placeholder HTML with a id, so you can refer to that div when you get the RootPanel. I'm not confident about this is necessary, but I saw this at the first time, and it's working properly.
If you have question or problem, please let me know. :)
Well, I've tried a local example looking exactly like yours code, and I think that problem is not in UI binder. The code you provided so far, is correct, so it most likely that the error is somewhere else.
The biggest suspect is the BaseDisplay class. As far as I can see, this class is not from GWT or gwt-bootstrap. You can really quickly check it, by changing WebRootDisplay class, so it extends classic GWT Composite class instead of BaseDisplay (and disabling all mvp stuff for while). If it works, you have a proof that the problem is caused by 'BaseDisplay'
Since I don't have the full code, I can only assume that WebRootDisplay is used also for displaying the views, and most likely the error is that when view is added to that class, previously added widget (in your case it is a NavBar which you add in constructor of WebRootDisplay) is removed. Most likely the problem should be in methods setWidget and initWidget
In my experience with GWT Activities and Places, a common culprit of a blank white page is failing to register the Place's Tokenizer with the PlaceHistoryMapper as so:
/**
* PlaceHistoryMapper interface is used to attach all places which the
* PlaceHistoryHandler should be aware of. This is done via the #WithTokenizers
* annotation or by extending PlaceHistoryMapperWithFactory and creating a
* separate TokenizerFactory.
*/
#WithTokenizers({
MyPlace.Tokenizer.class,
SomeOtherPlace.Tokenizer.class})
public interface AppPlaceHistoryMapper extends PlaceHistoryMapper {}
See https://developers.google.com/web-toolkit/doc/latest/DevGuideMvpActivitiesAndPlaces#PlaceHistoryMapper
Another cause for a white page (particularly when using RootLayoutPanel.get() with a single place is failing to map the place correctly in the ActivityMapper:
/**
* AppActivityMapper associates each Place with its corresponding
* {#link Activity}
*
* #param clientFactory
* Factory to be passed to activities
*/
public class AppActivityMapper implements ActivityMapper {
private ClientFactory clientFactory;
public AppActivityMapper(ClientFactory clientFactory) {
super();
this.clientFactory = clientFactory;
}
#Override
public Activity getActivity(Place place) {
if (place instanceof MyPlace)
return new MyActivity((MyPlace) place, clientFactory);
else if (place instanceof SomeOtherPlace)
return new SomeOtherActivity((SomeOtherPlace) place, clientFactory);
return null; // If your return null with single place bound to RootLayoutPanel
// you may get a blank white page
}
}
See https://developers.google.com/web-toolkit/doc/latest/DevGuideMvpActivitiesAndPlaces#ActivityMapper
Without a more complete code sample it is impossible to determine exactly what is happening, but the two causes outlined above are common oversights which may help anyone who comes across this thread.
Instead of System.println use GWT.log messages. Then open the Javascript console (of Firebug or Chrome) and see where your code ends up. GWT.log will print out in the browser console, so you can see what the compiled JS code does.
Also, if you compile with the Pretty mode, you'll see the generated Javascript code in the browser and be able to step through and see what is being called (or not).
It's a little embarassing to not know how to fix this on my own, seeing how I have a bit of experience with Java, however until now I've never really done anything other than web programming with Java.
I'm trying to create a wizard, and trying to generalize creation of the fields presented in the window. As such, I don't have direct control over the actual component JTextField but a wrapper class which handles the finer details. However I would like to know when the value has changed, so I've added a "addVetoableChangeListener" method which allows me to register a VetoableChangeListener to the JTextField itself.
I've verified that the method gets called and that it passes the listener onto the JTextField in debug. However, nothing gets called. No exception is launched, and my breakpoint inside the method which implements the interface VetoableChangeListener is never called.
Is there something I'm not getting? Does the listener have to be some sort of component before it works correctly or does it simply have to implement the interface? Perhaps I'm overlooking an obvious error because I've been concentrating on it for too long, and I'm hoping it'll be evident to one of you. A simpler version of what I'm attempting is:
public class TomcatConfigPanel extends WizardKeyValuePanel implements VetoableChangeListener {
protected void initPanel(JPanel mainPanel) {
addField("port", "8080");
IWizardField portField = getField("port");
portField.addVetoableChangeListener(this);
}
public void vetoableChange(PropertyChangeEvent evt)
throws PropertyVetoException {
// Stuff that would drive you Lovecraft insane if you saw what was written here
}
}
public class WizardTextField implements IWizardField {
private JLabel label;
private JTextField field;
public WizardTextField() {
// some initialization stuff ...
}
public void addVetoableChangeListener(VetoableChangeListener listener) {
field.addVetoableChangeListener(listener);
}
}
The VetoableChangeListener will only be called if a constrained property is being changed on the JTextField. A constrained property is a property whose setter method throws a PropertyVetoException. So, if your code never calls any such setter method on the JTextField, your listener won't ever be called. Read http://download.oracle.com/javase/tutorial/javabeans/properties/constrained.html for more details.
I haven't found any constrained property in JTextField (and in all its class hirarchy) in the API doc, so I doubt your listener could ever be called.
I have this piece of code :
public class Profile extends Container{
public Profile(String value) {
Container profilo_1 =new Container();
Container profilo_2 =new Container();
// (1) THIS ADD A BUTTON TO THE MAIN CLASS
this.createButton().setLabel("Modifica Profilo");
// (2) NOW I NEED TO ADD A BUTTON INTO THE INSTANCE OF profilo_2 BUT IT FAILS
profilo_2.add(this.createButton());
this.add(profilo_1);
this.add(profilo_2);
}
}
the point (2) fails, because it said that im about to adding a child to this container, but it is owner already by a container...
In fact, if i do this :
ILabel label=new ILabel();
profilo_2.add(label);
it said to me that ILabel() is abract and cannot be instantiated!
How can I fix it? Cheers to everybody :)
Guessing wildly, since this depends on your code... Try this (moreless what Piotr said)
profilo_2.add(profilo_2.createButton());
Try changing to
Button button2 = this.createButton();
button2.setLabel("EDIT");
profilo_2.add(button2);
By the way this has nothing to do with abstract classes, from what I see
EDIT: Though you say that #1 "adds a button to the main class", so does that mean that createButton() does this.add(button) ? If so then you should probably change that function so that isn't done every time you create a button.
The problem is probably that when you create a button with "this.createButton", that button has its parent set to "this" (in this context), and when you try to add it to profilo_2, it throws an error. Instead you should createButton on profilo_2 directly, then the parent will be the correct one (and perhaps you won´t have to add() it either?)
Probably, setLabel() returns something which cannot be passed to Container::add(..). Please provide your code for Container
I have an applet I've built using NetBeans, called AKApplet. It runs fine in the IDE, but when I put it in a web page it throws the following error:
Exception in thread "Thread-15" java.lang.NoClassDefFoundError: AKApplet$2
at AKApplet.run(AKApplet.java:675)
The applet uses the run() method to load some data in the background while keeping the UI responsive. Pretty standard stuff. At line 675, after the data has been loaded, I'm trying to update the UI components using invokeLater():
public void run() {
// ... data loads ...
// line 675:
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
userMessages.setText("Data loaded.");
panelList.setVisible(true);
validate();
}
});
}
The components I'm trying to update are userMessages, a JLabel and panelList which is a Panel. I don't think it's getting that far however.
Does anyone know what might be happening? At this point the applet has loaded and the components can be seen and have been updated, etc.
Make sure you're deploying not only AKApplet.class, but also AKApplet$1.class, AKApplet$2.class, etc.
I guess I don't understand what the $ classes refer to. There is only a single AKApplet class, no inner classes. There are no static definitions either.
I do have two other classes defined, but they are separate classes:
class ThreadFlags { /*...*/ }
class DeleteButton extends JLabel { /*...*/ }
Also, I've verified that they are in AKApplet.jar file at the root level:
META-INF/MANIFEST.MF
META-INF/AKAPPLET.SF
META-INF/AKAPPLET.DSA
META-INF/
AKApplet.class
DeleteButton.class
ThreadFlags.class
Update: Ok, I found the AKApplet$.class files in the /build/classes/ directory of the NetBeans project. I added them, and it works. Thanks for your help. Can someone give me a brief explanantion of what those files are? As I said, there are no inner classes that I've defined...
Are there any static definitions in the second inner class of AKApplet that could throw any kind of exception?
Exceptions in the static initializer are the most common cause for NoClassDefFoundErrors after you have made sure that the class file exists and is on the classpath.