# ListEvents.java Uses the following classes:
* CloseableFrame.java
* SelectionReporter.java
* ActionReporter.java
/././././././././././././
import java.awt.event.*;
/././././././
public class ListEvents2 extends ListEvents {
public static void main(String[] args) {
new ListEvents2();
}
/** Extends ListEvents with the twist that
* typing any of the letters of "JAVA" or "java"
* over the language list will result in "Java"
* being selected
*/
public ListEvents2() {
super();
// Create a KeyAdapter and attach it to languageList.
// Since this is an inner class, it has access
// to nonpublic data (such as the ListEvent's
// protected showJava method).
KeyAdapter javaChooser = new KeyAdapter() {
public void keyPressed(KeyEvent event) {
int key = event.getKeyChar();
if ("JAVAjava".indexOf(key) != -1) {
showJava();
}
}
};
languageList.addKeyListener(javaChooser);
}
}
***************************
import java.awt.*;
import java.awt.event.*;
/** A class to demonstrate list selection/deselection
* and action events.
*
/*******************/.>
public class ListEvents extends CloseableFrame {
public static void main(String[] args) {
new ListEvents();
}
protected List languageList;
private TextField selectionField, actionField;
private String selection = "[NONE]", action;
/** Build a Frame with list of language choices
* and two textfields to show the last selected
* and last activated items from this list.
*/
public ListEvents() {
super("List Events");
setFont(new Font("Serif", Font.BOLD, 16));
add(makeLanguagePanel(), BorderLayout.WEST);
add(makeReportPanel(), BorderLayout.CENTER);
pack();
setVisible(true);
}
// Create Panel containing List with language choices.
// Constructor puts this at left side of Frame.
private Panel makeLanguagePanel() {
Panel languagePanel = new Panel();
languagePanel.setLayout(new BorderLayout());
languagePanel.add(new Label("Choose Language"),
BorderLayout.NORTH);
languageList = new List(3);
String[] languages =
{ "Ada", "C", "C++", "Common Lisp", "Eiffel",
"Forth", "Fortran", "Java", "Pascal",
"Perl", "Scheme", "Smalltalk" };
for(int i=0; i<languages .length; i++) {
languageList.add(languages[i]);
}
showJava();
languagePanel.add("Center", languageList);
return(languagePanel);
}
// Creates Panel with two labels and two textfields.
// The first will show the last selection in List; the
// second, the last item activated. The constructor puts
// this Panel at the right of Frame.
private Panel makeReportPanel() {
Panel reportPanel = new Panel();
reportPanel.setLayout(new GridLayout(4, 1));
reportPanel.add(new Label("Last Selection:"));
selectionField = new TextField();
SelectionReporter selectionReporter =
new SelectionReporter(selectionField);
languageList.addItemListener(selectionReporter);
reportPanel.add(selectionField);
reportPanel.add(new Label("Last Action:"));
actionField = new TextField();
ActionReporter actionReporter =
new ActionReporter(actionField);
languageList.addActionListener(actionReporter);
reportPanel.add(actionField);
return(reportPanel);
}
/** Select and show "Java". */
protected void showJava() {
languageList.select(7);
languageList.makeVisible(7);
}
}
*********************
SelectionReporter.java
*********************
import java.awt.*;
import java.awt.event.*;
/** Whenever an item is selected, it is displayed
* in the textfield that was supplied to the
* SelectionReporter constructor.
*
*******************************
public class SelectionReporter implements ItemListener {
private TextField selectionField;
public SelectionReporter(TextField selectionField) {
this.selectionField = selectionField;
}
public void itemStateChanged(ItemEvent event) {
if (event.getStateChange() == event.SELECTED) {
List source = (List)event.getSource();
selectionField.setText(source.getSelectedItem());
} else
selectionField.setText("");
}
}
*********************
ActionReporter.java
*******************
import java.awt.*;
import java.awt.event.*;
/** Whenever an item is activated, it is displayed
* in the textfield that was supplied to the
* ActionReporter constructor.
*
*************************
public class ActionReporter implements ActionListener {
private TextField actionField;
public ActionReporter(TextField actionField) {
this.actionField = actionField;
}
public void actionPerformed(ActionEvent event) {
List source = (List)event.getSource();
actionField.setText(source.getSelectedItem());
}
}
Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10340
Categories:Programming Code Examples, Java/J2EE/J2ME, Advanced Swing
Tags:Java/J2EE/J2MEAdvanced Swing
Post Data:2017-01-02 16:04:35
Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada
