Java Reference
In-Depth Information
Example 10•18: FontChooser.java (continued)
public void itemChosen(ItemChooser.Event e) {
setFontStyle(((Integer)e.getSelectedValue()).intValue());
}
});
sizes.addItemChooserListener(new ItemChooser.Listener() {
public void itemChosen(ItemChooser.Event e) {
setFontSize(Integer.parseInt((String)e.getSelectedValue()));
}
});
// Create a component to preview the font.
preview = new JTextArea(defaultPreviewString, 5, 40);
preview.setFont(selectedFont);
// Create buttons to dismiss the dialog, and set handlers on them
okay = new JButton("Okay");
cancel = new JButton("Cancel");
okay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { hide(); }
});
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selectedFont = null;
hide();
}
});
// Put the ItemChoosers in a Box
Box choosersBox = Box.createHorizontalBox();
choosersBox.add(Box.createHorizontalStrut(15));
choosersBox.add(families);
choosersBox.add(Box.createHorizontalStrut(15));
choosersBox.add(styles);
choosersBox.add(Box.createHorizontalStrut(15));
choosersBox.add(sizes);
choosersBox.add(Box.createHorizontalStrut(15));
choosersBox.add(Box.createGlue());
// Put the dismiss buttons in another box
Box buttonBox = Box.createHorizontalBox();
buttonBox.add(Box.createGlue());
buttonBox.add(okay);
buttonBox.add(Box.createGlue());
buttonBox.add(cancel);
buttonBox.add(Box.createGlue());
// Put the choosers at the top, the buttons at the bottom, and
// the preview in the middle.
Container contentPane = getContentPane();
contentPane.add(new JScrollPane(preview), BorderLayout.CENTER);
contentPane.add(choosersBox, BorderLayout.NORTH);
contentPane.add(buttonBox, BorderLayout.SOUTH);
// Set the dialog size based on the component size.
pack();
}
/**
Search WWH ::




Custom Search