CheckBox clickhandler code doesn't work...there is no compile time error...look at the snap shot - gwt

CheckBox clickhandler code doesn't work...there is no compile time error...look at the snap shotlook at the snap shot in which there is no image in popup...i have highlighted it
cb1.addClickHandler(new ClickHandler(){
public void onClick(ClickEvent event) {
System.out.println("hello2");
boolean checked = ((CheckBox) event.getSource()).getValue();
if (checked) {
System.out.println("hello3");
int left = toothWidget.getToothImage().getAbsoluteLeft();
int top = toothWidget.getVPanel().getAbsoluteTop();//toothWidget.getToothImage().getAbsoluteTop();
Image im = new Image();
im.setUrl(GWT.getHostPageBaseURL()+"/images/"+menuItem.getImg());
System.out.println(GWT.getHostPageBaseURL()+"/images/"+menuItem.getImg());
int offx = left;
int offy = top;
final PopupPanel popup1 = new PopupPanel(true);
popup1.setStylePrimaryName("transparent");
popup1.setPopupPosition(Math.max(offx, 0),Math.max(offy, 0));
//popup.add(im);
ToothWidget wgt = new ToothWidget(toothWidget.getToothNumber(),menuItem.getImg(), toothWidget.getTeeth());
toothWidget.getTeeth().getMap().get(toothWidget.getToothNumber()).put(menuItem.getName(), wgt);
wgt.setMenu(toothWidget.getMenu());
wgt.setPanel(popup1);
popup1.add(wgt);
popup1.show();
}
else{
ToothWidget wgt = toothWidget.getTeeth().getMap().get(toothWidget.getToothNumber()).remove(menuItem.getName());
wgt.getPanel().hide();
}
}
});

WEB Dev 101 - Always use FireFox/FireBug's Network/Image console to check for URL mismatch . You will be seeing 404 errors for the Image path.
Where is YOUR Image located?
1) public folder parallel to your gwt.xml
If you have images folder in your gwt module's public folder you need to use im.setUrl(GWT.getModuleBaseURL()+"/images/"+menuItem.getImg());
or
2) webapps folder
If you have images in deployed into your war/images you need to use
im.setUrl(GWT.getHostPageBaseURL()+"/images/"+menuItem.getImg());
If the above does not solve the issue then you may not have the image at all under "images"

try this. It is better to use the ValueChangeHandler that works for sure and is used for changing values.
CheckBox cb1 = new CheckBox();
cb1.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
#Override
public void onValueChange(ValueChangeEvent<Boolean> event) {
Boolean isChecked = event.getValue();
if (isChecked) {
System.out.println("hello3");
int left = toothWidget.getToothImage().getAbsoluteLeft();
int top = toothWidget.getVPanel().getAbsoluteTop();//toothWidget.getToothImage().getAbsoluteTop();
Image im = new Image();
im.setUrl(GWT.getHostPageBaseURL()+"/images/"+menuItem.getImg());
System.out.println(GWT.getHostPageBaseURL()+"/images/"+menuItem.getImg());
int offx = left;
int offy = top;
final PopupPanel popup1 = new PopupPanel(true);
popup1.setStylePrimaryName("transparent");
popup1.setPopupPosition(Math.max(offx, 0),Math.max(offy, 0));
//popup.add(im);
ToothWidget wgt = new ToothWidget(toothWidget.getToothNumber(),menuItem.getImg(), toothWidget.getTeeth());
toothWidget.getTeeth().getMap().get(toothWidget.getToothNumber()).put(menuItem.getName(), wgt);
wgt.setMenu(toothWidget.getMenu());
wgt.setPanel(popup1);
popup1.add(wgt);
popup1.show();
}
else
{
ToothWidget wgt = toothWidget.getTeeth().getMap().get(toothWidget.getToothNumber()).remove(menuItem.getName());
wgt.getPanel().hide();
}
}
});
}
Plus the images/ folder must be inside the war forlder (though i think you already know that having developed this site)
Also check what menuItem.getImg() returns. It might return only the filename but not the extension or something else entirely.

Related

displaying jpg to a jPanel

So i am trying to add an image to my program after a button is pressed with a certain value entered into a text field. I want the image to be blank until the button is pressed with the correct value. The only issue i seem to be having is making the photo appear, i know the output should be working. here is the code i have for adding the label.
photo = new JLabel();
photo.setBounds(425, 170, 400, 360);
contentPane.add(photo);
ImageIcon icon = new ImageIcon("AwsomeSauce.jpg");
here is the code for when the value is entered correctly
if (error) {
photo.setIcon(image);
}
now im still pretty new at this so go easy on me.
Here is simple example for you:
public class Frame extends JFrame {
private JLabel label;
public Frame() {
getContentPane().add(label = new JLabel(),BorderLayout.SOUTH);
JButton b = new JButton("show Icon");
b.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
URL resource = getClass().getResource("/res/3_disc.png");
ImageIcon ico = new ImageIcon(resource);
label.setIcon(ico);
}
});
getContentPane().add(b,BorderLayout.NORTH);
}
public static void main(String[] args) {
Frame frame = new Frame();
frame.setTitle("This is a test");
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
"/res/3_disc.png" - is my icon which is placed in my project in res folder.

JavaFX Dragboard setContent interfering with setOnMouseDragged?

I would like to use drag and drop in JavaFX and have a feature where you can visually see the item being dragged. I've implemented what I thought would be a working solution, but there seems to be an issue with the API.
According to the API startDragAndDrop(TransferMode...) MUST be initialized inside setOnDragDetected. This is where I use a Dragboard to store content I'd like to transfer to another node.
EDIT: This appears to have something to do with the TrasferMode. If I use TransferMode.NONE there is no issue, but use of COPY, ANY, LINK always results in this problem.
But calling dragBoard.setContent(some clipboard content) only allows very small increments of movement with the mouse (a max of 4 pixels in any direction!). Removing this line of code, I can then drag the item and see it being dragged anywhere, but of course, I then can't store clipboard content.
The problem I see is that setOnMouseDragged(..) gets called before setOnDragDetected! It doesn't make much sense why setOnMouseDragged gets run before setOnDragDetected...
Is there something obvious to you in my code that's maybe causing a problem? I'd simply like to be able to see the imgView moving when dragging and be able to drop it on a target as usual, with the clipboard content.
EDIT 2: Updated code below to reflect using only Drag events, rather than Drag and Mouse events. Using both caused issues. The problem that still remains is that I'm unable to drop on a target, since using setOnDragOver makes the dragged node always right below the cursor.
protected ImageView initImageView(Image img){
final Pane ldzPane = GameBoard.getInstance().getLDZpane();
final ObjectProperty<Point2D> dragAnchor = new SimpleObjectProperty<>();
final ImageView imgView = new ImageView(img);
final DoubleProperty initX = new SimpleDoubleProperty();
final DoubleProperty initY = new SimpleDoubleProperty();
final DoubleProperty dragX = new SimpleDoubleProperty();
final DoubleProperty dragY = new SimpleDoubleProperty();
final DoubleProperty newXPosition = new SimpleDoubleProperty();
final DoubleProperty newYPosition = new SimpleDoubleProperty();
final int buffer = 3;
imgView.setOnDragDetected(new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
initX.set(imgView.getTranslateX());
initY.set(imgView.getTranslateY());
dragAnchor.set(new Point2D(event.getSceneX(), event.getSceneY()));
ClipboardContent content = new ClipboardContent();
content.putString(RHSIconizedToken.this.tokenLookupInfo());
Dragboard db = imgView.startDragAndDrop(TransferMode.ANY);
db.setContent(content);
event.consume();
}
});
imgView.setOnDragOver(new EventHandler<DragEvent>(){
#Override
public void handle(DragEvent event) {
imgView.toFront();
dragX.set(event.getSceneX() - dragAnchor.get().getX());
dragY.set(event.getSceneY() - dragAnchor.get().getY());
imgView.setOpacity(0.5);
newXPosition.set(initX.get() + dragX.get());
newYPosition.set(initY.get() + dragY.get());
//if( (Math.abs((newXPosition.get() - ldzPane.getWidth())) <= ldzPane.getWidth() + startX + buffer) &&
// ((newXPosition.get() + startX + imgView.getImage().getWidth()+ buffer)<= ldzPane.getWidth()))
imgView.setTranslateX(newXPosition.get());
//if( (Math.abs((newYPosition.get() - ldzPane.getHeight())) <= ldzPane.getHeight() + startY + buffer) &&
// ((newYPosition.get() + startY + imgView.getImage().getHeight()+ buffer)<= ldzPane.getHeight()))
imgView.setTranslateY(newYPosition.get());
event.consume();
}
});
imgView.setOnDragDone(new EventHandler<DragEvent>() {
public void handle(DragEvent event) {
LinkedList<RHSIconizedToken> iTokens = GameBoard.getInstance().getTokenBayItokens();
if (event.getTransferMode() == TransferMode.MOVE) {
RHSIconizedToken element = iTokens.remove(index);
iTokens.add(index, new RHSIconizedToken(element.getImage(), new SourceToken("removed", "removed"), index));
imgView.setVisible(false);
GameBoard theGameBoard = GameBoard.getInstance();
GUI theGUI = GUI.getInstance();
//was this the last one removed from the rhs?
//if so we need to signal the CompileButton to be turned on!
if(theGameBoard.isRHSempty())
theGUI.enableCompileButton();
else
theGUI.disableCompileButton();
}
event.consume();
}
});
imgView.setOnMouseEntered(new EventHandler <MouseEvent>() {
public void handle(MouseEvent event) {
imgView.setEffect(new Glow(0.5));
event.consume();
}
});
imgView.setOnMouseExited(new EventHandler <MouseEvent>() {
public void handle(MouseEvent event) {
imgView.setEffect(new Glow(0.0));
imgView.setOpacity(1);
event.consume();
}
});
return imgView;
}
Java 8 is scheduled to ship with more support for visual drag and drop. DragBoard's setDragView (Image, XOffset, YOffset) method works great, and is available with the Java 8 beta.
imgView.setOnDragDetected(new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
ClipboardContent content = new ClipboardContent();
content.putString(RHSIconizedToken.this.tokenLookupInfo());
Dragboard db = imgView.startDragAndDrop(TransferMode.ANY);
db.setDragView(image, 7, 7);
db.setContent(content);
event.consume();
}
});
I think this would help you:
DraggablePanelsExample.java
I usedd this for my Panels too, and it works for ImageView like a charm.
EDIT:
You are combining this false! You can not combine DragDetected with MouseDragged.
If you start dragging in JavaFX, the mouseevents does not get fired anymore.
If you want a Drag-Board-String then simply save you String in you class.
ps: replace DragDone with MouseReleased

CheckBox Code Doesn't working there is no compile time error

public SubMenuItemCommand(MenuObject menuItem,JsArray listOfMenu,ToothWidget toothWidget){ this.menuItem=menuItem; this.listOfMenu=listOfMenu; this.toothWidget=toothWidget; }
cb1.addClickHandler(new ClickHandler(){
public void onClick(ClickEvent event) {
System.out.println(menuItem.getImage());
boolean checked = ((CheckBox) event.getSource()).getValue();
if (checked) {
System.out.println("hello3");
int left = toothWidget.getToothImage().getAbsoluteLeft();
int top = toothWidget.getVPanel().getAbsoluteTop();//toothWidget.getToothImage().getAbsoluteTop();
Image im = new Image();
im.setUrl("images/"+menuItem.getImg());
int offx = left;
int offy = top;
final PopupPanel popup1 = new PopupPanel(true);
popup1.setStylePrimaryName("transparent");
popup1.setPopupPosition(Math.max(offx, 0),Math.max(offy, 0));
//popup.add(im);
ToothWidget wgt = new ToothWidget(toothWidget.getToothNumber(),menuItem.getImg(), toothWidget.getTeeth());
toothWidget.getTeeth().getMap().get(toothWidget.getToothNumber()).put(menuItem.getName(), wgt);
wgt.setMenu(toothWidget.getMenu());
wgt.setPanel(popup1);
popup1.add(wgt);
popup1.show();
}
else{
ToothWidget wgt = toothWidget.getTeeth().getMap().get(toothWidget.getToothNumber()).remove(menuItem.getName());
wgt.getPanel().hide();
}
}
});
Usually it is a path issue. i.e image location is not what you are setting up.
If you have images folder in your gwt module's public folder you need to use im.setUrl(GWT.getModuleBaseURL()+"/images/"+menuItem.getImg());
If you have images in your war/images you need to use
im.setUrl(GWT.getHostPageBaseURL()+"/images/"+menuItem.getImg());
Also you can always use FireFox/FireBug's Network/Image console to check which URL is being accessed.

SWT: remove the invisible component space in composite

I put three composite(1,2,3) inside one composite0, so they all set their layout based on the composite0.setLayout(new FormLayout()). The problem I have now is, I make the composite3 invisible: composite3.setVisible(false); (I dont want to delete the data, still need that component, but just doesn't want it shows on the UI), and there's a big gap after componite2 now. How to remove the big gap(which use to put the composite2)? thank you in advanced!
Snippet 313 of the official SWT examples should help you with that. You basically set a different FormData on composite3 depending on whether you want to show or hide it.
I have a databinding observable for that, but it assumes the controls to be within a GridLayout. I use it to show and hide composites and widgets within a wizard page, depending on the selection state of a checkbox.
To use it, set it up with something like this:
DataBindingContext dbc = new DataBindingContext();
Button button = new Button(composite, SWT.CHECK);
IObservableValue target = SWTObservables.observeSelection(button);
dbc.bindValue(target, new HideControlObservable(someControl));
Here is the observable:
/**
* Observable to control the presence (visibility and size) of any control
* within a grid layout.
* <p>
* Changing the value of this observable will do two things:
* <ol>
* <li>Set the visibility of the control</li>
* <li>Set the size of the control to zero when the control is invisible.</li>
* </ol>
* So, when using this observable, the control will not only be invisible,
* but it will be gone, completely. Normally, when setting the visibility of
* a control to <code>false</code>, the control will not be displayed but
* will still take all the space on the screen.
* </p>
* <p>
* <strong>Note:</strong> this observable works for controls within a
* <strong>GridLayout only</strong>.
* </p>
*/
public class HideControlObservable extends WritableValue implements IValueChangeListener {
private final DataBindingContext dbc = new DataBindingContext();
private final ISWTObservableValue sizeObservable;
private final Point size = new Point(0, 0);
private final Control control;
public HideControlObservable(Control control) {
super(control.getVisible(), Boolean.class);
this.control = control;
UpdateValueStrategy never = new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER);
dbc.bindValue(SWTObservables.observeVisible(control), this, never, null);
sizeObservable = SWTObservables.observeSize(control);
sizeObservable.addValueChangeListener(this);
if (!control.isVisible()) {
GridData gd = (GridData) control.getLayoutData();
if (gd == null) {
gd = new GridData();
}
gd.exclude = true;
control.setLayoutData(gd);
control.setSize(new Point(0, 0));
}
}
#Override
public void doSetValue(Object value) {
super.doSetValue(value);
Boolean bool = (Boolean) value;
if (bool) {
GridData gd = (GridData) control.getLayoutData();
if (gd == null) {
gd = new GridData();
}
gd.exclude = false;
control.setLayoutData(gd);
control.setSize(size);
control.getParent().layout();
} else {
GridData gd = (GridData) control.getLayoutData();
if (gd == null) {
gd = new GridData();
}
gd.exclude = true;
control.setLayoutData(gd);
control.setSize(new Point(0, 0));
control.getParent().layout();
}
}
#Override
public synchronized void dispose() {
sizeObservable.dispose();
super.dispose();
}
#Override
public void handleValueChange(ValueChangeEvent event) {
Point newSize = (Point) event.getObservableValue().getValue();
if (newSize.x > size.x) {
size.x = newSize.x;
}
if (newSize.y > size.y) {
size.y = newSize.y;
}
}
}
FormData data = new FormData();
data.left = new FormAttachment(0, 100, EditorPanel.SPACING);
data.top = new FormAttachment(section1, EditorPanel.SPACING);
data.height = 0;
data.width = 0;
addSectionFormData(a, b, c);
a.setLayoutData(data);
b.setLayoutData(data);
this solve the problem! :)

GWT Canvas2D draw Image

/**
* This is the entry point method.
*/
public void onModuleLoad() {
Canvas canvas = Canvas.createIfSupported();
final Context2d context2d = canvas.getContext2d();
RootPanel.get("canvas").add(canvas);
Image img = new Image("face.png");
final ImageElement face = ImageElement.as(img.getElement());
img.addLoadHandler(new LoadHandler() {
#Override
public void onLoad(LoadEvent event) {
context2d.drawImage(face, 0, 0);
}
});
//RootPanel.get("canvas").add(img);
}
This is my code. I want to draw the image onto the canvas.
This works if the last line:
RootPanel.get("canvas").add(img);
is NOT commented out.
But with the line commented it seems that the image wont be loaded or so. Any ideas?
The Image widget has to be added to the page to trigger image load. Just make it invisible:
public void onModuleLoad() {
Canvas canvas = Canvas.createIfSupported();
final Context2d context2d = canvas.getContext2d();
RootPanel.get().add(canvas);
Image img = new Image("face.png");
final ImageElement face = ImageElement.as(img.getElement());
img.addLoadHandler(new LoadHandler() {
#Override
public void onLoad(LoadEvent event) {
context2d.drawImage(face, 0, 0);
}
});
img.setVisible(false);
RootPanel.get().add(img);
}
Have you placed the image within the WEB-INF folder? That's where GWT loads the images from. Normally it's recommend to create an "images" folder so you can load it with "images/face.png".
You can use an image loader, such as: http://code.google.com/p/google-web-toolkit-incubator/wiki/ImageLoader.
If you want to avoid needing to add the image to the page to load (which can cause the image to flash up on the page), you can use a native JavaScript load.
package com.mypackage;
public interface Loaded<T> {
public void data(T data);
}
public static native void loadImageNative(String imgBase64, Loaded<ImageElement> loaded) /*-{
var image = new Image();
image.onload = function() {
loaded.#com.mypackage.Loaded::data(*)(image);
};
image.src = imgBase64;
}-*/;
public void onModuleLoad() {
Canvas canvas = Canvas.createIfSupported();
Context2d context2d = canvas.getContext2d();
String base64Img = MyResouces.myImage().getSafeUri().asString();
RootPanel.get().add(canvas);
loadImageNative(base64Img, imageLoaded -> {
context2d.drawImage(imageLoaded, 0, 0);
});
}