When you use a Java Dialogbox, you must remember the following important points:
Because the dialog box will exist until you exit the scope of its variable, even after the dialog disappears you may retrieve values that it accumulated. You should have noticed when you created the dialog box, you stored values in variables. These variables are used to return values to the program calling the dialog. You must still add functions that return the proper values for your purpose in the calling method.
You will create a font with these values, so before completing these functions check the types of the constructor for Font. The first parameter of the Font constructor is a string which is "Monospace", "Serif", or "SansSerif". You insured that the user would choose one of three choices with names that are commonly associated with fonts. These choices correspond closely to the constructors values, but are different strings. You need to add a second array of strings that corresponds to those expected by the Font constructor. Below you see that the actual current value is the index selected in the combo box and this is used as the subscript of the array item returned in the next section.
String fontNames[]={"Courier" ,"Times" ,"Helvetica" };
String fontName = "Courier" ;//used to hold the name of the font selected from dialog
String fontType[]={"Monospaced" ,"Serif" ,"SansSerif" };
JList fontList = new JList(fontNames);
The next two parameters of the Font constructor are size and style. Both are integers, but style must be one of Font.PLAIN, Font.BOLD, Font.ITALIC or Font.BOLD + Font.ITALIC. This suggests the best variable should be an integer assigned directly as a result of checkbox clicks. The font size is more difficult. It can be any positive integer. The best way to handle this is with a text box. As you have already seen, the value is stored as a string until it is returned, to allow for easier filtering of the user input.
Add the following functions to retrieve the values needed from your dialog. These functions are called accessors.
// functions that return input values
public boolean getResult() {
return result;
}
public int getAlignment(){
return alignment;
}
public String getFontName(){
return fontName;
}
public int getFontSize(){
return Integer.parseInt(fontSizeVar);
}
This dialog is called from a menu handler. Dialogs may also be called from a button handler. There are four distinct parts to the handler. First create and display the dialog. Define a variable of the dialog type and create a new dialog. Note the last parameter is the boolean true. This makes the dialog into a modal dialog, which means the user can't do anything in your program until the ok or cancel button are pressed. Display the dialog by a call to the setVisible function. At this point control transfers to the dialog. The rest of the handler will not get executed until the user presses ok or cancel.
The first thing you need to know after the user exits from the dialog is whether the ok or cancel button was used. Since there are only two choices here, the result should be boolean with ok the desired or true result. If ok was used, Create the new font and set the font in the main window to that font. Here you can see the use of the accessors you wrote above. Finally always repaint the window. The menu will leave garbage in the toolbar otherwise.
void jMenuItem1_actionPerformed(ActionEvent e) {
// Create and Display the dialog
MyDialog dialog = new MyDialog(this,"Font Information" ,true);
dialog.setVisible(true );
// Which Button was clicked?
boolean ok = dialog.getResult();
// if ok, then process the data - in this case change the font
if (ok) {
// JOptionPane.showMessageDialog(null,"Font: "+dialog.getFontName(),"Font Type",JOptionPane.PLAIN_MESSAGE);
mainTextPane.setFont(new Font(dialog.getFontName(),dialog.getFontStyle(),dialog.getFontSize()));
}
// Always repaint to get rid of the menu junk
repaint();
}
For a complete version of this code click here .