johnsonwi
johnsonwi

Reputation: 159

Using swing components with an applets paint method

I have a functioning Java applet which can be embedded into a web page and wish to now add some swing components for additional functions. However whenever I add a component like a JLabel, it simply does not appear on the viewport/canvas unless I remove my entire paint method. The latter option allows me to add swing components but naturally I then cannot render any shapes. It appears to resemble an eXclusive OR (XOR), either one but not the other.

Is there anyway in a native Java applet to add swing components and still maintain the paint(Graphics g) method. Please note that I am inheriting from Applet and not JApplet.

Upvotes: 1

Views: 893

Answers (4)

johnsonwi
johnsonwi

Reputation: 159

I was able to visualise the components over the applet canvas by invoking

paintComponents(g);

Thus the components are thereafter painted AFTER the actual paint(Graphics g) method completes its epoch.

Upvotes: 0

Theodore Norvell
Theodore Norvell

Reputation: 16251

At the end of your paint method, call super.paint(g).

Upvotes: 0

oikku
oikku

Reputation: 562

If you override paint method in applet then there is no simple way.

What you could do instead of overriding paint in applet.

  1. Extend JComponent instead and do the custom drawing there.
  2. create JPanel that contains all the needed swing components including the component from earlier step.
  3. Add that panel to applet which is using default paint method.

Upvotes: 3

Dan D.
Dan D.

Reputation: 32391

I suggest that you add the components to a separate panel that doesn't override paint. By overridding paint, you are customizing the way the component is drawn, so the layout manager and the components it has to manage do not count, it's only the implementation of paint you wrote that dictates how stuff is rendered.

So, your applet would contain a panel with the components and a panel that does the custom painting.

Upvotes: 2

Related Questions