Ike said:
Anyone aware of code for a JDialog for Font selection ? This is not
incorporated into 1.4.x or higher, is it? Thanks, Ike
Sure, I had just run into this problem also. So I had to wrote my own.
You will have to modify a few things but then all the required methods
are there. I also use a foreground and background color selection along
with the font\font styles selection.
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class FontDialogSwing extends JDialog {
private static boolean isRunning = false;
private static final String BACKGROUND = "Background";
private static String defaultFont = "Dialog";
private static final String FOREGROUND = "Foreground";
private static JButton bgColorButton = new
JButton("Background", new ImageIcon(CommonSwing.getIcon("ColorSelection")));
private static JCheckBox ckbbold = new
JCheckBox(new ImageIcon(CommonSwing.getIcon("BoldFont")));
private static JButton closeButton = new
JButton("Close", new ImageIcon(CommonSwing.getIcon("Close")));
private static JButton fgColorButton = new
JButton("Foreground", new ImageIcon(CommonSwing.getIcon("ColorSelection")));
private static JComboBox fontsComboBox;
private static DatabaseManagerSwing fOwner;
private static JFrame frame = new
JFrame("DataBaseManagerSwing Font Selection Dialog");
private static JCheckBox ckbitalic = new
JCheckBox(new ImageIcon(CommonSwing.getIcon("ItalicFont")));
private static JSpinner spinnerFontSizes;
private static SpinnerNumberModel spinnerModelSizes;
/**
* Create and display FontDialogSwing Dialog.
*
*/
static public void CreatFontDialog(DatabaseManagerSwing owner) {
if (isRunning)
frame.setVisible(true);
else {
CommonSwing.setSwingLAF();
fOwner = owner;
frame.setIconImage(CommonSwing.getIcon("Frame"));
isRunning = true;
frame.setUndecorated(true);
frame.setSize(600,100);
CommonSwing.setFramePositon(frame);
ckbitalic.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
setStyle();
}
});
ckbbold.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
setStyle();
}
});
fgColorButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
setColor(FOREGROUND);
}
});
bgColorButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
setColor(BACKGROUND);
}
});
closeButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
}
});
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontNames = ge.getAvailableFontFamilyNames();
Dimension fontsComboBoxDimension = new Dimension(40, 25);
fontsComboBox = new JComboBox(fontNames);
fontsComboBox.setMaximumSize(fontsComboBox.getPreferredSize());
fontsComboBox.setEditable(true);
fontsComboBox.setSelectedItem(defaultFont);
fontsComboBox.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
setFont();
}
});
Dimension spinnerDimension = new Dimension(35, 20);
spinnerFontSizes = new JSpinner();
spinnerFontSizes.setMinimumSize(spinnerDimension);
spinnerFontSizes.setPreferredSize(spinnerDimension);
spinnerFontSizes.setMaximumSize(spinnerDimension);
spinnerModelSizes = new SpinnerNumberModel(12, 8, 72, 1);
spinnerFontSizes.setModel(spinnerModelSizes);
spinnerFontSizes.addChangeListener(
new ChangeListener(){
public void stateChanged( ChangeEvent e ) {
setFontSize();
}
});
Container contentPane = frame.getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add( fontsComboBox );
contentPane.add( spinnerFontSizes );
contentPane.add( ckbbold );
contentPane.add( ckbitalic );
contentPane.add( fgColorButton );
contentPane.add( bgColorButton );
contentPane.add( closeButton );
frame.pack();
frame.setVisible(false);
}
}
static public void setFont() {
Font txtResultFont = fOwner.txtResult.getFont();
fOwner.txtResult.setFont( new Font(
fontsComboBox.getSelectedItem().toString(),
txtResultFont.getStyle(), txtResultFont.getSize() ) );
Font txtCommandFont = fOwner.txtResult.getFont();
fOwner.txtCommand.setFont( new Font(
fontsComboBox.getSelectedItem().toString(),
txtCommandFont.getStyle(), txtCommandFont.getSize() ) );
Font txtTreeFont = fOwner.txtResult.getFont();
fOwner.tTree.setFont( new Font(
fontsComboBox.getSelectedItem().toString(),
txtTreeFont.getStyle(), txtTreeFont.getSize() ) );
}
/**
* Displays a color chooser and Sets the selected color.
*/
static public void setFontSize() {
float fontSize = ((Number)
(spinnerModelSizes.getValue())).floatValue();
Font fonttTree = fOwner.tTree.getFont().deriveFont(fontSize);
fOwner.tTree.setFont(fonttTree);
Font fontTxtCommand =
fOwner.txtCommand.getFont().deriveFont(fontSize);
fOwner.txtCommand.setFont(fontTxtCommand );
Font fontTxtResult =
fOwner.txtResult.getFont().deriveFont(fontSize);
fOwner.txtResult.setFont(fontTxtResult);
}
/**
* Changes the style (Bold, Italic ) of the selected text by
checking the
* style buttons
*/
static public void setStyle() {
int style = Font.PLAIN;
if (ckbbold.isSelected()) {
style |= Font.BOLD;
}
if (ckbitalic.isSelected()) {
style |= Font.ITALIC;
}
fOwner.tTree.setFont(fOwner.txtCommand.getFont().deriveFont(style));
fOwner.txtCommand.setFont(fOwner.txtCommand.getFont().deriveFont(style));
fOwner.txtResult.setFont(fOwner.txtResult.getFont().deriveFont(style));
}
static public void setColor(String inTarget) {
if (inTarget.equals(BACKGROUND) ) {
Color backgroundColor = JColorChooser.showDialog(null,
"Choose Color",
fOwner.txtResult.getBackground());
if (backgroundColor != null) {
bgColorButton.setBackground(backgroundColor);
fOwner.txtCommand.setBackground(backgroundColor);
fOwner.txtResult.setBackground(backgroundColor);
}
}
else {
Color foregroundColor = JColorChooser.showDialog(null,
"Choose Color",
fOwner.txtResult.getBackground());
if (foregroundColor != null) {
fgColorButton.setBackground(foregroundColor);
fOwner.txtCommand.setForeground(foregroundColor);
fOwner.txtResult.setForeground(foregroundColor);
}
}
}
}
--
Hope this helps...
IchBin
__________________________________________________________________________
'The meeting of two personalities is like the contact of two chemical
substances: if there is any reaction, both are transformed.'
- Carl Gustav Jung, (1875-1961), psychiatrist and psychologist