Use JBuilder to Create a Dialog

Introduction

You will construct a dialog box with many of the most important types of interface components. Before starting you need to know that in Java dialogs, you are responsible for returning the values that the user enters. This means that you must store the current information for each component.

In the case of the ok and cancel buttons, since there are only two exits possible, so a boolean variable, result, works well to represent which button has been pressed. You need to provide a function, getResult, (again a standard name for this function) that returns the boolean value. When you have a list or combo box, you should have one array of strings with the values to be shown in the list or combo box, an array representing values returned for the choice. If you need to return a string make an array of strings. On the other hand if you needed a constant type like font style, make your second array an array of integers (in this case). If you have a text box, you should store your value in a string. If you want the user to enter an integer, it is easier to filter bad entries with a string holding the value than an integer. Here is code for such a box, with an integer holding the value. It almost works, except if you delete after entering a number, then enter a non-number, the original number comes back to haunt you! The version below with a string holding the value works the right way.

  void  fontSize_keyReleased(KeyEvent e) {
    if ((e.getKeyChar() <  '0' || e.getKeyChar() > '9') && e.getKeyCode()!=8)
      fontSize.setText(fontSizeVar);
    else 
      fontSizeVar = fontSize.getText();

Step-By-Step

From the file menu, select new. Then from the New window Dialog tab, select Standard Dialog 1
The Code Snippet dialog contains code that will be inserted into your project to create the new dialog. Make sure to name your dialog.
You will now see your dialog listed in the in the structure pane and the naviation pane.
Select the design tab and start adding the components listed here. Notice that the layout is GridBag so you can size items to fit your needs. You will use panels for each of your interface components and these panels may have different layout managers. First notice your dialog comes with two panels. The ok and cancel buttons are in jPanel1. You will design your components in panel2 which is above jPanel1. Since this dialog has only two possible exit buttons, you may store the result (fairly standard name for the value of the exit button) as a boolean. This value should be set in the actionPerformed handler before dispose is called. By the way, the comments are provided by the compiler when you build your dialog, and provide a quick way to find these handlers.
 // OK
    void  button1_actionPerformed(ActionEvent e) {
    result = true ;
    dispose();
  }

  // Cancel
    void  button2_actionPerformed(ActionEvent e) {
    result = false ;
    dispose();
  }

Change the layout of panel2 to Border. Then drop in a label (found in AWT tab of the component pallet) with constraint NORTH. Change its background to WindowBorder and forground to ActiveCaption. Next add a JPanel, jPanel2 with constraint CENTER and layout Grid with 3 rows and 3 columns. Place 6 JPanels in jPanel2. Change the borders of the panels to Etched.

Add three radio buttons to the top left panel. Set its layout to be grid with 3 rows and one column.Change the labels to align left, right and center. Set the align left button's selected property to true. To make sure the buttons work together you have to add a ButtonGroup to the dialog class

ButtonGroup radioGroup = new ButtonGroup();

Also add the following code at the end of the JInit function.

radioGroup.add(leftBtn); radioGroup.add(centerBtn); radioGroup.add(rightBtn);

Add a JList (called fontList to the upper-right panel. Set the selectedIndex to 0, selectionMode to SINGLE_SELECTION and visibleRowCount to 3. Find the line
JList fontList = new JList(fontNames);
And insert the two array definitions and string variable definition (seen here at the right) and add the parameter fontNames to the constructor call (as shown at the right).
  String fontNames[]={"Courier" , "Times" , "Helvetica" };
  String fontName = "Courier" ;//font selected from dialog 
  String fontType[]={"Monospaced" ,"Serif" ,"SansSerif" };
  JList fontList = new JList(fontNames);
In the center-left panel you will insert a JTextField (fontSize) and a label "Size." Use a flow layout with settings shown at the far right of this screen capture. You can play with the vgap to move the items up and down. The hgap sets the distance between the label and the textfield. Finally, alignment sets the position in the panel of both items.
Add the code for keyReleased handler. Notice this will not allow non integers in the fontSize JTextField
  void  fontSize_keyReleased(KeyEvent e) {
    if ((e.getKeyChar()< '0' || e.getKeyChar()>'9' ) && e.getKeyCode()!=8)
      fontSize.setText(fontSizeVar);
    else 
      fontSizeVar = fontSize.getText();

  }
At this point, you discover you don't need the last row. To get rid of it first delete the two panels you placed there, then click on the layout manager for and set the rows to two.
Set the layout manager for the last panel to grid with 3 rows of 1 column and vgap of 3. Add the check boxes for plain, bold and italic types. Use check boxes since bold and italic can be checked at the same time, and checkboxs are used when more than one item can be checked. Radio buttons are used for mutually exclusive events. The final version of the dialog is shown here at the right.

Font style is an integer with PLAIN equal to zero. The action handlers for the check boxes will set a int value, fontStyle, for the font style. The number representing the combination of bold and italic is just the sum of the two. Notice in the code for this section, when the bold or italic are clicked, the number type number is added if the checkbox is checked, then the code is add otherwise it is subtracted. In all cases, if as a result of the arithmetic, fontStyle is Font.PLAIN, then the type is plain and the plain box is checked (otherwise it is unchecked).

  void  plainCheckBox_actionPerformed(ActionEvent e) {
    fontStyle = Font.PLAIN;
    boldCheckBox.setSelected(false);
    italicCheckBox.setSelected(false);
  }

  void  boldCheckBox_actionPerformed(ActionEvent e) {
    if (boldCheckBox.isSelected())
      fontStyle += Font.BOLD;
    else
       fontStyle -= Font.BOLD;
    plainCheckBox.setSelected(fontStyle == Font.PLAIN);
  }

  void  italicCheckBox_actionPerformed(ActionEvent e) {
    if  (italicCheckBox.isSelected())
      fontStyle += Font.ITALIC;
    else 
      fontStyle -=Font.ITALIC;
    plainCheckBox.setSelected(fontStyle == Font.PLAIN);
  }
  public int  getFontStyle(){
    return  fontStyle;
  }

Complete Code

Click here for the complete code.