![]() TEA APIs |
In this section we'll see how to use the tea.awt.dialog.BoxDialog class to obtain easy to use Dialogs. Objects derived from BoxDialog provide persistent properties (size and location), international languages support and a simple action management. The code exposed is taken from the Source Codes of the TETRACTYS Enhanced Apis and is extensively used by the TEA Editor.
One is the class:
This is an abstract class that performs some common tasks of Dialogs. It defines support for international languages, persistent properties and action delivering events.
There are many issues about BoxDialog
Put this code in the constructor of the Dialog or Frame. It creates an Inner Class that listens to the Window Closing Event and calls a developer defined close() method.
WindowAdapter waClosing = new WindowAdapter() { public void windowClosing(WindowEvent e) { close(); } } ; addWindowListener(waClosing);
Usually Dialogs act in different ways when particular keys are pressed. The most important key is the Enter Key. The user expects that the Dialog accepts the inserted data when the Enter Key is pressed on the OK Button or even in a TextField and exits when the Enter Key is pressed on the CANCEL Button.
This task is realized for Buttons with the following code:
protected KeyAdapter keyAdapter = new KeyAdapter() { public void keyPressed(KeyEvent ke) { if(ke.getKeyCode() == KeyEvent.VK_ENTER) { if( ke.getSource() instanceof Button && !((Button)ke.getSource()).getActionCommand().equals("Cancel") ) { reply(); } else { close(); } } } };
reply() and close() are described below. A class derived from BoxDialog can use the keyAdapter in this way:
okBtn = new Button("Ok"); okBtn.setActionCommand("OK"); okBtn.addKeyListener(keyAdapter);
The same task is realized for TextFields in a different manner. First of all BoxDialog is an ActionListener and has the following actionPerformed() method:
public void actionPerformed(ActionEvent ae) { if( (!ae.getActionCommand().equals("Cancel")) || ae.getSource() instanceof TextField ) { reply(); } else { close(); } }
Then TextFields are declared in the Dialog constructor in this way:
textField = new TextField(defStr); textField.addActionListener(this);
When the TextField has the focus and the Enter Key is pressed, an ActionEvent is dispatched to the BoxDialog that acts consequentely.
When the user hits the OK Button, the Dialog notifies its ActionListeners that the data is available. The method is reply().
public void reply() { afterReply(); if(actionListener != null) { String cmd = actionCommand; if(cmd == null) cmd = name; actionListener.actionPerformed( new ActionEvent( this,ActionEvent.ACTION_PERFORMED,cmd ) ); } } public void afterReply() { close(); }
When the ActionListener receives the ActionEvent, it can retrieve the data using getSource() and the methods declared by the developer for the particular Dialog. For example, if you have a Find Dialog, the method could be String getFindText() and you get it with getSource().getFindText().
When the Dialog is closed, it must store its location (to restore it later). propertiesOwner is a PropertiesOwner declared in BoxDialog (see Persistent Properties Support)
protected void close() { setPropLocation(new Point(getLocation().x,getLocation().y)); setVisible(false); } protected void setPropLocation(Point loc) { propertiesOwner.set("dialog."+name+".location.x",String.valueOf(loc.x)); propertiesOwner.set("dialog."+name+".location.y",String.valueOf(loc.y)); }
The code and concepts for BoxDialog are quite complex. You can find the complete source code in the TEA APIs page. Write directly to TETRACTYS Freeware for any question or suggestion.
![]() |
In the next Issue Java Tutorials will be | ![]() |
---|