tags:

views:

522

answers:

2

So I am creating this applet which I want to have full on swing components in it. I have looked at all the docs, I have made the applet, and I can get something to show up in it if I override the update(Graphics g) method, but simply adding components to the contentPane doesn't seem to be doing it! What am I doing wrong?

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import org.steephill.kindlab.LabApp;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

public class ClientApplet extends JApplet {

    ClientTreePanel treePanel;

    public void destroy() {
     // Put your code here
    }

    public String getAppletInfo() {
     return "KindLab Client Applet";
    }

    public void init() {

     try {
      LabApp.initializeHibernate();
      if (!LabApp.authenticate("user", "pass")) {

       JOptionPane.showMessageDialog(this, "authentication failed");
      } else {

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

        }
     } catch (Exception ex) {
      JOptionPane.showMessageDialog(this, "error intitializing applet\r\n" + ex.getMessage());
     }
    }

    protected void createGUI() {

     treePanel = new ClientTreePanel();
     treePanel.setVisible(true);
     getContentPane().add(new JLabel("TESTING!"));
     getContentPane().add(treePanel);

     System.out.println("THIS DOES RUN");
    }

    public void start() {
     // Put your code here
    }

    public void stop() {
     // Put your code here
    }

    /*  if I uncomment this method, it WORKS and I get "Hello World!"
    public void paint(Graphics g) {
     super.paint(g);
     g.drawString("Hello World!",25,25);
    }
     */
}

Please, help! And thank you! Joshua

+2  A: 

Hi, I see several problems with your code here:

  1. you do not call pack() at the end of your GUI setup
  2. you add several components to the applet's content pane, but without any layout constraints. The default content pane usually is a BorderLayout, so adding two components without any constraints will probably only put the ClientTreePanel on top.

Since you do not call pack(), the layout will not be calculated, which for your case probably results in nothing being displayed (you did not provide the code for ClientTreePanel).

boxofrats
Since there is no pack() and no setLocationRelativeTo(null), it will appear as an extremely small window in the upper left corner of the screen. It's very easy to miss if you're not looking for it up there.
Michael Myers
Actually, I take that back--I was thinking of desktop applications. I don't know what an applet would do.
Michael Myers
There is no pack() method in JApplet, or a setLocationRelativeTo. It's handled by the HTML that it's embedded in. So I don't know why it wouldn't show up at all. You're right about item #2, though.
Michael Myers
+1  A: 

You shouldn't have to call pack() - the layout will be calculated when the component first becomes realized, which happens when you call pack - but also when the component first becomes visible.

The "adding components without constraints" is on the right track - you should change the code adding components to the content pane to :

getContentPane().add(new JLabel("TESTING!"), BorderLayout.NORTH);
getContentPane().add(treePanel, BorderLayout.CENTER);

The other problem is why your ClientTreePanel component isn't showing up - it could be sizing, or a layout problem, or other things - but without seeing that code, it would just be guesses.

Nate
+1 just tested your solution locally and it fixed the problem for me.
Pool