Switch to full style
Graphics and animation Java code examples
Post a reply

ComponentListener event handling

Sat Feb 09, 2013 2:27 pm

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);
}
}
}




Post a reply
  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 Applet, Java AWT