Categories
discuss

Remove all classes that begin with a certain string

I have a div with id="a" that may have any number of classes attached to it, from several groups. Each group has a specific prefix. In the javascript, I don’t know which class from the group is on the div. I want to be able to clear all classes with a given prefix and then add a new one. If I want to remove all of the classes that begin with “bg”, how do I do that? Something like this, but that actually works:

$("#a").removeClass("bg*");

Answer

With jQuery, the actual DOM element is at index zero, this should work

$('#a')[0].className = $('#a')[0].className.replace(/bbg.*?b/g, '');
Categories
discuss

How do you “OR” criteria together when using a criteria query with hibernate?

I’m trying to do a basic “OR” on three fields using a hibernate criteria query.

Example

class Whatever{
 string name;
 string address;
 string phoneNumber;
}

I’d like to build a criteria query where my search string could match “name” or “address” or “phoneNumber”.

Answer

You want to use Restrictions.disjuntion(). Like so

session.createCriteria(Whatever.class)
    .add(Restrictions.disjunction()
        .add(Restrictions.eq("name", queryString))
        .add(Restrictions.eq("address", queryString))
        .add(Restrictions.eq("phoneNumber", queryString))
    );

See the Hibernate doc here.

Categories
discuss

jQuery and Java applets

I’m working on a project where we’re using a Java applet for part of the UI (a map, specifically), but building the rest of the UI around the applet in HTML/JavaScript, communicating with the applet through LiveConnect/NPAPI. A little bizarre, I know, but let’s presume that setup is not under discussion. I started out planning on using jQuery as my JavaScript framework, but I’ve run into two issues.

Issue the first:

Selecting the applet doesn’t provide access to the applet’s methods.

Java:

public class MyApplet extends JApplet {
  // ...
  public String foo() { return "foo!"; }
}

JavaScript:

var applet = $("#applet-id");
alert(applet.foo());

Running the above JavaScript results in

$("#applet-id").foo is not a function

This is in contrast to Prototype, where the analogous code does work:

var applet = $("applet-id");
alert(applet.foo());

So…where’d the applet methods go?

Issue the second:

There’s a known problem with jQuery and applets in Firefox 2: http://www.pengoworks.com/workshop/jquery/bug_applet/jquery_applet_bug.htm

It’s a long shot, but does anybody know of a workaround? I suspect this problem isn’t fixable, which will mean switching to Prototype.

Thanks for the help!

Answer

For the first issue, how about trying

alert( $("#applet-id")[0].foo() );

For the second issue here is a thread with a possible workaround.

Quoting the workaround

// Prevent memory leaks in IE
// And  prevent errors on refresh with events  like mouseover in other  browsers
// Window isn't included so as not to unbind existing unload events
jQuery(window).bind("unload",
function() {
        jQuery("*").add(document).unbind();
});

change that code to:

// Window isn't included so as not to unbind existing unload events
jQuery(window).bind("unload",
function() {
        jQuery("*:not('applet, object')").add(document).unbind();
});
Categories
discuss

How do you set focus to the HTML5 canvas element?

I’m using the HTML5 <canvas> element in Firefox 2.0.0.16 and in Safari 3.1.2, both on my iMac. (I’ve tried this in Firefox 3.0 on Windows as well, also to no avail.) The tag looks like this:

<td>
   <canvas id="display"
           width="500px"
           height="500px">
   </canvas>
</td>

I have a button to “activate” some functionality that interacts with the canvas. That button’s onclick() event calls a function. In that function I have the following line:

document.getElementById("display").focus();

This does not work. Firebug reports no error. But the focus still remains where it was. I can click on the canvas or tab towards the canvas and focus will be lost from the other elements, but apparently never be gained on by the canvas (The canvas’s onfocus() event never fires).

I find this odd. Is it that the canvas simply cannot get focus, or am I missing something here? Any insight would be appreciated.

Thank you.

Answer

Give the canvas a tab index:

   <canvas id="display"
           width="500px"
           height="500px"
           tabindex="1">
   </canvas>
Categories
discuss

Java Right Click does not make a selection. What is the easiest way to solve this globally?

Is there a way to globally make right click also select the element that you right click on?

From what I understand this has been a bug in Swing for a long time likely to never be fixed because at this point applications depend on it.

Any advice on doing this on a global scale? Perhaps on the L&F?

Answer

Using the Glass Pane will do the trick.

Here’s a tutorial on how to use the glass pane to get the right click button and redispatch it to the right component.


As the glass pane is not a solution in this case, I suggest you take a look at the Toolkit class. Specificaly the addAWTEventListener method. You can add a global event listener with it. To add a mouse event listener:

Toolkit.getDefaultToolkit().
        addAWTEventListener(listener, AWTEvent.MOUSE_EVENT_MASK);

Cheers

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..