Using Swing Components in an Applet

One awesome thing about Java Swing is that you can use it’s components inside a Java Applet, without having to change large portions of your existing code. For simple applications, all you have to do is to extend your MainWindow from the JApplet class, instead of JFrame.
For example:

public class MainApp extends JApplet{
    public void init(){
        Container c = getContentPane();
        c.setLayout(new BorderLayout());
        c.add(new JLabel("Hello!"),BorderLayout.NORTH);
        c.add(new JButton("Stupid Button!"),BorderLayout.SOUTH);
    /* You do not need a static main method for
       this to run. */


And it doesn’t end with just Buttons and Labels, you can add any Swing Component you like, including Tabbed-Panes, Text-Panes, MDI etc. Which means you can get pretty creative. For example here is an incomplete multi-tabbed text editor applet I made last month:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s