The following code creates a new class - textFieldNew - which is an extension of the TextField class. The new class has some protected variables and a public method isModified() to check if the field has been modified. An event id of 1001 is used to trigger the check for the "modified" event. This event id is for the "Enter" key pressed in the text filed (I think). But this code will give you an idead on how to create an extension to the TextField class to handle specific problems.The code is for a standalone application that lets you check if the field has been modified or not. It can be used in any applet and the logic can also be extended to apply to any TextComponent (TextFiled or TextArea).
import java.awt.*; import java.applet.*; class textFieldNew extends TextField { private String oldValue = new String(); private String newValue = new String(); private boolean isMod = false; public textFieldNew() { super(20); } public boolean isModified() { handleEvent(new Event(this, 1001, this)); return isMod; } public boolean action(Event e, Object o) { if (e.id == 1001) { newValue = this.getText(); if (oldValue.compareTo(newValue) != 0) isMod = true; else isMod = false; oldValue = newValue; } return true; } } public class easyText extends Applet { Label l1 = null; textFieldNew tn1 = new textFieldNew(); TextArea ta1 = null; public void init() { } public easyText() { setLayout(new BorderLayout()); Panel p1 = new Panel(); p1.setLayout(new FlowLayout()); add("North", p1); p1.add(new Button("CheckField")); p1.add(tn1); ta1 = new TextArea(10, 40); p1.add(ta1); } public boolean action(Event e, Object o) { if ("CheckField".equals(o)) { if (tn1.isModified()) ta1.appendText("\n" + "Text Field modified"); else ta1.appendText("\n" + "Text Field Not modified"); } return false; } public static void main(String args[]) { Frame f1 = new Frame("Standalone Application"); easyText s1 = new easyText(); s1.init(); s1.start(); f1.add("Center", s1); f1.resize(300, 300); f1.show(); } }
(Submitted by M. K. Kwong)public String alignRight(String s, Font font, int width) { FontMetrics fontmetrics = getFontMetrics(font); char c; // Strip blanks at the right // Could also expand to strip \t \n or whatever by changing the // test at the start of the while loop c = s.charAt(s.length()-1); while (c == ' ') { s = s.substring(1, s.length()-1); c = s.charAt(s.length()-1); } // The 5 in the next line really should be a class variable so you // can easily change it. This is left as an exercise to the reader. while (fontmetrics.stringWidth(s) < width-5) s = " " + s; return s; }
It belongs to the more general question of how to center or right adjust a string, since an integer n can be "cast" first as a string, using something like: "" + n. The crucial step is to find out the width of the string, using FontMetrics. What follows are two useful methods that I have used a lot in my own codes:
// method to right adjust a string tx at (x,y) public void rString(Graphics g, String tx, int x, int y) { int tw = g.getFontMetrics().stringWidth(tx); g.drawString(tx,x-tw,y); } // method to center a string tx in a field starting at (x,y) // having width w public void cString(Graphics g, String tx, int x, int w, int y) { int tw = g.getFontMetrics().stringWidth(tx); g.drawString(tx,x+(w-tw)/2,y); }For instance, to print a right adjusted interger n with the lower right hand corner at the coordinates (x,y), use
rString(g,"" + n, x, y);inside paint(Graphics, g).
Use,textField.select(pos, pos);where "pos" is the character position at which you wish to place the cursor.Then after all components have been layed out (e.g. after Applet.start() has been called),
textField.requestFocus();