Font selection dialog

I

Ike

Anyone aware of code for a JDialog for Font selection ? This is not
incorporated into 1.4.x or higher, is it? Thanks, Ike
 
C

Chris Smith

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

To answer your second question, no it does not exist in the standard
JDK. I'm not aware of anything widely available, but I haven't looked
either. Hopefully someone else can help you find an existing font
dialog class to use.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
I

IchBin

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
 
I

IchBin

Ike said:
Thanks! But what is fOwner? -Ike
fOwner is of type private static DatabaseManagerSwing. I use this to
make it a class var and visible to all of the methods in the class
FontDialogSwing. Just happens that type DatabaseManagerSwing is the
calling class name. I did not make this a model dialog. I then
can get/set attributes of the calling class objects.

This is not the standard way of way of coding it but it fit what I
wanted do. This is so I have a reference to all of the object of type
DatabaseManagerSwing. Thats why you see references to it later in the code.

The important thing is that it has all the objects you need to build
your own dialog, the way you want


Thanks in Advance...
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top