Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

making an instance of an applet in an applet  RSS feed

 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have a class that extends JApplet, this class has a JTextField and a button next to it. What it does is when a valid url is entered in the JTextField and a button is pressed, a new browser window is poped-up showing web page of the url entered. What I want to do is make an instance of this applet in another applet so that I can use that object as any regular component. So for example if the name of my URL class is URLFiled, I want to write in my main applet: URLField url = new URLField(); and add this field in GridBagLayout let's say. However, I've tried it and it does not work. Am I doing something wrong or is it not possible at all?
Here is the code:
URLField.java
=============
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class URLField extends Applet implements ActionListener {
Button b;
TextField t;

public URLField(){}
public void init() {
t = new TextField(20);
add(t);
b = new Button("GO");
add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == b) {
System.out.println("button");
try{
getAppletContext().showDocument(new URL(t.getText()), "_blank");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
testURL.java
============
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
public class testURL extends JApplet{
Container container;
GridBagConstraints gridBagConstraints;
public void init() {
container = getContentPane();
container.setLayout(new GridBagLayout());
container.setBackground(new Color(153,204,255));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridwidth = 8;
gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 0);
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
container.add(new URLField(), gridBagConstraints);
}
}

thanks,
Alex Kravets
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that the first applet is a JApplet and the sencond is just an Applet. You shouldn't be mixing Swing and AWT components. AWT components will always show on top of any Swing components. ( So you are probably really adding the JApplet to the Applet, it's just impossible to see it because the Applet is displayed over it...)

-Nate
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!