Java Reference
In-Depth Information
If the user selects the menu item insert, the actionPerformed() method of the registered handler
is called, where the generated ActionEvent is handled. In the actionPerformed() method, a
dialog will be shown enabling the user to add text into two columns labeled Amount and Item. The
following code snippet is responsible for creating an instance of our InsertItemDialog , showing
the dialog and adding the results to the main panel:
public void actionPerformed(ActionEvent ae) {
InsertItemDialog dialog = new InsertItemDialog(frame);
dialog.show();
textPanel.add(new Label (dialog.getAmount()));
textPanel.add(new Label (dialog.getItem()));
frame.validate();
}
Because the dialog is modal, it will block the event handler of the frame until the dialog is dismissed
using the OK button. In this case, the event handler switches back to the frame. To make sure that the
new content of the grid layout is arranged properly, we call the invalidate() method of the frame.
Listing 4.5 ShoppingChart.java
import java.awt.*;
import java.awt.event.*;
import javax.microedition.midlet.*;
public class ShoppingChart extends MIDlet implements ActionListener {
class InsertItemDialog extends Dialog implements ActionListener {
TextField amount = new TextField();
TextField item = new TextField();
public InsertItemDialog (Frame owner) {
super (owner, "Insert Item", true);
Panel panel = new Panel (new GridLayout (2, 0));
panel.add(new Label ("Amount"));
panel.add(amount);
panel.add(new Label ("Item"));
panel.add(item);
add(panel, BorderLayout.CENTER);
Panel buttonPanel = new Panel (new FlowLayout());
Button b = new Button ("ok");
b.addActionListener(this);
buttonPanel.add(b);
add(buttonPanel, BorderLayout.SOUTH);
pack();
}
public String getAmount() {
return amount.getText();
}
public String getItem() {
return item.getText();
}
public void actionPerformed (ActionEvent ae) {
setVisible (false);
 
Search WWH ::




Custom Search