I've implemented my own widget (extending Canvas), that has as distinct characteristic the fact that it's all black.
When adding my canvas to a Shell in design-mode in WindowBuilder, it looks just like a regular canvas, although when in execution-mode everything shows up just alright.
Is there anything I'm missing, or is this an inherent limitation on the part of WindowBuilder?
Here's the code I'm using, just in case:
public class MyCanvas extends Canvas {
public MyCanvas(Composite parent, int style) {
super(parent, style);
setBackground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
addPaintListener(new PaintListenerEx());
}
private class PaintListenerEx implements PaintListener {
#Override
public void paintControl(PaintEvent e) {
e.gc.fillRectangle(MyCanvas.this.getBounds());
e.gc.dispose();
}
}
}
I believe that WindowBuilder is not calling the constructor you are trying to use to initilize your Canvas. WindowBuilder uses some known entry points per framework (like SWT) but sometimes it fails to find one even if you are using the right signature.
If you want to specify an entry point for WindowBuilder you can use this:
/**
* #wbp.parser.entryPoint
*/
to make WindowBuilder start from there but it is not guaranteed to work.
There's also the case where you need to specify the default constructor of a specific graphic element.
/**
* #wbp.parser.constructor
*/
In my particular case entryPoint didn't work and this was the solution.
Related
everyone. I've been searching for this question but I haven't found it here, so I'll guess it's really simple.
I'm creating a very simple application in JavaFX with a single button. Now I want to handle its events (like when it's pressed or when it's released), but when I see examples over the Internet, they all use anonymous classes (and a different class for each event), which makes the code dirty in my opinion. That's why I want to put the event handlers in a separate class and add them to the button.
The problem is that I don't know if I have to create a different class for every event, which I think isn't cool. So I came up with an idea. In the handle() method of the class I check which type of event is going on and process it.
This is the code
Main class
public class Main extends Application{
Button button;
PruebaEventHandler evhandler;
public Main() {
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("h0i");
button = new Button("Púlsame!");
evhandler = new PruebaEventHandler();
button.addEventHandler(MouseEvent.ANY, evhandler);
StackPane layout = new StackPane();
layout.getChildren().add(button);
Scene scene = new Scene(layout, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
}
EventHandler class
public class PruebaEventHandler implements EventHandler<MouseEvent>{
#Override
public void handle(MouseEvent event){
if(event.getEventType().equals(MouseEvent.MOUSE_PRESSED)){
System.out.printf("Te cogí\n");
}
if(event.getEventType().equals(MouseEvent.MOUSE_RELEASED)){
System.out.printf("\nMe ha soltado!!!");
}
}
}
I don't know if this is very inefficient or bad programming style, but is the only solution I've come up with so far. So I want to ask you if this is a good solution or, if there's a better one, shed your light on me! Thanks beforehand.
There already exists a solution for this, which is creating a JavaFX-project with a FXML-file, Controller and also a Main class. IDEs like IntelliJ and NetBeans have support for letting you create a JavaFX-project, which automatically creates those files for you, but I'm not sure if you have to add a plugin to make it work, that I don't remember.
The FXML-file takes care of the GUI, for example placing a button in a scene, and the easiest way to use it is with a SceneBuilder, which Oracle has, and can also be integrated in your IDE.
If you use FXML you can direct buttons to methods inside your FXML-document, so you don't need to have anonymous classes for event handlers. Instead you make the button call a spesific method in your Controller-class.
Here are a couple of youtube-tutorials that showcase the basics of using JavaFX with FXML:
https://www.youtube.com/watch?v=K7BOH-Ll8_g
https://www.youtube.com/watch?v=LMdjhuYSrqg
I already have a Java application with Swing GUI which reads a bunch of XML files and makes some graphs based on the information found in those XML files.
Now I was asked to turn that application into an Eclipse Plugin so the application can be launched from inside the Eclipse IDE. On top of that I have to make my application to sometimes open an XML file which contains the data that the user clicks.
Now, after a fast walk through a tutorial about how to make an Eclipse Plugin, it does not seem that I will be able to use Swing components inside a Plugin Project. I have seen that there are other tools and frameworks for making GUI for a plugin.
I need a suggestion for how can I turn my Swing application into an Eclipse plugin, the easiest possible. Even with some frameworks for Swing I had a hard time to make a treelayout graph. I imagine that should be even harder to implement into Eclipse plugin if Swing components do not work over there.
Here it is how my application looks like right now, based on Swing components:
If you don't want to rewrite the whole application, you may want to check possibilities of using SWT_AWT bridge, which allows to integrate Swing applications into SWT world. It is pretty easy, but you may want to check some articles as well.
I used it to integrate some Swing-based print preview functionality into existing Eclipse-RCP application. Worked well, though it still has its own underwater rocks.
You can use Swing components inside a Eclipse plugin.
For demonstaration I took the Swing components from https://code.google.com/p/treelayout/ and put them into an Eclipse view:
The important file looks like this:
package createaview.views;
import org.abego.treelayout.demo.swing.SwingDemo;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
public class TreeView extends ViewPart {
public static final String ID = "createaview.views.SampleView";
private TableViewer viewer;
class ViewContentProvider implements IStructuredContentProvider {
public void inputChanged(Viewer v, Object oldInput, Object newInput) {}
public void dispose() {}
public Object[] getElements(Object parent) {
return new String[] {"One", "Two", "Three"};
}
}
class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {
public String getColumnText(Object obj, int index) {
return getText(obj);
}
public Image getColumnImage(Object obj, int index) {
return getImage(obj);
}
public Image getImage(Object obj) {
return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT);
}
}
class NameSorter extends ViewerSorter {
}
public TreeView() {}
public void createPartControl(Composite parent) {
Composite composite = new Composite(parent, SWT.EMBEDDED | SWT.NO_BACKGROUND);
java.awt.Frame frame = SWT_AWT.new_Frame(composite);
frame.add(SwingDemo.getPanel());
}
public void setFocus() {
viewer.getControl().setFocus();
}
}
and if you pass me an email address I'll bundle up the demo project I did and send them to you (actually, it should probably be that if this is looking like the right answer, I'll put the projects in a zip file somewhere around here for the community to have a look at)
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.
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.
I have a JPanel extension that I've written and would like to be able to use it in the NetBeans designer. The component is simply adds some custom painting and continues to function as a container to be customised on each use.
I have properties to expose in addition to the standard JPanel ones and have a custom paintComponent() method that I'd like to be able to see in use when building up GUIs. Ideally I'd like to associate an icon with the component as well so that its easily recognisable for my colleagues to work with.
What's the best way of achieving this?
I made JPanel component in NetBeans with overridden paint method:
#Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
...
//draw elements
...
}
It has some custom properties accessible through NetBeans properties window.
public int getResolutionX() {
return resolutionX;
}
public void setResolutionX(int resolutionX) {
this.resolutionX = resolutionX;
}
public int getResolutionY() {
return resolutionY;
}
public void setResolutionY(int resolutionY) {
this.resolutionY = resolutionY;
}
I put it in my palette using:
Tools->Palette->Swing/AWT Components.
It even has the same look I painted in my overridden paint method while I am doing drag/drop in another container. I didn't associate icon to it though.
Hope this helps.
http://www.netbeans.org search for Matisse.
You can add your custom component to the matisse GUI palatte.
Build your project so the class file you want to use is part of the jar file
Open a java class that has a form, and switch to design mode.
3, Right click in the palatte and choose "palatte manager".
Choose the "add from jar" button to select your jar.
Choose the class you made, and add it to your palatte.
Now your panel is known to netbeans, and you can drag it into new panels.