After studying one year java I started to try things and begun a small project of writing a text writer. The writer should have all basic functions and include RTF and images. I started with setting up the several menus and finished the file, edit and font menus (the more basic part of the GUI). At the moment I am working on two toolbars that will double some of the menu functions and add some more typical text writer stuff.
Font Picker is a clean and simple tool for identifying fonts and their details on any website. Once you install the extension, you will have the ability to right click on any text and view its font details. Clicking the 'Font Details' option will open up a pop up with all the information you need about that font. Search for fonts by foundry, designer, properties, languages, classifications, and more. Explore the latest additions to our font library at Adobe Fonts. Choose the text style you like; Copy and paste into Instagram, Facebook, or other social media platforms; Different text styles resulting from the generator can include unique cursive, calligraphy, handwriting, and web script fonts. You can also choose to add different symbols and emojis as well. Java2s.com © Demo Source and Support. All rights reserved. A font chooser is a component that lets the user pick various attributes for fonts.
I'd like to present it for comments and criticism the font menu. I tried to write it in a generic way so there is a simple Main class included for testing. (Put the 7 classes into a project called: fontchooser2)
There are 6 classes involved of which four I wrote myself (ColerFillcon is of Kim Topley author of the FFC book and MenuScroller I got from the Net Menu Scroller « Java Tips Weblog).
The base class, FontClass speaks for itself, all font families (String[]), styles (FontStyleKeyValue[]), sizes (String[]) and colors (HashMap) are derived from this class.
The two MenuFontMenu classes do the actual work. The first one takes care of the font family and offers the user the opportunity to construct a Short-font-list-menu from the long All-font-list-menu. The second class takes care of the rest (style, size and color). Some of the menu items will also appear on a toolbar as buttons and combo boxes. (In case of the file menu I worked exclusively with action derived from the AbstractAction class and it was very easy just to add these actions to the toolbar and have the action code only once present).
I should still look into how to comment the code in a more professional manner, so sorry for that part of the code (I first wanted to get the programming going).
package fontchooser2;
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author willem
*/
public class Fontclass {
private String familyName, currentFont;
private ArrayList <String> fontshortlist;
private Font font;
private int familyStyle, familySize;
private Map <String,Object> fontStylenames;
private FontStyleKeyValue[] fontstyleKeyValue;
private Map<String, Color> colorMap;
public Fontclass() {
fontStylenames = new HashMap<String,Object>();
}
public void getDefaultFontFields() {
if (font != null) {
familyName = font.getFamily();
familyStyle = font.getStyle();
familySize = font.getSize();
}
}
/**
* This important helper class sets the font family, style, size and color.
* These main font atributes form the basis of the two MenuFontMenu classes
* and the Toolbar class and its FontColorDialog helpers class
* All font stuff is set in this class so it can be considered as the
* base class of the application!
*
*/
public String[] getAllFontNames() {
GraphicsEnvironment e =
GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontNameList = e.getAvailableFontFamilyNames();
return fontNameList;
}
public FontStyleKeyValue[] getAllFontStyles() {
fontstyleKeyValue = new FontStyleKeyValue[] {
new FontStyleKeyValue('Regular', Font.PLAIN),
new FontStyleKeyValue('Bold', Font.BOLD),
new FontStyleKeyValue('Italic', Font.ITALIC)};
return fontstyleKeyValue;
}
public String[] getAllFontSizes() {
String[] fontSizeList = new String[18];
int count=0;
for (int i = 6; i <= 11; i++) {
fontSizeList[count] = ' + i;
count++;
}
for (int i = 12; i <= 28; i += 2) {
fontSizeList[count] = ' + i;
count++;
}
fontSizeList[15] = '36';
fontSizeList[16] = '48';
fontSizeList[17] = '72';
return fontSizeList;
}
public Map getColorList() {
colorMap = new HashMap<String, Color>();
colorMap.put('Black', Color.black);
colorMap.put('Light Grey', Color.lightGray);
colorMap.put('Grey', Color.gray);
colorMap.put('Dark Grey', Color.darkGray);
colorMap.put('Blue', Color.blue);
colorMap.put('Red', Color.red);
colorMap.put('Yellow', Color.yellow);
colorMap.put('Green', Color.green);
colorMap.put('Orange', Color.orange);
colorMap.put('Magenta', Color.magenta);
colorMap.put('Pink', Color.pink);
colorMap.put('Cyan', Color.cyan);
colorMap.put('White', Color.white);
return colorMap;
}
public class FontStyleKeyValue { // helper class for font style
private String key; // the style name
private int value; // the style contstant
public FontStyleKeyValue(String key, int value) {
this.key = key;
this.value = value;
}
public String getFontStyleKey() {
return key;
}
public int getFontStyleValue() {
return value;
}
}
}
package fontchooser2;
import fontchooser2.MenuFontMenu2.ColorChangeAction;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import javax.swing.AbstractAction;
import javax.swing.ButtonGroup;
import javax.swing.JMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenuItem;
import javax.swing.event.MenuListener;
/**
*
* @author willem
*/
class MenuFontMenu {
private Fontclass fontclass;
private MenuFontMenu2 fontMenu2;
private Font font;
private IconImport iconImport;
private Container textcanvas;
private JRadioButtonMenuItem rbShortList, rbSetup, rbAllfonts;
private ButtonGroup shortlistgroup;
private int familyStyle, familySize;
private String familyName, currentFont;
private JMenu fontMenu, allFontMenu, selectFontMenu, shortFontMenu;
private JMenuItem dummyItem;
private String[] fontNameList, fontStyleList,
fontSizeList, currentFontNameList;
private JCheckBoxMenuItem[] checkbMenuList;
private JRadioButtonMenuItem[] radiobAll, radiobShortlist;
private Map <String,Object> shortMenuMap;
private ColorChangeAction[] colorActions;
private MenuActionListener menulistener;
private MenuListener fontlistener;
public MenuFontMenu(Container textcanvas) {
// the text component reference
this.textcanvas = textcanvas;
iconImport = new IconImport('http://www.java-forums.org/images/');
// Get the system fonts
fontclass = new Fontclass();
fontNameList = fontclass.getAllFontNames();
shortlistgroup = new ButtonGroup();
menulistener = new MenuActionListener();
shortMenuMap = new HashMap<String,Object>();
//font menu extention for setting style, size and color
fontMenu2 = new MenuFontMenu2(textcanvas);
}
public String getCurrentFont() {
return currentFont;
}
public int getCurrentSize() {
return fontMenu2.getCurrentSize();
}
public int getCurrentStyle() {
return fontMenu2.getCurrentStyle();
}
public Color getCurrentColor() {
return fontMenu2.getCurrentColor();
}
public JMenu getMenu() {
return fontMenu;
}
public String[] getCurrentFontList() {
if (shortFontMenu null) {
currentFontNameList = fontNameList;
} else {
// Component comp;
Component[] comp = shortFontMenu.getComponents();
currentFontNameList = new String[comp.length];
for (int i=0; i < comp.length; i++) {
currentFontNameList[i] = ' + comp[i];
}
}
return currentFontNameList;
}
public ColorChangeAction[] getColorActions() {
return fontMenu2.getColorActions();
}
/**
* Create the Font menu to support family, style, size and color
* The family, style size and color elements are declared in
* the Fontclass.
* The buildFontMenu returns a populated menu: fontMenu
* The variable currentFont contains the selected font family
* The single font menu list gets removed and installed constantly
* 1) for selection radio buttons 2) for setting up the short list
* check boxes. The font family list is at the 4th position
* of the font menu.
* The second class MenuFontMenu2 adds the style size and color to
* the menu (down part from postion 4)
*/
public JMenu buildFontMenu() {
// Action for Font menu items / the all font list
AbstractAction allFontsAction = new AbstractAction() {
{ // 'Constructor'
putValue(NAME, 'All Fonts');
putValue(SMALL_ICON, iconImport.getImageIcon('whatever'));
putValue(MNEMONIC_KEY, new Integer('N'));
putValue(SHORT_DESCRIPTION, 'Creates a whatever');
putValue(LONG_DESCRIPTION, 'Creating new whatever..');
}
public void actionPerformed(ActionEvent evt) {
fontMenu.remove(4);
fontMenu.insert(allFontMenu, 4);
for (int i= 0; i < fontNameList.length; i++) { // remembering
radiobAll[i].setSelected(false); // previous selection
if (fontNameList[i].equals(currentFont)) { // short list
radiobAll[i].setSelected(true);
}
}
}
};
// the short font list after construction (see next block)
AbstractAction shortListFontsAction = new AbstractAction() {
{
putValue(NAME, 'Short Fontlist');
putValue(SMALL_ICON, iconImport.getImageIcon('whatever'));
putValue(MNEMONIC_KEY, new Integer('N'));
putValue(SHORT_DESCRIPTION, 'Creates whatever');
putValue(LONG_DESCRIPTION, 'Creating whatever..');
}
public void actionPerformed(ActionEvent evt) {
fontMenu.remove(4);
fontMenu.insert(shortFontMenu, 4);
if (shortFontMenu.getItemCount() > 1 & currentFont != null) {
shortlistgroup.clearSelection();
JRadioButtonMenuItem rb = (JRadioButtonMenuItem)
shortMenuMap.get(currentFont);
if (rb != null)
rb.setSelected(true); // remembering
} // previous selection all list
}
};
// constructing the short list see also makeShortFontlist
// and MenuActionListener. the list is stored in shortMenuMap
// and shortFontMenu
AbstractAction setupshortListFontsAction = new AbstractAction() {
{
putValue(NAME, 'Setup Shortlist');
putValue(SMALL_ICON, iconImport.getImageIcon('whatever'));
putValue(MNEMONIC_KEY, new Integer('N'));
putValue(SHORT_DESCRIPTION, 'Creates a new whatever');
putValue(LONG_DESCRIPTION, 'Creating new whatever..');
}
public void actionPerformed(ActionEvent evt) {
fontMenu.remove(4); // the selection is constructed at
fontMenu.insert(selectFontMenu, 4); // MenuActionListener
}
};
// the several menus
fontMenu = new JMenu('Fonts');
ButtonGroup group = new ButtonGroup();
// first the three fixed radio buttons All/ Short list/
//selecting short list
rbAllfonts = new JRadioButtonMenuItem(allFontsAction);
rbAllfonts.setSelected(true);
fontMenu.add(rbAllfonts);
group.add(rbAllfonts);
rbShortList = new JRadioButtonMenuItem(shortListFontsAction);
rbShortList.setSelected(false);
rbShortList.setEnabled(true);
fontMenu.add(rbShortList);
group.add(rbShortList);
rbSetup = new JRadioButtonMenuItem(setupshortListFontsAction);
rbSetup.setSelected(false);
fontMenu.add(rbSetup);
group.add(rbSetup);
fontMenu.addSeparator();
// a second menu added to show the font listings All/Short
allFontMenu = new JMenu('Font List');
selectFontMenu = new JMenu('Font List');
shortFontMenu = new JMenu('Font List');
buildFontMenuItems(fontNameList);
dummyItem = new JMenuItem('empty');
dummyItem.setEnabled(false);
shortFontMenu.add(dummyItem);
fontMenu.add(allFontMenu);
MenuScroller.setScrollerFor(allFontMenu);
MenuScroller.setScrollerFor(selectFontMenu);
// the menus of the extions from the class MenuFontMenu2
fontMenu.addSeparator();// the addional font menus of style, size and
fontMenu.add(fontMenu2.buildFontMenu2(fontMenu)); // color
return fontMenu; // returns the populated menu
}
class MenuActionListener implements ActionListener{
public void actionPerformed(ActionEvent evt) {
Object menusource = evt.getSource();
String cmd = evt.getActionCommand();
if (rbSetup.isSelected()){ // here setup the font short list
makeShortFontlist(evt);
} else { // here: readout the font family selection
currentFont = cmd;
System.out.println('naction: ' + currentFont);
}
if (shortFontMenu.getItemCount() > 0) { // removing the dummy of the
shortFontMenu.remove(dummyItem); // empty short list
} else {
shortFontMenu.add(dummyItem);
}
}
}
// building all radio buttons and checkboxes for selection (done only once)
public void buildFontMenuItems(String[] names) {
radiobAll = new JRadioButtonMenuItem[names.length];
radiobShortlist = new JRadioButtonMenuItem[names.length];
checkbMenuList = new JCheckBoxMenuItem[names.length];
ButtonGroup allFontgroup = new ButtonGroup();
Font ft = fontMenu.getFont();
Font font;
//the names-array contains all font families see FontClass
for (int i = 0; i < names.length; i++) {
font = new Font(names[i], Font.PLAIN, 16);
radiobAll[i] = new JRadioButtonMenuItem(names[i]);
checkbMenuList[i] = new JCheckBoxMenuItem(names[i]);
radiobAll[i].setActionCommand(names[i]);
checkbMenuList[i].setActionCommand(names[i]);
radiobAll[i].setFont(font);
checkbMenuList[i].setFont(font);
allFontgroup.add(radiobAll[i]);
radiobAll[i].addActionListener(menulistener);
checkbMenuList[i].addActionListener(menulistener);
selectFontMenu.add(checkbMenuList[i]);
allFontMenu.add(radiobAll[i]);
}
allFontMenu.setFont(ft);
selectFontMenu.setFont(ft);
}
// this method gets called when selecting the font families for the
// short font menu list stored in: shortMenuMap and shortFontMenu
// this menu gets removed and installed at the 4th position of the font menu
public void makeShortFontlist(ActionEvent evt) {
String cmd = evt.getActionCommand();
JRadioButtonMenuItem rb;
Font ft = fontMenu.getFont();
Font font;
for (int i = 0; i < fontNameList.length; i++) {
String s = checkbMenuList[i].getActionCommand();
if (cmd.equals(s)) {
if (checkbMenuList[i].isSelected()) { // adding onto the list
font = new Font(s, Font.PLAIN, 16); // when selected
rb = new JRadioButtonMenuItem(s);
rb.setActionCommand(s);
rb.addActionListener(menulistener);
rb.setFont(font);
shortlistgroup.add(rb);
shortFontMenu.add(rb);
shortMenuMap.put(s, rb);
} else { // removing from the list
rb = (JRadioButtonMenuItem) shortMenuMap.get(s);
shortMenuMap.remove(s);
shortlistgroup.remove(rb);
shortFontMenu.remove(rb);
}
}
}
shortFontMenu.setFont(ft);
}
}
package fontchooser2;
import fontchooser2.Fontclass.FontStyleKeyValue;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.swing.AbstractAction;
import javax.swing.ButtonGroup;
import javax.swing.JMenu;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
/**
*
* @author willem
*/
class MenuFontMenu2 extends JPanel {
private IconImport iconImport;
private Container textcanvas;
private Fontclass fontclass;
private String[] fontSizeList;
private ColorChangeAction[] colorActions;
private MenuStyleAction menuStyleAction;
private String currentColor;
private int currentSize, currentStyle;
private Color color;
private FontStyleKeyValue[] fontStyleList;
private Map<String, Color> colorMap;
// private FontStyle fontstyle;
public MenuFontMenu2(Container textcanvas) {
this.textcanvas = textcanvas;
iconImport = new IconImport('http://www.java-forums.org/images/');
// Get all the system fonts
fontclass = new Fontclass();
fontStyleList = fontclass.getAllFontStyles();
fontSizeList = fontclass.getAllFontSizes();
colorMap = fontclass.getColorList();
}
/**
* MenuFontMenu2
* creates the Font menu to support style, size and color it is
* the extention of the MenuFontMenu class and
* Sets the lower part of the font menu
*/
public int getCurrentSize() {
return currentSize;
}
public int getCurrentStyle() {
return currentStyle;
}
public Color getCurrentColor() {
return color;
}
public ColorChangeAction[] getColorActions() {
return colorActions;
}
public JMenu buildFontMenu2(JMenu jm) { // the previous font class
// sets the reference (jm) for
//the fontstyle menu added extending the menu list
JMenu fontStyle = new JMenu('Font Style');
menuStyleAction = new MenuStyleAction(');
Font font;
Font ft = jm.getFont();
JRadioButtonMenuItem rbStyle;
ButtonGroup buttongroupStyle = new ButtonGroup();
for (int i=0; i < fontStyleList.length; i++) { // the style menu
font = new Font(' + ft, fontStyleList[i].getFontStyleValue(), 16);
rbStyle = new JRadioButtonMenuItem(fontStyleList[i].getFontStyleKey());
rbStyle.setFont(font);
rbStyle.setActionCommand(' + fontStyleList[i].getFontStyleKey());
rbStyle.addActionListener(menuStyleAction);
buttongroupStyle.add(rbStyle);
fontStyle.add(rbStyle);
}
jm.add(fontStyle);
//the font size menu added
JMenu fontSize = new JMenu('Font Size');
ft = jm.getFont();
JRadioButtonMenuItem rbSize;
ButtonGroup buttongroupSize = new ButtonGroup();
for (int i=0; i < fontSizeList.length; i++) { // the font size menu
rbSize = new JRadioButtonMenuItem(fontSizeList[i]);
rbSize.setActionCommand(fontSizeList[i]);
rbSize.addActionListener(menuStyleAction);
buttongroupSize.add(rbSize);
fontSize.add(rbSize);
}
jm.add(fontSize);
//the color menu added
JMenu fontColor = new JMenu('Font Color');
ft = jm.getFont();
JRadioButtonMenuItem rbColor;
int size = colorMap.size();
colorActions = new ColorChangeAction[size];
Set entries = colorMap.entrySet();
Iterator iter = entries.iterator();
int count = 0;
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry)iter.next();
String key = (String) entry.getKey();
Color value = (Color) entry.getValue();
colorActions[count] = new ColorChangeAction(key, value);
count++;
}
ButtonGroup buttongroupColor = new ButtonGroup();
for (int i=0; i < colorActions.length; i++) { // setting the colors
rbColor = new JRadioButtonMenuItem(colorActions[i]);
buttongroupColor.add(rbColor);
fontColor.add(rbColor);
}
jm.add(fontColor);
return jm;
}
// the action for the choosen color
class ColorChangeAction extends AbstractAction {
ColorChangeAction(String name, Color color) { // color name and color
// size of icon + border
super(name, new ColorFillIcon(color, 15, 10, 1));
this.color = color;
}
// Action to take when this color is selected
public void actionPerformed(ActionEvent evt) {
System.out.println('Color chosen: ' + color);
}
Color color;
}
// the actions for font style and size
class MenuStyleAction extends AbstractAction {
public MenuStyleAction(String name) {
putValue(NAME, name);
putValue(SMALL_ICON, iconImport.getImageIcon('whatever'));
putValue(MNEMONIC_KEY, new Integer('N'));
putValue(SHORT_DESCRIPTION, 'Creates whatever');
putValue(LONG_DESCRIPTION, 'Creating whatever..');
}
public void actionPerformed(ActionEvent evt) {
Object menusource = evt.getSource();
String cmd = evt.getActionCommand();
// sets the current font style
for (int i=0; i < fontStyleList.length; i++) {
if (cmd.equals(fontStyleList[i].getFontStyleKey())) {
currentStyle = fontStyleList[i].getFontStyleValue();
System.out.println('Font Style: ' + currentStyle
+ ' string ' + cmd);
}
}
// sets the current font size
for (int i=0; i < fontSizeList.length; i++) {
if (cmd.equals(fontSizeList[i])) {
currentSize = Integer.parseInt(fontSizeList[i]);
System.out.println('Font Size: ' + currentSize);
}
}
}
}
}
GtkFontChooserDialogGtkFontChooserDialog — A dialog for selecting fonts Pantone 000c. |
Functions
Types and Values
struct | GtkFontChooserDialog |
struct | GtkFontChooserDialogClass |
Implemented Interfaces
GtkFontChooserDialog implements AtkImplementorIface, GtkBuildable and GtkFontChooser.
Description
Css Font Chooser
The GtkFontChooserDialog widget is a dialog for selecting a font.It implements the GtkFontChooser interface.
GtkFontChooserDialog as GtkBuildable
Wordmark
The GtkFontChooserDialog implementation of the GtkBuildableinterface exposes the buttons with the names “select_button”and “cancel_button”.
Functions
gtk_font_chooser_dialog_new ()
Creates a new GtkFontChooserDialog.
Parameters
title | Title of the dialog, or NULL . | [allow-none] |
parent | Transient parent of the dialog, or NULL . | [allow-none] |
Since: 3.2
Types and Values
struct GtkFontChooserDialogClass
Members
Font Chooser Html
See Also
GtkFontChooser, GtkDialog