Total members 11890 |It is currently Thu Apr 25, 2024 7:46 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





ComponentListener with Text field and Frame example. event handling
java code
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class ComponentEventExample extends Applet
implements ComponentListener,
ActionListener {
TextArea textAreaObj;
Frame frameObj;
public boolean showIt = false;
final static String showMessage = "show";
final static String emptyMessage = "empty";
String newline;

public void init() {
lineSeparator = System.getProperty("line.separator");
setLayout(new BorderLayout());

textAreaObj = new TextArea(5, 25);
textAreaObj.setEditable(false);
add("Center", textAreaObj);

Button btnObj1 = new Button("Start");
btnObj1.setActionCommand(showMessage);
btnObj1.addActionListener(this);
add("North", btnObj1);

Button btnObj2 = new Button("Clear");
btnObj2.setActionCommand(emptyMessage);
btnObj2.addActionListener(this);
add("South", btnObj2);

frameObj = new Frame("My Frame");
comPanel comPanelObj = new comPanel(this);
frameObj.addComponentListener(this);
comPanelObj.addComponentListener(this);
frameObj.add("Center", comPanelObj);
frameObj.pack();

frameObj.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
showIt = false;
frameObj.setVisible(false);
}
});
}
//
public void actionPerformed(ActionEvent e) {
// condition to do action
// print the content of getActionCommand() if you want.
if (e.getActionCommand() == showMessage) {
showIt = true;
frameObj.setVisible(true);
} else { // clear textArea content.
textAreaObj.setText("");
}
}

public void stop() {
frameObj.setVisible(false);
}

public void start() {
if (showIt) {
frameObj.setVisible(true);
}
}

protected void displayMessage(String message) {
try {
textAreaObj.append(message + lineSeparator);
} catch (Exception event) {}
}

public void componentHidden(ComponentEvent e) {
displayMessage("componentHidden event from "
+ e.getComponent().getClass().getName());
}

public void componentMoved(ComponentEvent e) {
displayMessage("componentMoved event from "
+ e.getComponent().getClass().getName());
}

public void componentResized(ComponentEvent e) {
displayMessage("componentResized event from "
+ e.getComponent().getClass().getName());
}

public void componentShown(ComponentEvent e) {
displayMessage("componentShown event from "
+ e.getComponent().getClass().getName());
}
}

class comPanel extends Panel
implements ItemListener {
Label labelObj;
Checkbox checkBoxObj;

comPanel(ComponentEventExample listener) {
setLayout(new BorderLayout());

labelObj = new Label("Label", Label.CENTER);
add("Center", labelObj);

checkBoxObj = new Checkbox("Label visible", true);
checkBoxObj.addItemListener(this);
add("South", checkBoxObj);

labelObj.addComponentListener(listener);
checkBoxObj.addComponentListener(listener);
}

public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
labelObj.setVisible(true);
} else {
labelObj.setVisible(false);
}
}
}




_________________
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 : ComponentListener event handling
 What is Event Handling?     -  
 Event Handling Notes     -  
 Container Event handling with ContainerListener     -  
 explain event handling in java     -  
 java.util classes and interfaces support event handling     -  
 advantage of the event delegation model over event-inherit     -  
 relationship an event-listener interface & event handler     -  
 AWT event hierarchy     -  
 Thread of Event Dispatcher     -  
 event results from the clicking of a button     -  



Topic Tags

Java AWT, Java Applet
cron





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