Reputation: 493
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
Upvotes: 0
Views: 3099
Reputation: 11
Try to delete your paint method and you'll see it works. The problem might be the fact that since you've got a paint method all the changes are made through it. It's weird though because it does display a JButton.
Upvotes: 1
Reputation: 16888
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.
Upvotes: 1
Reputation: 882
I see several problems with your code here:
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).
Upvotes: 2