Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove outer white border in a java applet when embedded inside an HTML page

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

like image 791
Silent Hunter Avatar asked Oct 31 '22 23:10

Silent Hunter


1 Answers

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?

like image 194
dimo414 Avatar answered Nov 15 '22 05:11

dimo414