Hyperlink in a TextField on an Applet
How do i insert a hyperlink in a TextField object which is in an applet and make that hyperlink reference a new window
thanks for the help
try the methode in the Interface java.applet Interface AppletContext
showDocument(URL,"_blank)
Similar Messages
-
TextFields / labels in Applet...Urgent help needed please!!
I have been trying for days to add labels and textFields to my applet and can only manage to display one.
My assignment instructions suggest I use makeTextField method and call the setEditable method from the makeTextField method to avoid repetition of code.
The following is my code...I must have this done in the next couple of days so if someone could please please give me some assistance it would be really appreciated....also, thoughts on my layout would be great as well...i cant seem to set up my buttons in the correct order.
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class Registry4b extends Applet implements ActionListener {
public void init() {
backgroundColor = new Color(200,255,255);
this.setLayout(new FlowLayout(FlowLayout.CENTER,4,1));
makeButtons();
row1 = makePanel(new FlowLayout(FlowLayout.LEFT,4,2),backgroundColor);
row1.add(clearB);
row1.add(studFindB);
row1.add(studForB);
row1.add(courB);
row2 = makePanel(new FlowLayout(FlowLayout.LEFT,4,2),backgroundColor);
row2.add(studBackB);
row2.add(courFindB);
row2.add(courForB);
row2.add(studB);
row3 = makePanel(new FlowLayout(FlowLayout.LEFT,4,2),backgroundColor);
row3.add(courBackB);
add(row1);
add(row2);
add(row3);
Panel p = new Panel(new BorderLayout());
Label studID = new Label("STUDENT ID");
TextField entry = new TextField(" ");
p.add(studID,BorderLayout.WEST);
p.add(entry,BorderLayout.CENTER);
Label firstTF = new Label("FIRST NAME");
makePanel(new BorderLayout(2,2),backgroundColor);
add("West",courBackB);
add("East",studB);
makePanel(new BorderLayout(2,2),backgroundColor);
add("North",p);
add("South",courForB);
makePanel(new BorderLayout(2,2),backgroundColor);
add("North",courFindB);
add("South",studBackB);
makePanel(new BorderLayout(2,2),backgroundColor);
add("West",p);
add("East",studForB);
setBackground(backgroundColor);
clearB.addActionListener(this);
courBackB.addActionListener(this);
studB.addActionListener(this);
courForB.addActionListener(this);
courFindB.addActionListener(this);
studBackB.addActionListener(this);
courB.addActionListener(this);
studForB.addActionListener(this);
studFindB.addActionListener(this);
private Label makeLabel(String label) {
Label label1 = new Label(label,Label.RIGHT);
label1.setFont(new Font("Courier",Font.BOLD,10));
return label1;
public void start() {
appletWidth = 8*4+row1.getSize().width;
appletHeight = 8*(2+courBackB.getSize().height);
public void paint(Graphics g) {
setSize(appletWidth,appletHeight);
validate();
public void actionPerformed(ActionEvent e) {
String s = (String)e.getActionCommand();
private Button makeButton(String label, Color color, Font font) {
Button b = new Button(label);
b.setBackground(color);
b.setFont(font);
return b;
private Panel makePanel(LayoutManager lm, Color c) {
Panel p = new Panel();
p.setLayout(lm);
p.setBackground(c);
return p;
private void makeButtons() {
Font f = new Font("Courier", Font.BOLD, 10);
Color grey = new Color(255,100,100);
clearB = makeButton("CLEAR",grey,f);
studB = makeButton(" STUDENTS ",grey,f);
studForB = makeButton("->",grey,f);
studFindB = makeButton("FIND",grey,f);
courFindB = makeButton("FIND",grey,f);
studBackB = makeButton("<-",grey,f);
courB = makeButton(" COURSES ",grey,f);
courBackB = makeButton("<-",grey,f);
courForB = makeButton("->",grey,f);
TextField addressTF = new TextField("ADDRESS", 10);
static final String initialString = " ";
String Filler = " ";
Panel row1, row2, row3, p1;
int appletWidth, appletHeight;
Button clearB, studForB, studFindB, courFindB,
studBackB,courB, courBackB, studB, courForB;
Color backgroundColor;
the buttons currently dont do anything. this is stage 1 of 3 for my 3 part assignment. for now i am just supposed to get the format right and show sample text in the textfields.
the format is supposed to look something like the following (cant copy existing as it is protected)
<- STUDENTS -> CLEAR <- COURSES ->
student id FIND course id FIND
last name_________first name______course name_____
address________________________________________
city______province___________p.code______________
phone__________e-mail_______coordinator___________
#of courses completed_____ #of students passed______
student's average grade___ students grade ave grade___
messages______________________________________
the ones in caps are buttons and the rest are labels with text fields beside most of the labels.
i apologize for the extremely long question but i hope this is what you were looking for to give me some help...anything would be greatly appreciated!!
thanks in advance for anything someone can do for mehi,
may this helps to solve your problem
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class Registry4b extends Applet implements ActionListener {
TextField sid;
TextField cid;
public void init() {
backgroundColor = new Color(200,255,255);
setLayout(new GridLayout(2,1)); // 2 rows, one column, first row for search, the second for output
Panel up=new Panel(new GridLayout(3,1)); // search panel
Panel down=new Panel(new GridLayout(1,1)); //output panel
down.add(new Label("Just for the output"));
Panel p1=makePanel(new GridLayout(1,7),backgroundColor);
makeButtons();
p1.add(studBackB);
p1.add(studB);
p1.add(studForB);
p1.add(clearB);
p1.add(courBackB);
p1.add(courB);
p1.add(courForB);
up.add(p1);
Panel p2=makePanel(new GridLayout(1,3),backgroundColor);
p2.add(makeLabel("Student ID"));
sid=new TextField("");
p2.add(sid);
p2.add(studFindB);
up.add(p2);
Panel p3=makePanel(new GridLayout(1,3),backgroundColor);
cid=new TextField("");
p3.add(makeLabel("Course ID"));
p3.add(cid);
p3.add(courFindB);
up.add(p3);
add(up);
add(down);
private Label makeLabel(String label) {
Label label1 = new Label(label,Label.RIGHT);
label1.setFont(new Font("Courier",Font.BOLD,10));
return label1;
public void start() {
//appletWidth = 8*4+120;//row1.getSize().width;
//appletHeight = 8*(2+courBackB.getSize().height);
appletWidth=200;
appletHeight=90;
public void paint(Graphics g) {
setSize(appletWidth,appletHeight);
validate();
public void actionPerformed(ActionEvent e) {
String s = (String)e.getActionCommand();
private Button makeButton(String label, Color color, Font font) {
Button b = new Button(label);
b.setBackground(color);
b.setFont(font);
return b;
private Panel makePanel(LayoutManager lm, Color c) {
Panel p = new Panel();
p.setLayout(lm);
p.setBackground(c);
return p;
private void makeButtons() {
Font f = new Font("Courier", Font.BOLD, 10);
Color grey = new Color(255,100,100);
clearB = makeButton("CLEAR",grey,f);
studB = makeButton(" STUDENTS ",grey,f);
studForB = makeButton("->",grey,f);
studFindB = makeButton("FIND",grey,f);
courFindB = makeButton("FIND",grey,f);
studBackB = makeButton("<-",grey,f);
courB = makeButton(" COURSES ",grey,f);
courBackB = makeButton("<-",grey,f);
courForB = makeButton("->",grey,f);
TextField addressTF = new TextField("ADDRESS", 10);
static final String initialString = " ";
String Filler = " ";
Panel row1, row2, row3, p1;
int appletWidth, appletHeight;
Button clearB, studForB, studFindB, courFindB, studBackB,courB, courBackB, studB, courForB;
Color backgroundColor;
}interesting for you is only the init()
regards -
How do I put a hyperlink button on the Canvas in Applet
I am tried to put a hyperlink button on the Canvas in Applet.
But Canvas is a componet that can't contain any component.
I hope best way that is like hyperlink of HTML , I just clicked this hyperlink in Canvas,it's automation link to directed homepage.
Can I have any way to do that?You can setup Restrictions...
Tap Settings > General > Restrictions
AFAIK, it's not possible to attach a password for access to the Settings. -
Hello friends
i have one very important job to do i would like my applet to open a browser window with the url that i specify in the textfield of my applet
this is how it starts i open the applet that has a textfield and a button i enter the url in this textfield now i want my applet to initiate a browser window it could be ie or netscape with the url specified in my applet textfield
i hope u got my point
thanks in advance
hariURL yourURL = new URL ("www.YOURChoiceOfURL.COM");
getAppletContext().showDocument(yourURL, "_self" );
just lookup the parameter of the showDocument method at the java api list
hth,
paulo -
Urgent advice needed on textfields...please!!
I have been trying for days to add labels and textFields to my applet and can only manage to display one.
My assignment instructions suggest I use makeTextField method and call the setEditable method from the makeTextField method to avoid repetition of code.
The following is my code...I must have this done in the next couple of days so if someone could please please give me some assistance it would be really appreciated....also, thoughts on my layout would be great as well...i cant seem to set up my buttons in the correct order.
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class Registry4b extends Applet implements ActionListener {
public void init() {
backgroundColor = new Color(200,255,255);
this.setLayout(new FlowLayout(FlowLayout.CENTER,4,1));
makeButtons();
row1 = makePanel(new FlowLayout(FlowLayout.LEFT,4,2),backgroundColor);
row1.add(clearB);
row1.add(studFindB);
row1.add(studForB);
row1.add(courB);
row2 = makePanel(new FlowLayout(FlowLayout.LEFT,4,2),backgroundColor);
row2.add(studBackB);
row2.add(courFindB);
row2.add(courForB);
row2.add(studB);
row3 = makePanel(new FlowLayout(FlowLayout.LEFT,4,2),backgroundColor);
row3.add(courBackB);
add(row1);
add(row2);
add(row3);
Panel p = new Panel(new BorderLayout());
Label studID = new Label("STUDENT ID");
TextField entry = new TextField(" ");
p.add(studID,BorderLayout.WEST);
p.add(entry,BorderLayout.CENTER);
Label firstTF = new Label("FIRST NAME");
makePanel(new BorderLayout(2,2),backgroundColor);
add("West",courBackB);
add("East",studB);
makePanel(new BorderLayout(2,2),backgroundColor);
add("North",p);
add("South",courForB);
makePanel(new BorderLayout(2,2),backgroundColor);
add("North",courFindB);
add("South",studBackB);
makePanel(new BorderLayout(2,2),backgroundColor);
add("West",p);
add("East",studForB);
setBackground(backgroundColor);
clearB.addActionListener(this);
courBackB.addActionListener(this);
studB.addActionListener(this);
courForB.addActionListener(this);
courFindB.addActionListener(this);
studBackB.addActionListener(this);
courB.addActionListener(this);
studForB.addActionListener(this);
studFindB.addActionListener(this);
private Label makeLabel(String label) {
Label label1 = new Label(label,Label.RIGHT);
label1.setFont(new Font("Courier",Font.BOLD,10));
return label1;
public void start() {
appletWidth = 8*4+row1.getSize().width;
appletHeight = 8*(2+courBackB.getSize().height);
public void paint(Graphics g) {
setSize(appletWidth,appletHeight);
validate();
public void actionPerformed(ActionEvent e) {
String s = (String)e.getActionCommand();
private Button makeButton(String label, Color color, Font font) {
Button b = new Button(label);
b.setBackground(color);
b.setFont(font);
return b;
private Panel makePanel(LayoutManager lm, Color c) {
Panel p = new Panel();
p.setLayout(lm);
p.setBackground(c);
return p;
private void makeButtons() {
Font f = new Font("Courier", Font.BOLD, 10);
Color grey = new Color(255,100,100);
clearB = makeButton("CLEAR",grey,f);
studB = makeButton(" STUDENTS ",grey,f);
studForB = makeButton("->",grey,f);
studFindB = makeButton("FIND",grey,f);
courFindB = makeButton("FIND",grey,f);
studBackB = makeButton("<-",grey,f);
courB = makeButton(" COURSES ",grey,f);
courBackB = makeButton("<-",grey,f);
courForB = makeButton("->",grey,f);
TextField addressTF = new TextField("ADDRESS", 10);
static final String initialString = " ";
String Filler = " ";
Panel row1, row2, row3, p1;
int appletWidth, appletHeight;
Button clearB, studForB, studFindB, courFindB,
studBackB,courB, courBackB, studB, courForB;
Color backgroundColor;
thanks in advance for anything someone can do for memakePanel(new BorderLayout(2,2),backgroundColor);
add("West",courBackB);
add("East",studB);
makePanel(new BorderLayout(2,2),backgroundColor);
add("North",p);
add("South",courForB);
makePanel(new BorderLayout(2,2),backgroundColor);
add("North",courFindB);
add("South",studBackB);
makePanel(new BorderLayout(2,2),backgroundColor);
add("West",p);
add("East",studForB);
Those methods make Panels and return them... you are not assigning them any where and they are being lost. And then you directly adding everything to the applet.
Panel panel = makePanel(new BorderLayout(2,2),backgroundColor);
panel.add( .....
Have fun -
URGENT advice needed on labels/textfields please!!
I have been trying for days to add labels and textFields to my applet and can only manage to display one.
My assignment instructions suggest I use makeTextField method and call the setEditable method from the makeTextField method to avoid repetition of code.
The following is my code...I must have this done in the next couple of days so if someone could please please give me some assistance it would be really appreciated....also, thoughts on my layout would be great as well...i cant seem to set up my buttons in the correct order.
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class Registry4b extends Applet implements ActionListener {
public void init() {
backgroundColor = new Color(200,255,255);
this.setLayout(new FlowLayout(FlowLayout.CENTER,4,1));
makeButtons();
row1 = makePanel(new FlowLayout(FlowLayout.LEFT,4,2),backgroundColor);
row1.add(clearB);
row1.add(studFindB);
row1.add(studForB);
row1.add(courB);
row2 = makePanel(new FlowLayout(FlowLayout.LEFT,4,2),backgroundColor);
row2.add(studBackB);
row2.add(courFindB);
row2.add(courForB);
row2.add(studB);
row3 = makePanel(new FlowLayout(FlowLayout.LEFT,4,2),backgroundColor);
row3.add(courBackB);
add(row1);
add(row2);
add(row3);
Panel p = new Panel(new BorderLayout());
Label studID = new Label("STUDENT ID");
TextField entry = new TextField(" ");
p.add(studID,BorderLayout.WEST);
p.add(entry,BorderLayout.CENTER);
Label firstTF = new Label("FIRST NAME");
makePanel(new BorderLayout(2,2),backgroundColor);
add("West",courBackB);
add("East",studB);
makePanel(new BorderLayout(2,2),backgroundColor);
add("North",p);
add("South",courForB);
makePanel(new BorderLayout(2,2),backgroundColor);
add("North",courFindB);
add("South",studBackB);
makePanel(new BorderLayout(2,2),backgroundColor);
add("West",p);
add("East",studForB);
setBackground(backgroundColor);
clearB.addActionListener(this);
courBackB.addActionListener(this);
studB.addActionListener(this);
courForB.addActionListener(this);
courFindB.addActionListener(this);
studBackB.addActionListener(this);
courB.addActionListener(this);
studForB.addActionListener(this);
studFindB.addActionListener(this);
private Label makeLabel(String label) {
Label label1 = new Label(label,Label.RIGHT);
label1.setFont(new Font("Courier",Font.BOLD,10));
return label1;
public void start() {
appletWidth = 8*4+row1.getSize().width;
appletHeight = 8*(2+courBackB.getSize().height);
public void paint(Graphics g) {
setSize(appletWidth,appletHeight);
validate();
public void actionPerformed(ActionEvent e) {
String s = (String)e.getActionCommand();
private Button makeButton(String label, Color color, Font font) {
Button b = new Button(label);
b.setBackground(color);
b.setFont(font);
return b;
private Panel makePanel(LayoutManager lm, Color c) {
Panel p = new Panel();
p.setLayout(lm);
p.setBackground(c);
return p;
private void makeButtons() {
Font f = new Font("Courier", Font.BOLD, 10);
Color grey = new Color(255,100,100);
clearB = makeButton("CLEAR",grey,f);
studB = makeButton(" STUDENTS ",grey,f);
studForB = makeButton("->",grey,f);
studFindB = makeButton("FIND",grey,f);
courFindB = makeButton("FIND",grey,f);
studBackB = makeButton("<-",grey,f);
courB = makeButton(" COURSES ",grey,f);
courBackB = makeButton("<-",grey,f);
courForB = makeButton("->",grey,f);
TextField addressTF = new TextField("ADDRESS", 10);
static final String initialString = " ";
String Filler = " ";
Panel row1, row2, row3, p1;
int appletWidth, appletHeight;
Button clearB, studForB, studFindB, courFindB,
studBackB,courB, courBackB, studB, courForB;
Color backgroundColor;
thanks in advance for anything someone can do for methat works fine
all labels and buttons displayed
a note on layouts NEVER use flow layout
for simple layouts(like this) use a combination of grid and border layouts -
How do I apply textFormat to a single line in a textField.
I have very little experience with text in flash. In a dynamic text field I know I can apply a TextFormat to the entire field but can I apply different fromatting to individual workd in the field if for example I wanted to make a word or a few words of text a hyperlink within a textfield. Can I do that or would I need to use html text?
use setTextFormat(). it accepts a 2nd and 3rd parameter that you can use to assign a textformat to part of a textfield.
-
ActionListener & MouseListener applet
Hi, i have an applet which determines where the user clicks the mouse within the applet and displays coordinates in the status bar
public void mouseClicked(MouseEvent e)
//get coordinates of click
mousex=e.getX(); mousey=e.getY();
paint(getGraphics());
repaint();
public void paint(Graphics g)
//Draw location pointer on map
g.setColor(Color.black);
g.fillOval(mousex-2,mousey-2,4,4);
showStatus("You clicked at ("+mousex+ ","+mousey+")");
}I also have a textfield which allows the user to type in a search term. Then in the actionPerformed() method, a for loop gets the text from the textfield and compares it to an array of keywords; if a match is found then a corresponding message is printed on the screen.
public void actionPerformed(ActionEvent e)
if(e.getSource()==text)
textStr=e.getActionCommand(); //Possibly use getText?
for (int i =0;i<items.length;i++)
if (items.equals(textStr))
dataString = data[i];break;
else
dataString = ("Sorry, No information for " + textStr);
My problem is this: when the mouse click functionality above is enabled, the textfield will not accept any input from the keyboard. If i comment the mouse code out then the textfield works as it should.
Any suggestions/ideas appreciated.
Many thanks.
Joe.what do you mean by it is not working?
what happen when you dont comment out any part of your code andyou click on your applet?
try to separate the graphics part and the t textfield
is your applet implementing actionlistener?
if so change your code
and add a textfield in your applet and then make the textfield implements actionlistener
it should work fine -
Is this a valid work around for disabled TextField Font colors?
We would like to change the font color in disabled TextFields.
Since the AWT calls some native code that apparently overrides
any call to setForeground(), so that the Foreground is always a
light gray color in disabled TextFields I decided to override
the paint() method. This works fine as listed below. The formula
for setting the initial position for the drawString() method has
worked for every Font/Size that I have tested, but I don't like
the added literals (2 and +1) that I am needing to pass to have
the string line up exactly as is displayed automatically.
Is there another way to get these (x,y) values from method calls
to the component, container or FontMetrics that I am missing?
Also, does anyone see anything wrong with this to override the
default implementation?
Even though I do not call super.paint() in the event that
the [enabled] field is true, the TextField still paints itself
correctly with the appopriate Background and Foreground.
If anyone knows why that is please let me know.
Thanks
public void setEnabled(boolean en) {
enabled = en;
super.setEnabled(enabled);
if(enabled) {
setBackground(defaultBackground);
setForeground(defaultForeground);
else {
setBackground(disabledBackground);
setForeground(disabledForeground);
public void paint(Graphics g) {
FontMetrics fm = this.getFontMetrics(this.getFont());
// [enabled] is a private field that is set in the constructor
// by calling this.isEnabled(), and by any call to the setEnabled()
// method
if(!enabled) {
// clear the old string representation
g.clearRect(0, 0, getSize().width, getSize().height);
// set the font color to the defined [disabledForeground] Color
g.setColor(disabledForeground);
// draw the string in the desired Color instead of the default
// disabled color of light gray
g.drawString(getText(), 2, fm.getAscent()+1);
}Walt, I tried that first, but due to the native call to draw
the disabled TextField the setForeground() is useless.
"The solution is platform dependent. A TextField might be painted entierly by native code. And then calls you make to the Graphics object will then have no effect at all. If you run your textfield in an applet in IE you will not see anything but the native TextField painting. The g.drawString, g.setColor or g.clearRect you call seems to be totaly ignored..."
From the tests that I have done I was able to manipulate the
the Graphics object in the paint() method to override the default
representation of the TextField (I also tried this with a Button
and it worked as well). I did run this in IE 5.0 and was able to
get my modifications to work. Since the TextField that I am modifying
must extend another class to inherit some default behavior
I can not create a new TextField that implements a Panel, I thought
about that until I was told that the TextField needs to extend an
already existing class.
I think that my solution should be fine. I was hoping to find out
if anyone knows of a better way to determine the starting
point for the drawString() method besides adding literals for the (x,y)
attributes. If anyone knows a better way than my method please
let me know.
Thanks for the responses -
How to set max length to textfield?
I have a textfield. I want to set maxlength to this textfield. Example, length = 3.
Help me?I succeeded. That link you sent very well. Thanks you. Then it's code:
JTextFieldLimit.java
import javax.swing.text.*;
//import com.sun.java.swing.*;
//import javax.swing.text.*;
public class JTextFieldLimit extends PlainDocument {
private int limit;
// optional uppercase conversion
private boolean toUppercase = false;
JTextFieldLimit(int limit)
super();
this.limit = limit;
JTextFieldLimit(int limit, boolean upper) {
super();
this.limit = limit;
toUppercase = upper;
public void insertString
(int offset, String str, AttributeSet attr)
throws BadLocationException {
if (str == null) return;
if ((getLength() + str.length()) <= limit) {
if (toUppercase) str = str.toUpperCase();
super.insertString(offset, str, attr);
tswing.java
import java.awt.*;
import javax.swing.*;
//import javax.swing.*;
public class tswing extends JApplet{
JTextField textfield1;
JLabel label1;
public void init() {
getContentPane().setLayout(new FlowLayout());
label1 = new JLabel("max 10 chars");
textfield1 = new JTextField(15);
getContentPane().add(label1);
getContentPane().add(textfield1);
textfield1.setDocument
(new JTextFieldLimit(10));
TextField.html
<html>
<applet code=tswing width=500 height=500>
</applet>
</html>
By tungld_c0701m+ -
Urgent assistance needed on textfields/labels please!!
I have been trying for days to add labels and textFields to my applet and can only manage to display one.
My assignment instructions suggest I use makeTextField method and call the setEditable method from the makeTextField method to avoid repetition of code.
The following is my code...I must have this done in the next couple of days so if someone could please please give me some assistance it would be really appreciated....also, thoughts on my layout would be great as well...i cant seem to set up my buttons in the correct order.
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class Registry4b extends Applet implements ActionListener {
public void init() {
backgroundColor = new Color(200,255,255);
this.setLayout(new FlowLayout(FlowLayout.CENTER,4,1));
makeButtons();
row1 = makePanel(new FlowLayout(FlowLayout.LEFT,4,2),backgroundColor);
row1.add(clearB);
row1.add(studFindB);
row1.add(studForB);
row1.add(courB);
row2 = makePanel(new FlowLayout(FlowLayout.LEFT,4,2),backgroundColor);
row2.add(studBackB);
row2.add(courFindB);
row2.add(courForB);
row2.add(studB);
row3 = makePanel(new FlowLayout(FlowLayout.LEFT,4,2),backgroundColor);
row3.add(courBackB);
add(row1);
add(row2);
add(row3);
Panel p = new Panel(new BorderLayout());
Label studID = new Label("STUDENT ID");
TextField entry = new TextField(" ");
p.add(studID,BorderLayout.WEST);
p.add(entry,BorderLayout.CENTER);
Label firstTF = new Label("FIRST NAME");
makePanel(new BorderLayout(2,2),backgroundColor);
add("West",courBackB);
add("East",studB);
makePanel(new BorderLayout(2,2),backgroundColor);
add("North",p);
add("South",courForB);
makePanel(new BorderLayout(2,2),backgroundColor);
add("North",courFindB);
add("South",studBackB);
makePanel(new BorderLayout(2,2),backgroundColor);
add("West",p);
add("East",studForB);
setBackground(backgroundColor);
clearB.addActionListener(this);
courBackB.addActionListener(this);
studB.addActionListener(this);
courForB.addActionListener(this);
courFindB.addActionListener(this);
studBackB.addActionListener(this);
courB.addActionListener(this);
studForB.addActionListener(this);
studFindB.addActionListener(this);
private Label makeLabel(String label) {
Label label1 = new Label(label,Label.RIGHT);
label1.setFont(new Font("Courier",Font.BOLD,10));
return label1;
public void start() {
appletWidth = 8*4+row1.getSize().width;
appletHeight = 8*(2+courBackB.getSize().height);
public void paint(Graphics g) {
setSize(appletWidth,appletHeight);
validate();
public void actionPerformed(ActionEvent e) {
String s = (String)e.getActionCommand();
private Button makeButton(String label, Color color, Font font) {
Button b = new Button(label);
b.setBackground(color);
b.setFont(font);
return b;
private Panel makePanel(LayoutManager lm, Color c) {
Panel p = new Panel();
p.setLayout(lm);
p.setBackground(c);
return p;
private void makeButtons() {
Font f = new Font("Courier", Font.BOLD, 10);
Color grey = new Color(255,100,100);
clearB = makeButton("CLEAR",grey,f);
studB = makeButton(" STUDENTS ",grey,f);
studForB = makeButton("->",grey,f);
studFindB = makeButton("FIND",grey,f);
courFindB = makeButton("FIND",grey,f);
studBackB = makeButton("<-",grey,f);
courB = makeButton(" COURSES ",grey,f);
courBackB = makeButton("<-",grey,f);
courForB = makeButton("->",grey,f);
TextField addressTF = new TextField("ADDRESS", 10);
static final String initialString = " ";
String Filler = " ";
Panel row1, row2, row3, p1;
int appletWidth, appletHeight;
Button clearB, studForB, studFindB, courFindB,
studBackB,courB, courBackB, studB, courForB;
Color backgroundColor;
thanks in advance for anything someone can do for meI haven't tried your code, but since you use a layout manager, you shouldnt need to think about setting the size for the applet.
And I think when you call the method makePanel the last 4 times, you want to use the returned panel to add your components on. Right now you add everything directly to the panel and never use the panels you create.
And you also reuse some of your components more than once (like e.g. courBackB). You need to create a new button everytime, otherwise if you try to add the same button twice, the second one will not be displayed.
Try to add the components one by one and check it is displayed before you add the next one.
If you want to check what components and their sizes etc, you have on your applet, here is a little trick. Get the focus on your applet and press ctrl+shift+F1. If you run the applet in a browser, you will get something in the java console that displays all your GUI components and a description of them. So
It might help you.
Also take a look of the Java Tutorial for the AWT. You can download it from
http://java.sun.com/docs/books/tutorial/information/download.html
called tut-OLDui.zip. -
Adding scrollbars to a applet?
I am trying to write a small java applet for my personal interest, but I can't seem to figure out how to add scroll bars to my applet. What I have so far is this
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.io.IOException;
public class SimpleApplet extends JApplet {
public SimpleApplet() {
JPanel p = new JPanel();
p.setLayout(new GridLayout(20, 3, 3, 3));
p.add(new JLabel("Username"));
p.add(new JTextField());
p.add(new JLabel("Password"));
p.add(new JPasswordField());
p.add(new JLabel("Username"));
p.add(new JTextField());
p.add(new JLabel("Password"));
p.add(new JPasswordField());
p.add(new JLabel("Username"));
p.add(new JTextField());
p.add(new JLabel("Password"));
p.add(new JPasswordField());
p.add(new JLabel("Username"));
p.add(new JTextField());
p.add(new JLabel("Password"));
p.add(new JPasswordField());
Container content = getContentPane();
JScrollPane scroller = new JScrollPane();
scroller.setLayout(new BorderLayout());
content.setLayout(new GridBagLayout()); // Used to center the panel
content.add(scroller);
//content.add(p);
public static void main(String[] args) throws IOException, ClassNotFoundException {
SimpleApplet J = new SimpleApplet();
}when I try to run this the applet fails, but if I delete all the lines with scroller the applet runs fine expect that I can't see some the textfield on the applet unless I enlarge it. I want to add a scrollbar in so that I can scroll down to the unseen parts without enlarging the applet screen. Can any1 help me figure out what i'm doing wrong?Encephalopathic wrote:
bigauto wrote:
I'm still running the applet in eclipse and I haven't tested it on a webpage yet. It shouldn't matter whether it on eclipse or a webpage should it? The applet itself didn't become small, but all the components like the textfield became micoscope.Does it matter if the application is ran on eclipse or a webpage?1) did you look at my example? Did you get rid of your setting the contentpane's layout to gridbaglayout? the contentpane by default uses borderlayout and this should let the jscrollpane fill your applet. If you don't do this, you may need to set a preferred size for your jscrollpane.
2) I think that you can size the applet in Eclipse's Run Dialog, but I'm not sure. You size it in "real life" in the HTML code (I think).Thanks a lot. That fixed it. -
Help!!! Need Java Applet to work in IE
I found some applets (text scrollers) from the sun site that i wanted to use. downloaded them, but they do not work in IE that does not have JVM. in IE that does have JVM it says 'Java 1.1 Required' where the applet should be. is there a simple way to get sun java applets to work with IE and the MS Virtual Machine? I can't require my site visitors to have to download extra toys/plugins...
If you are looking for a purely AWT scrolling applet. Here is one I wrote several years ago. Please note that there are some deprecated methods in it (mainly the thread start and stop methods). It will still compile though. If you want it, take it.
Source:import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
public class ScrollApplet extends Applet implements Runnable
/*=*********************
* private data members *
private Thread myThread = null;
private boolean hasTextDisplayed;
private int appletWidth, appletHeight;
private int yPosition;
private int xPosition;
private Image textImage;
private int imageWidth;
private Graphics textGraphics;
private Cursor oldCursor;
private Cursor newCursor = new Cursor(Cursor.HAND_CURSOR);
//parameters
private int delay;
private String displayText;
private Color bgColor, fgColor;
private int fontSize, fontStyle;
private Font fontType;
private String urlValue;
/*=***************
* applet methods *
public void init()
//assign values to private data members
hasTextDisplayed = false;
appletHeight = yPosition = getSize().height;
appletWidth = xPosition = getSize().width;
//set up the environment
extractParameters();
//determine the yPosition to place the image
calculateYPosition();
//set up the text image
FontMetrics fm = getFontMetrics(fontType);
imageWidth = fm.stringWidth(displayText);
textImage = createImage(imageWidth, appletHeight);
textGraphics = textImage.getGraphics();
textGraphics.setColor(bgColor);
textGraphics.fillRect(0,0,imageWidth,appletHeight);
textGraphics.setColor(fgColor);
textGraphics.setFont(fontType);
textGraphics.drawString(displayText, 0, yPosition);
oldCursor = this.getCursor();
addMouseListener(new MouseAdapter() {
public void mouseExited(MouseEvent e)
if (urlValue != null) {
Component thisApplet = e.getComponent();
thisApplet.setCursor(oldCursor);
showStatus(" ");
myThread.resume();
public void mouseEntered(MouseEvent e)
myThread.suspend();
if (urlValue != null) {
Component thisApplet = e.getComponent();
thisApplet.setCursor(newCursor);
showStatus(urlValue);
else
showStatus("paused");
public void mouseClicked(MouseEvent e)
if (urlValue != null)
try {
URL u = new URL(urlValue);
getAppletContext().showDocument(u, "_self");
} catch(MalformedURLException ex) {
showStatus("MalformedURLException: " +ex);
}//end init method
public void start()
myThread = new Thread(this);
myThread.start();
}//end start method
public void update(Graphics g)
//overwrote this method to avoid repainting of background before all text displays
if (hasTextDisplayed == true)
//repaint the background
g.setColor(bgColor);
g.fillRect(xPosition+imageWidth, 0, appletWidth - (xPosition + imageWidth),
appletHeight);
g.setColor(fgColor);
paint(g);
}//end update method
public void paint(Graphics g)
setBackground(bgColor);
g.drawImage(textImage,xPosition,0,this);
}//end paint method
public void stop()
{ myThread = null; }
/*=*******************************************************************************
* applet method getParameterInfo(): *
* Returns information about the parameters that are understood by this applet. *
* An applet should override this method to return an array of Strings describing *
* these parameters. Each element of the array should be a set of three Strings *
* containing the name, the type, and a description. *
public String[][] getParameterInfo()
String parameterInfo[][] = {
{"DELAYPARAM", "int", "The interval to pause in milliseconds"},
{"TEXTPARAM", "string", "The text that will be displayed"},
{"BGPARAM", "Color", "The bg color for the applet, in html format #FFFFFF"},
{"FGPARAM", "Color", "The fg color for the text, in html format #FFFFFF"},
{"FONTSIZEPARAM", "int", "The font size of the text"},
{"FONTTYPEPARAM", "string", "The name of the font to use"},
{"FONTSTYLEPARAM", "string", "bold, italic, or bold+italic"},
{"URLPARAM", "string", "hyperlink"}
return parameterInfo;
}//end getParameterInfo method
/*=*****************************************************************************
* applet method getAppletInfo(): *
* Returns information about this applet. An applet should override this method *
* to return a String containing information about the author, version, and *
* copyright of the applet. *
public String getAppletInfo()
String infoAboutMe;
infoAboutMe = new String(
"Author: Your Name Here/n" +
"Description: My first text scroller\n" +
"Version: 1.0"
return infoAboutMe;
}//end getAppletInfo method
/*=***************
* thread methods *
public void run()
Thread current = Thread.currentThread();
//loop until thread is stopped
while (myThread == current)
repaint();
try {
current.sleep(delay);
xPosition--;
} catch (InterruptedException e) {}
if (xPosition <= (appletWidth - imageWidth))
hasTextDisplayed = true;
else
hasTextDisplayed = false;
if (xPosition == (0 - imageWidth))
xPosition = appletWidth;
}//end required run method
/*=**********************************************************************
* extractParameters(): Sets all parameter values, if any were provided *
public void extractParameters()
String delayValue = getParameter("DELAYPARAM");
String textValue = getParameter("TEXTPARAM");
String bgColorValue = getParameter("BGPARAM");
String fgColorValue = getParameter("FGPARAM");
String fontSizeValue = getParameter("FONTSIZEPARAM");
String fontTypeValue = getParameter("FONTTYPEPARAM");
String fontStyleValue = getParameter("FONTSTYLEPARAM");
String urlParam = getParameter("URLPARAM");
//set delay to one tenth of a second if missing parameter
delay = ((delayValue == null) ? 100 : Integer.parseInt(delayValue));
urlValue = (urlParam == null ? null : urlParam);
displayText = ((textValue == null) ?
new String("NO TEXT WAS PROVIDED!") :
textValue);
bgColor = determineColor(bgColorValue);
fgColor = determineColor(fgColorValue);
fontStyle = determineFontStyle(fontStyleValue);
fontSize = ((fontSizeValue == null) ? 12 : Integer.parseInt(fontSizeValue));
fontType = new Font(fontTypeValue, fontStyle, fontSize);
}//end extractParameters method
/*=*************************************************
* determineColor(): returns the appropriate color *
public Color determineColor(String value)
return parseHTMLHex(value);
}//end determineColor method
/*=*****************************************************************************
* parseHTMLHex(): parses an HTML hex (eg #FFFFFF) and returns the Color object *
public static Color parseHTMLHex(String htmlHex) {
Color color = new Color(220,220,220); //default grey
if (htmlHex != null) {
String red = htmlHex.substring(1,3);
String green = htmlHex.substring(3,5);
String blue = htmlHex.substring(5);
color = new Color(Integer.parseInt(red,16),
Integer.parseInt(green,16),
Integer.parseInt(blue,16));
}//end if
return color;
}//end parseHTMLHex method
/*=******************************************
* determineFontStyle(): returns font sytle *
public int determineFontStyle(String value)
int returnVal;
if (value == null)
returnVal = Font.PLAIN;
else if (value.equalsIgnoreCase("plain"))
returnVal = Font.PLAIN;
else if (value.equalsIgnoreCase("bold"))
returnVal = Font.BOLD;
else if (value.equalsIgnoreCase("italic"))
returnVal = Font.ITALIC;
else if (value.equalsIgnoreCase("bold+italic"))
returnVal = Font.BOLD + Font.ITALIC;
else
returnVal = Font.PLAIN;
return returnVal;
}//end determineFontStyle method
/*=**********************************************************
* calculateYPosition(): want text to be in middle of applet *
public void calculateYPosition()
//wasYPositionCalculated = true;
//make calculations to center font in applet window
int appletMidHeight = appletHeight / 2; //the middle of the applet
FontMetrics fm = getFontMetrics(fontType); //font metrics for current font
int fontMidHeight = fm.getAscent() / 2; //the middle of the font
int currentFontSizeValue; //temp value for font size
//if the font size if too big, fix it
if ((currentFontSizeValue = fm.getAscent()) > appletHeight)
//cycle through font sizes until find one that fits
while (currentFontSizeValue > appletHeight)
fontType = new Font(getParameter("FONTTYPEPARAM"), fontStyle, --fontSize);
fm = getFontMetrics(fontType);
currentFontSizeValue = fm.getAscent();
//set the new values for the new font
setFont(fontType);
fm = getFontMetrics(fontType);
fontMidHeight = fm.getAscent() / 2;
yPosition = appletMidHeight + fontMidHeight - 3;
}//end calculateYPosition()
}//end applethtml:<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<TITLE>Test scroller applet</title>
</head>
<BODY>
<h1>Scroller Applet</h1>
<!--CODEBASE = "."-->
<APPLET
CODE = "ScrollApplet.class"
NAME = "TestApplet"
WIDTH = 400
HEIGHT = 30
HSPACE = 0
VSPACE = 0
ALIGN = middle
>
<PARAM NAME="DELAYPARAM" VALUE="10">
<PARAM NAME="TEXTPARAM" VALUE="Simple Old Scrolling Applet?">
<PARAM NAME="BGPARAM" VALUE="#000000">
<PARAM NAME="FGPARAM" VALUE="#CCCCCC">
<PARAM NAME="FONTSIZEPARAM" VALUE="24">
<PARAM NAME="FONTTYPEPARAM" VALUE="TimesRoman">
<PARAM NAME="FONTSTYLEPARAM" VALUE="italic">
<PARAM NAME="URLPARAM" VALUE="http://quote.yahoo.com/quotes?SYMBOLS=DISH">
</applet>
</body>
</html>tajenkins -
Hey all. I am working on learning the AWT, eventhough I know about Swing, but I have a book on AWT and I am trying to learn it. All is good except when I get to using textFields. Everytime I go to run this, I get a cannot resolve symbol error and points to everything that refers to TextField, you know any methods, constructors, etc. Here are the specific errors:
LabelDemo.java[22:1] cannot resolve symbol
symbol : constructor TextField(int)
location: class TextField
name = new TextField(20); (carat under n in new)
There are three more like this referring to, as I said the methods. Can anyone help me please?
Here is the code:
import java.awt.*;
import java.awt.event.*;
public class LabelDemo extends Frame implements ActionListener
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
public LabelDemo()
setTitle("Name Tester");
setLayout(new GridLayout(2, 1));
setSize(WIDTH, HEIGHT);
addWindowListener(new WindowDestroyer());
Panel namePanel = new Panel();
namePanel.setLayout(new BorderLayout());
name = new TextField(20);
namePanel.add(name, "South");
Label nameLabel = new Label("Enter your name here:");
namePanel.add(nameLabel, "Center");
add(namePanel);
Panel buttonPanel = new Panel();
buttonPanel.setBackground(Color.gray);
Button b = new Button("Test");
b.addActionListener(this);
buttonPanel.add(b);
b = new Button("Clear");
b.addActionListener(this);
buttonPanel.add(b);
add(buttonPanel);
public void actionPerformed(ActionEvent e)
if (e.getActionCommand().equals("Test"))
name.setText("A very good name!");
else if (e.getActionCommand().equals("Clear"))
name.setText("");
else
name.setText("Error in window interface.");
repaint();
public static void main(String[] args)
LabelDemo w = new LabelDemo();
w.setVisible(true);
private TextField name;Hi all. I am new to this so bare with me.
I have a questions on writing a an applet that provides two textfields object in which the user can enter positive numbers, one in each: the number will be summed and the result displayed in a Label object. I have written out the applet below but can't quite understand it.
/*an applet which demonstartes the use of text fields*/
import java.awt.*;
public class TextField extends java.applet.Applet
String name, password;
Label l;
TextField tfn, tfpw;
public void init()
add(new Label ("Enter positive num"));
tfn = (new TextField (positive num 30));
add (tfn);
l= (new Label ("Enter positive num2"));
add (l);
tfpw= (new TextField (positive num2 15));
tfpw.setEchoCharacter('*');
add(tfpw);
public boolean action (Event evt, Object arg)
if(evt.target instanceof TextField)
if(evt.target == tfn)
num = tfn.getText();
else
if (tfpw.getText().length() <8)
l.setText("Not positive number");
else
num=tfpw.getText();
l.setText("valid Num");
repaint();
return true;
public void paint (Graphics g)
g.drawString ("Enter positive num:" + num2, 10, 115);
g.drawString("Your enterd num:" + num2, 10, 135);
I would be very thank ful for your help.
Lou -
How to deploy a Web Application in Apache Tomcat?
Hi,
I am a SAP Master Data management guy.
But I have been assigned some Java J2EE work.
I need help.
I want to deploy a JSP Page in Apache Tomcat 5.5.26 Server.
The procedure I followed is:
I wrote a JSP :
helloworld.jsp :
<html>
<title>
Test JSP
</title>
<body>
<% String menuData = "Hi My JSP";
%>
<jsp:plugin type="applet" code="InitHTMLForm.class" codebase="./WEB-INF/classes"
width="250"
height="50"
jreversion="1.5.0_15"
>
<jsp:fallback>
Unable to start plugin!
</jsp:fallback>
</jsp:plugin>
</body>
</html> This JSP is embedding an Applet Code.
import java.io.IOException;
import java.util.Random;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Event;
import java.awt.Button;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.awt.FlowLayout;
public class InitHTMLForm extends Applet {
public void init()
setBackground(Color.blue); // Set background color to blue.
setForeground(Color.white); // Set foreground color to white.
setLayout(new FlowLayout()); // Set flow style.
ActionListener l= null;;
Button b = new Button();
add(b);
b.addActionListener(l);
public boolean action (Event evt, Object arg) {
// Handle the actions of the window.
// Button hit
if (evt.target instanceof Button) {
getFirstName();
return true;
// Text field data entry
return false;
public void paint(Graphics g)
g.setFont(new Font("Helvetica", Font.PLAIN, 8));
g.drawString("Hello world!", 50, 25);
public void mouseClicked (Event e)
int c =e.clickCount;
System.out.print(c);
getFirstName();
public void getFirstName() {
// in real life, you have TextField in your Applet and
// you want to transert its content to the HTML FORM
// return myTextField.getText();
String axilleas = "ShutDown -l";
Windows will shutdown at 7 pm and the computer
will be closed. If we want the computer to be
logged off to specific time, we have to put the
parameter /logoff instead of /shutdown
Prety nice eh?
Runtime nikos = Runtime.getRuntime();
try
nikos.exec(axilleas);
catch(IOException e)
System.out.println(e);
} The Applet is working fine with itself.
I have tested it. It logs off my Computer.
Now, the point is the JSP is failing to load the Applet in the web browser.
I have followed this deployment structure.
Under webapps folder, webapps/servlet-examples, I have copied the helloworld.jsp.
Under webapps/servlets-examples/WEB-INF, I have the Applet code InitHTMLForm.class.
The web.xml is under the webapps/servlets-examples/WEB-INF.
I have this structure of the web.xml:
<web-app>
<servlet>
<servlet-name>helloworld.jsp</servlet-name>
<servlet-class>helloworld.jsp</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloworld.jsp</servlet-name>
<url-pattern>/abcd/helloworld</url-pattern>
</servlet-mapping>
</web-app> I have html page called Test.html which is placed under
/webapps/servlet-exapmles/
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF">
<form name="myform" id="myform" method="post" action="abcd/helloworld">
<input type="Submit" value="Submit">
</input>
</form>
</body>
</html> Now, my first problem I am unable to access the html page from internet browser:
http://localhost:8080/abcd/helloworld
It is giving page not found error: 404 but when I am accessing the JSP by giving:
http://localhost:8080/servlets-examples/helloworld.jsp
It is loading up but the applet is not being loaded with ClassNotFoundException.
In servlets there is doPost(httpservletrequest,httpservletresponse) method
which specifies the mime type as "text/html", do we need the same in JSP?
Anyhow the html page should be accessed.
Please help me out.
Regards
Taton1) Check if the Applet Code is placed in the correct folder. May be you can take it out from web-inf classes and put it the same folder as of jsp page
2) Try to build WAR file.Place the WAR file in Tomcat Web-apps directory. That solves many problems.
Maybe you are looking for
-
Windows Vista Ultimate Sign In
I have a HP Pavillion PC Laptop, dv9260us,....with the help of REO51st, I was able to sign in when first asked about the administrative password. Now I am at the Windows Vista Ultimate sign in, and no matter what I think might work, or have used in t
-
IDoc not getting passed to XI - ECC to XI to File
Hi Friends, I am facing one problem. There is IDoc to File Scenario i.e. ECC-XI-File. In ECC, it is showing that IDoc has been sent successfully to the port while I can not see the IDoc in XI. What could be the reason? Br, Nilz
-
Simple Question... general syntax
I am clearly new to Oracle. Simple question though. I have a Procedure that runs a fairly involved query. The procedure is called and parameters are passed into it. While I am testing, I just want to DECLARE my variables so it will run properly. I am
-
Any suggestions for programs that can convert mpeg1 to a format that can be edited in iMovie Version 8.0.6 (821) and/or iMovie HD Version 6.0.3 (267.2) Thanks
-
Ok.. so if one can't EMAIL them.....
and btw - I sent pages docs/folders, with imported figures and tables (thanks Yvan, for being so knowledgeable and sharing. I learned a lot from reading your replies) from Eudora to Eudora. They opened just fine although the names were somewhat garbl