// ShapeInputTextDialog.java
// Dialog box which prompts the user for
// data for a ShapeOutline object. The
// showDialog method returns a string
// representation which can then be
// converted to a ShapeOutline.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class ShapeInputTextDialog extends JDialog
implements ActionListener
{
// Displayed regions of the dialog box:
private JTextField widthField;
private JTextField heightField;
private JTextField spokesField;
private JButton okButton;
private JButton cancelButton;
private static String shapeData = null;
// Group of radio buttons to select shape:
ButtonGroup shapeSelector;
public ShapeInputTextDialog(JFrame owner)
{
super(owner,
/* modal= */ true);
// Active components to display on window:
JRadioButton rbCircle = new JRadioButton("Circle");
rbCircle.getModel().setActionCommand("Circle");
JRadioButton rbWheel = new JRadioButton("Wheel");
rbWheel.getModel().setActionCommand("Wheel");
JRadioButton rbRightTriangle = new JRadioButton("RightTriangle");
rbRightTriangle.getModel().setActionCommand("RightTriangle");
widthField = new JTextField();
heightField = new JTextField();
spokesField = new JTextField();
okButton = new JButton(" OK ");
okButton.setBorder(
BorderFactory.createBevelBorder(
BevelBorder.RAISED));
cancelButton = new JButton(" Cancel ");
cancelButton.setBorder(
BorderFactory.createBevelBorder(
BevelBorder.RAISED));
// Arrange components on window:
Container contentPane = getContentPane();
JPanel southPanel = new JPanel();
contentPane.add(southPanel, BorderLayout.SOUTH);
southPanel.setLayout(new GridLayout(1, 4));
southPanel.add(new JPanel());
southPanel.add(okButton);
southPanel.add(cancelButton);
southPanel.add(new JPanel());
JPanel westPanel = new JPanel();
contentPane.add(westPanel, BorderLayout.WEST);
westPanel.setBorder(
BorderFactory.createLineBorder(
Color.black));
westPanel.setLayout(new GridLayout(4, 1));
westPanel.add(new Label(" Select shape below: "));
westPanel.add(rbCircle);
westPanel.add(rbWheel);
westPanel.add(rbRightTriangle);
JPanel centerPanel = new JPanel();
contentPane.add(centerPanel, BorderLayout.CENTER);
centerPanel.setBorder(
BorderFactory.createLineBorder(
Color.black));
centerPanel.setLayout(new BorderLayout());
JPanel labelPanel = new JPanel();
centerPanel.add(labelPanel, BorderLayout.WEST);
labelPanel.setLayout(new GridLayout(3, 1));
labelPanel.add(new Label(" Enter width:"));
labelPanel.add(new Label(" spokes, if needed:"));
labelPanel.add(new Label(" height, if needed:"));
JPanel textEntryPanel = new JPanel();
centerPanel.add(textEntryPanel, BorderLayout.CENTER);
textEntryPanel.setLayout(new GridLayout(3, 1));
textEntryPanel.add(widthField);
textEntryPanel.add(spokesField);
textEntryPanel.add(heightField);
// Group radio buttons so that only one can be checked at a time:
shapeSelector = new ButtonGroup();
shapeSelector.add(rbCircle);
shapeSelector.add(rbWheel);
shapeSelector.add(rbRightTriangle);
// Enable this dialog box to respond to "OK" and "Cancel" buttons:
okButton.addActionListener(this);
cancelButton.addActionListener(this);
// Routine necessities with JDialog windows:
setSize(500, 150);
setLocation(200, 470);
setTitle("Shape data input");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setVisible(true);
} // constructor
public void actionPerformed(ActionEvent e)
{
if ( e.getSource() == okButton )
{
shapeData = getData();
if ( shapeData != null )
dispose();
}
else if ( e.getSource() == cancelButton )
dispose();
else
throw new IllegalStateException(
"This dialog box listens only to "
+ "its own OK and Cancel buttons.");
} // method actionPerformed
public static String showDialog(JFrame owner)
{
new ShapeInputTextDialog(owner);
// Return current value of static variable shapeData,
// resetting its value to null for the next time:
String toReturn = shapeData;
shapeData = null;
return toReturn;
} // method showDialog
private String getData()
{
if ( shapeSelector.getSelection() == null )
{
JOptionPane.showMessageDialog(this,
"You must select a shape.",
"Shape not selected!",
JOptionPane.ERROR_MESSAGE);
return null;
} // if
String text = "";
text += shapeSelector.getSelection().getActionCommand();
if ( widthField.getText().equals("") )
{
JOptionPane.showMessageDialog(this,
"You must enter a width.",
"Width not entered!",
JOptionPane.ERROR_MESSAGE);
return null;
} // if
text += " width=" + widthField.getText();
if ( ! heightField.getText().equals("") )
text += " height=" + heightField.getText();
if ( ! spokesField.getText().equals("") )
text += " spokes=" + spokesField.getText();
return text;
} // method getData
// For test purposes only:
public static void main(String[] args)
{
System.out.println(showDialog(null));
System.exit(0);
} // method main
} // class ShapeInputTextDialog