/** * @file JNameDialog.java * @author Robert S. Laramee * @version 2.0 * @see SolidObject.java, NameFrame.java * start date Sunday 29 Nov 98 * "finish date" Thursday 3 Dec 98 *

* description This is the class that pops up the dialog box when the user * creates a new solid object. It is used only to obtain the * name of the new solid object.` * modified on Thursday 10 Dec 1998 * Dialog --> JDialog * Panel --> JPanel * Label --> JLabel * TextField --> JTextField */ import java.awt.*; import java.awt.event.*; import javax.swing.*; class JNameDialog extends JDialog { private SolidObject solidObject; public JNameDialog(JFrame frame, SolidObject so) { super(frame, "New Name Dialog", true); solidObject = so; // Put in a text field at the bottom for entering new Shape names JPanel dialogPanel = new JPanel(); JLabel namePrompt = new JLabel("Please enter a name for the object:"); JTextField shapeName = new JTextField ( "", 15 ); shapeName.addActionListener (new JTextFieldAL (shapeName)); dialogPanel.add(namePrompt); dialogPanel.add(shapeName); getContentPane().add (dialogPanel, "Center"); setSize(400, 85); // width, height /** * bob tries to place the window in a good location */ Toolkit toolkit = Toolkit.getDefaultToolkit(); Dimension dimension = toolkit.getScreenSize(); int screenHeight = dimension.height; int screenWidth = dimension.width; setLocation(screenWidth/2, screenHeight/4); } /** * TextFieldAL is an ActionListener that sets the name */ public class JTextFieldAL implements ActionListener { private JTextField field; public JTextFieldAL ( JTextField which ) { field = which; } public void actionPerformed (ActionEvent event) { String newName = field.getText().trim(); /** * we're only going to save names that are 1 word long */ if (newName.indexOf((int)' ') != -1) { newName = newName.substring(0, newName.indexOf((int)' ')); } solidObject.setName(newName); dispose(); } } }