Total members 11889 |It is currently Thu Mar 28, 2024 1:58 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Handle Focus events with FocusListener on frame
java code
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);
}
}




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Handle Focus events with FocusListener
 How can a GUI component handle its own events     -  
 removing focus border from a HTML element     -  
 check keyboard events using C++     -  
 Show hint when focus on html element using JQuery     -  
 Window events-handling by WindowListener     -  
 Text Listener for events TextListener-on TextArea-TextField     -  
 What are exceptions and how to handle them!!!     -  
 Handle IsPostBack Condition     -  
 Handle Cookies using JQuery     -  
 handle integer overflows and underflows     -  



Topic Tags

Java Applet, Java AWT






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
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