5

I am a dotnet guy and am trying to create a java applet for my application. I have been able to successfully create the applet and its also working fine in my application after I signed it.

The only issue that I have is that when I embed it into an HTML file (in my case the .cshtml file), I see a white border around the applet and this is not a style in the HTML.

I've been trying to get rid of the border but I was not able to do it. the applet only contains a button which has an Icon to it. thats the only control and I've set the border property of the button to EmptyBorder

here's the screen shot of the button when you view it in the browser.
Dx Button Image
notice the Dx in the Screen Shot. the Dx is a java applet and you can notice the WHITE border around it.

here's the HTML

<applet width="55" height="40" border="0" 
        codebase="~/Content/My/applet" 
        id="DxApplet" name="DxApplet" 
        code="DxApplet.class" 
        archive="DxButtonApplet.jar">
    <param name="boxborder" value="false"> 
    @Html.Raw(ViewBag.AppletParameters)
</applet>

additionally I added the following CSS but this didn't help either.

applet:focus {
    outline: none;
    -moz-outline-style: none;
} 

I've also added the following code in the init method of the applet

jButton1 is the name of the Dx button.

jButton1.setBorder(null);
jButton1.setBorder(BorderFactory.createEmptyBorder());

but this hasn't helped either.

Can you please tell me where am I going wrong?

Here's the stripped down applet code: https://gist.github.com/anonymous/1f31a97b68d34a5821e9

  • Relying on a Java applet in a newly developed application is a really bad idea. Google Chrome and Microsoft Edge (the new browser in Windows 10) no longer support the Java plugin, and Firefox and Safari disable Java applets by default — in short, your site will not work in many browsers if it requires Java. Unless you have special needs that cannot be satisfied by *anything* else, I'd strongly recommend researching alternatives. –  Jul 17 '15 at 22:28
  • I know that @duskwuff. I've already researched that and know that most new browsers do not support it. however the thing is that this applet is used to open a file from the local file system and I don't see anything else that can do it. its a medical product and has a lot of other components that the physicians use that cannot be put into a webapp. – Silent Hunter Jul 17 '15 at 22:36

1 Answers1

2

If your whole applet is just one clickable area, I wouldn't use a JButton at all. Just register a MouseListener on a JPanel and you're good to go. JButton comes with a number of extra "features" like shading and hover behavior that's great in a GUI app, but not what you want in an applet who's sole purpose is to process a single click.

The problem you're running into is because you're using the Nimbus Look and Feel. If you didn't know you were doing that, that's the problem with auto-generating code - it does things you didn't ask it to.

The documentation for .setBorder() mentions this issue:

Although technically you can set the border on any object that inherits from JComponent, the look and feel implementation of many standard Swing components doesn't work well with user-set borders.

So your attempts to overwrite the border aren't doing anything because you asked Swing to use the Nimbus LaF.

Easy fix: don't use the Nimbus LaF; just delete the Nimbus-related code from init().

Better fix: don't use a JButton, use a JPanel to listen for clicks and a JLabel to display your image. You don't want the behavior of a JButton, so don't use it. This is a little more effort (you have to center the JLabel) but it's the "right" way to do it, and you can basically turn jButton1 into a JLabel and your code will work.

Here's a screenshot of what I ended up seeing:

Screenshot of three different versions of the applet; original, Nimbus-disabled, and JLabel instead of JButton

I didn't bother tweaking the layout and color of the JLabel solution so it doesn't look as nice, but you can see there's no borders on either the second or third applet.

Some more references: The Swing source code (take a look at JButton and AbstractButton; they do a lot of work you don't need here), Border with rounded corners & transparency, and Java rounded corners on JFrame?

Community
  • 1
  • 1
dimo414
  • 47,227
  • 18
  • 148
  • 244
  • I already have the **background** set to black. can I have an image inside the panel? also even if I remove the button and just go with the JPanel, how would that remove the outer border? is it something that all Java Applets have? – Silent Hunter Jul 17 '15 at 23:50
  • 1
    Sure, you can put anything you need into a `JPanel`. The outer border is part of the `JButton`, not the applet itself. It's easy enough for you to test - just comment out the line that adds the button and see what the applet looks like. Consider posting an [SSCCE](http://sscce.org/) applet demo so we can replicate what you're seeing. – dimo414 Jul 18 '15 at 05:51
  • Also, just to clarify, did you set the background of the button, or the applet? – dimo414 Jul 18 '15 at 15:15
  • yes. I've already set the background of the button to black and also the container background is set to black. let me try your suggestion. will let you know if it works in some time. – Silent Hunter Jul 20 '15 at 16:05
  • Also, if you can create a demo applet you can share I can try to replicate / fix the behavior you're seeing. – dimo414 Jul 20 '15 at 17:02
  • yeah. I am working on it but where do i share the code? can i just edit my question and share it? – Silent Hunter Jul 20 '15 at 17:35
  • That's the generally recommended way to do it. Alternatives would be a [snippet](https://bitbucket.org/snippets) or [gist](https://gist.github.com/) if it's too large. When you post it, reply here and I'll get a notification. – dimo414 Jul 20 '15 at 18:15
  • here you go https://gist.github.com/anonymous/1f31a97b68d34a5821e9 this is the applet code that i have. please check it and let me know what am I doing wrong. – Silent Hunter Jul 20 '15 at 18:29
  • Sorry, not yet. There's no way for me to run applets in my work environment, I have to do it from home. – dimo414 Jul 21 '15 at 17:44
  • oh. ok. will wait and try to work on your suggestion. – Silent Hunter Jul 21 '15 at 18:01
  • @SilentHunter I got your applet loaded up (after a *lot* of fighting with the security model - you must really love your job if you're willing to put up with applet development...) and replicated the border behavior you saw. I've updated my answer. – dimo414 Jul 22 '15 at 04:51
  • Thanks @dimo414. who loves their job? ;) its something new that I am getting to learn, though its old school stuff but I've got no choice n have to use it in my project. I was thinking of using the HTML5 File System API but that doesn't seem to work on all the browsers. so I am stuck with a java applet and have to keep signing it and I am pretty sure the support for it is going to end in the new browser versions. I know chrome is doing it. well lets see. let me try your solution and get back to you. Thanks again. – Silent Hunter Jul 22 '15 at 16:41
  • one thing I notice when i remove the Nimbus thing is that the image icon Dx shows a white border when I view it. see my updated question – Silent Hunter Jul 22 '15 at 16:49
  • Try cleaning up your other border / background settings. It'll probably be easier to start from zero (i.e. create a new applet, add the button, set the icon) and restore the functionality you need piece by piece, rather than trying to cobble your existing code back into shape. – dimo414 Jul 22 '15 at 16:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/83997/discussion-between-silent-hunter-and-dimo414). – Silent Hunter Jul 22 '15 at 16:52
  • Thank you @dimo414. your fix works. thank you very much :) – Silent Hunter Jul 22 '15 at 18:54