5

I am trying to view a JApplet within a JFrame.

Class: Paint
public void paint(Graphics g) {
  g.drawString("hi", 50, 50);
}

public static void main(String args[]) {
  JFrame frame = new JFrame("test");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setJMenuBar(methodThatReturnsJMenuBar());

  JPanel panel = new JPanel(new BorderLayout());
  frame.add(panel);

  JApplet applet = new Paint();
  panel.add(applet, BorderLayout.CENTER);
  applet.init();
  frame.pack();

  frame.setVisible(true);
}

The applet shows up in the Window, but there is no background (it's transparent), and when I click on the Menu, the list is covered. How do I make it so that the Menu list isn't covered, and there is a background?

Edit: When I draw a white rectangle, it fixes the background problem, but the Menu list is still covered.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
LanguagesNamedAfterCofee
  • 5,782
  • 7
  • 45
  • 72
  • Why are you doing this? What problem are you trying to solve? – Greg Mattes May 15 '11 at 02:26
  • Just for convenience (I don't want to launch from appletviewer or web browser). – LanguagesNamedAfterCofee May 15 '11 at 02:33
  • I don't know your exact requirements, but it doesn't sound overly convenient to me. I'm of the opinion that applets aren't usually the way to go today. You might check out JavaFX or JNLP for alternatives. http://javafx.com/ http://en.wikipedia.org/wiki/Java_Web_Start – Greg Mattes May 15 '11 at 02:36
  • 3
    Many things wrong going on here. A JApplet is a root container and thus I believe is a heavyweight container and won't lay nice with your lightweight containers such as the containing JPanel. Why not gear your GUI's to create JPanels and then use them in a JApplet if you want them as an applet or in a JFrame if you want them in a stand alone? – Hovercraft Full Of Eels May 15 '11 at 03:00
  • 1
    Please accept and/or vote for answers you found useful, as mentioned in the [faq](http://stackoverflow.com/faq). – trashgod May 15 '11 at 06:45

1 Answers1

6

I would gear my GUI creation towards making a JPanel and then use the JPanel as I desire, either in an JApplet or a JFrame. For e.g.,

import java.awt.*;
import javax.swing.*;

public class MyPanel extends JPanel {
   private static final Dimension PREF_SIZE = new Dimension(400, 300);

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.drawString("hi", 50, 50);
   }

   @Override
   public Dimension getPreferredSize() {
      return PREF_SIZE;
   }

   public JMenuBar methodThatReturnsJMenuBar() {
      JMenu menu = new JMenu("Menu");
      JMenuBar menuBar = new JMenuBar();
      menuBar.add(menu);
      return menuBar;
   }
}

Then to use in an applet:

import javax.swing.JApplet;

public class MyApplet extends JApplet {
   public void init() {
      try {
         javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
               createGUI();
            }
         });
      } catch (Exception e) {
         System.err.println("createGUI didn't successfully complete");
      }
   }

   private void createGUI() {
      getContentPane().add(new MyPanel());
   }
}

Or in a JFrame:

import javax.swing.JFrame;

public class MyStandAlone {
   private static void createAndShowUI() {
      MyPanel myPanel = new MyPanel();
      JFrame frame = new JFrame("MyPanel");
      frame.getContentPane().add(myPanel);
      frame.setJMenuBar(myPanel.methodThatReturnsJMenuBar());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    +1, interesting approach to have the panel create the menubar. Don't forget to set the menu bar on the applet as well. – camickr May 15 '11 at 03:17
  • @camickr, thanks. If I'm not overriding a JPanel method, I sometimes get the JPanel in the same way and have the main class extend nothing -- all in the name of composition over inheritance. But on a practical side, it helps when coding in my IDE since it will only present the methods I've made for the class rather than all the methods of the JPanel. – Hovercraft Full Of Eels May 15 '11 at 03:21