Remove the outer white border in the java applet if it is embedded in the HTML page

I am a guy in dotnet and trying to create a java applet for my application. I was able to successfully create the applet , and it also works great in my application after I signed it.

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

I tried to get rid of the border, but I could not do it. the applet contains only the button that has the Icon . this is the only control and I set the border property of the EmptyBorder button

here is a screenshot when you view it in a browser.
Dx Button Image
pay attention to Dx in Screen Shot. Dx is a java applet , and you can notice WHITE around it.

here is 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> 

In addition, I added the following CSS , but that didn't help either.

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

I also added the following code in the applet init method

jButton1 is the name of the Dx button.

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

but that didn't help either.

Could you tell me where I am wrong?

Here is the applet code below: https://gist.github.com/anonymous/1f31a97b68d34a5821e9

+5
source share
1 answer

If your entire applet is just one clickable area, I would not use JButton at all. Just register MouseListener on JPanel and you will go well. JButton comes with a few extra “features,” such as shading and hovering behavior that works great in a graphics application, but not what you want in an applet whose sole purpose is to handle one click.

The problem you are facing is that you are using Nimbus Look and Feel . If you did not know that you were doing this, the problem with the auto-generating code is what you did not ask for.

The documentation for .setBorder() mentions this problem:

Although you can technically set a border for any object that inherits from JComponent, the appearance of many standard Swing components does not work well with custom borders.

So, your attempts to rewrite the border do nothing, because you asked Swing to use Nimbus LaF.

Easy fix: do not use Nimbus LaF; just remove the associated variant with init() .

Best fix: do not use JButton , use JPanel to listen for clicks and JLabel to display the image . You do not want JButton behavior, so do not use it. This is a bit more effort (you have to center JLabel ), but this is the “right” way to do it, and you can basically turn jButton1 into jButton1 and your code will work.

Here is a screenshot of what I saw:

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

I did not bother to adjust the layout and color of the JLabel solution so that it doesn’t look so good, but you don’t see the borders without the second or third applet.

A few more links: Swing source code (see JButton and AbstractButton ; they do a lot of work you don't need here), Rounded border and transparency and Java rounded corners on a JFrame?

+2
source

All Articles