Sat Feb 09, 2013 4:06 pm
import java.applet.Applet;
import java.awt.*;
import java.awt.event.FocusListener;
import java.awt.event.FocusEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
public class FocusEventExample extends Applet
implements FocusListener,
ActionListener {
TextArea textAreaObj;
FocusFrame frameObj;
Button btnOne, btnTwo;
static final String SHOW = "show";
static final String CLEAR = "clear";
String lineSep;
public void init() {
btnOne = new Button("Click to show frame");
btnOne.setActionCommand(SHOW);
btnOne.addActionListener(this);
btnTwo = new Button("Clear");
btnTwo.setActionCommand(CLEAR);
btnTwo.addActionListener(this);
textAreaObj = new TextArea(5, 20);
textAreaObj.setEditable(false);
setLayout(new BorderLayout());
add("North", btnOne);
add("Center", textAreaObj);
add("South", btnTwo);
//Create but don't show window.
frameObj = new FocusFrame(this);
lineSep = System.getProperty("line.separator");
}
public void stop() {
frameObj.setVisible(false);
}
public void focusGained(FocusEvent e) {
showStatus("Focus On ", e);
}
public void focusLost(FocusEvent e) {
showStatus("Focus Off ", e);
}
void showStatus(String prefix, FocusEvent e) {
textAreaObj.append(prefix
+ ": "
+ e.getComponent()
+ lineSep);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == SHOW) {
frameObj.pack();
frameObj.setVisible(true);
} else { //CLEAR
textAreaObj.setText("");
}
}
}
class FocusFrame extends Frame {
class FocusFrameListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
setVisible(false);
}
}
public FocusFrame(FocusListener listener) {
super("Focus Event Frame");
this.addFocusListener(listener);
this.addWindowListener(new FocusFrameListener());
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints gridBagConstObj = new GridBagConstraints();
setLayout(gridbag);
gridBagConstObj.fill = GridBagConstraints.HORIZONTAL;
gridBagConstObj.weightx = 1.0;
TextField textField = new TextField("A TextField");
textField.addFocusListener(listener);
gridbag.setConstraints(textField, gridBagConstObj);
add(textField);
gridBagConstObj.weightx = 0.1;
gridBagConstObj.fill = GridBagConstraints.NONE;
Label labelObj = new Label("Label:");
labelObj.addFocusListener(listener);
gridbag.setConstraints(labelObj, gridBagConstObj);
add(labelObj);
Choice choiceObj = new Choice();
String choiceprefix = "Choice item #";
for (int i = 0; i < 10; i++) {
choiceObj.addItem(choiceprefix + i);
}
choiceObj.addFocusListener(listener);
gridbag.setConstraints(choiceObj, gridBagConstObj);
add(choiceObj);
gridBagConstObj.gridwidth = GridBagConstraints.REMAINDER;
Button btnObj = new Button("Button: ");
btnObj.addFocusListener(listener);
gridbag.setConstraints(btnObj, gridBagConstObj);
add(btnObj);
gridBagConstObj.weighty = 1.0;
gridBagConstObj.weightx = 0.0;
gridBagConstObj.fill = GridBagConstraints.BOTH;
List list = new List();
String listprefix = "List item #";
for (int i = 0; i < 10; i++) {
list.addItem(listprefix + i);
}
list.addFocusListener(listener);
gridbag.setConstraints(list, gridBagConstObj);
add(list);
}
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.