FontChooser, adapted from NwFontChooserS by Noah Wairauch : Font Chooser « Swing Components « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Swing Components » Font ChooserScreenshots 
FontChooser, adapted from NwFontChooserS by Noah Wairauch
FontChooser, adapted from NwFontChooserS by Noah Wairauch
   

/*BEGIN_COPYRIGHT_BLOCK
 *
 * Copyright (c) 2001-2008, JavaPLT group at Rice University (drjava@rice.edu)
 * All rights reserved.
 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *    * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *    * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
 *      names of its contributors may be used to endorse or promote products
 *      derived from this software without specific prior written permission.
 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software is Open Source Initiative approved Open Source Software.
 * Open Source Initative Approved is a trademark of the Open Source Initiative.
 
 * This file is part of DrJava.  Download the current version of this project
 * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
 
 * END_COPYRIGHT_BLOCK*/


import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.*;

/**
 * FontChooser, adapted from NwFontChooserS by Noah Wairauch.
 * (see http:///forum.java.sun.com/thread.jsp?forum=57&thread=195067)
 *
 @version $Id: FontChooser.java 4872 2009-04-06 21:37:28Z mgricken $
 */

public class FontChooser extends JDialog {
  /** Available font styles.
   */
  private static final String[] STYLES =
      new String[] { "Plain""Bold""Italic""Bold Italic" };

  /** Available font sizes.
   */
  private static final String[] SIZES =
      new String[] { "3""4""5""6""7""8""9""10""11""12",
                     "13""14""15""16""17""18""19""20""22",
                     "24""27""30""34""39""45""51""60"};

  // Lists to display
  private NwList _styleList;
  private NwList _fontList;
  private NwList _sizeList;

  // Swing elements
  private JButton _okButton;
  private JButton _cancelButton;
  private JLabel _sampleText = new JLabel();

  private boolean _clickedOK = false;

  /** Constructs a new modal FontChooser for the given frame,
   * using the specified font.
   */
  private FontChooser(Frame parent, Font font) {
    super(parent, true);
    initAll();
    if (font == nullfont = _sampleText.getFont();
    _fontList.setSelectedItem(font.getName());
    _sizeList.setSelectedItem(font.getSize() "");
    _styleList.setSelectedItem(STYLES[font.getStyle()]);
    //this.setResizable(false);
    resize();
  }

  /** Method used to show the font chooser, and select a new font.
   *
   @param parent The parent frame.
   @param title  The title for this window.
   @param font   The previously chosen font.
   @return the newly chosen font.
   */
  public static Font showDialog(Frame parent, String title, Font font) {
    FontChooser fd = new FontChooser(parent, font);
    fd.setTitle(title);
    
    fd.setVisible(true);

    Font chosenFont = null;
    if (fd.clickedOK()) {
      chosenFont = fd.getFont();
    }
    fd.dispose();
    return (chosenFont);
  }

  /** Shows the font chooser with a standard title ("Font Chooser").
   */
  public static Font showDialog(Frame parent, Font font) {
    return showDialog(parent, "Font Chooser", font);
  }

  private void initAll() {
    getContentPane().setLayout(null);
    setBounds(5050425400);
    _sampleText = new JLabel();
    addLists();
    addButtons();
    _sampleText.setForeground(Color.black);
    getContentPane().add(_sampleText);
    addWindowListener(new WindowAdapter() {
      public void windowClosing(java.awt.event.WindowEvent e) {
        setVisible(false);
      }
    });
    addComponentListener(new ComponentAdapter() {
      public void componentResized(ComponentEvent evt) {
        resize();
      }
    });
  }

  private void resize() {
    int w = getWidth();
    int h = getHeight();
    double wf = (doublew / 425;
    int w2 = (int) (80 * wf);
    int w3 = (int) (50 * wf);
    if (w3 < 30w3 = 30;
    int w1 = w - w2 - w3 - 25;
    _fontList.setBounds(55, w1, h - 91);
    _styleList.setBounds(w1 + 105, w2, h - 91);
    _sizeList.setBounds(w1 + w2 + 155, w3, h - 91);
    _sampleText.setBounds(10, h - 78, w - 2045);
    _okButton.setBounds(w - 165, h - 557020);
    _cancelButton.setBounds(w - 81, h - 557020);
    validate();
  }

  private void addLists() {
    _fontList = new NwList(GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames());
    _styleList = new NwList(STYLES);
    _sizeList = new NwList(SIZES);
    getContentPane().add(_fontList);
    getContentPane().add(_styleList);
    getContentPane().add(_sizeList);
  }

  private void addButtons() {
    _okButton = new JButton("OK");
    _okButton.setMargin(new Insets(0000));
    _cancelButton = new JButton("Cancel");
    _cancelButton.setMargin(new Insets(0000));
    _okButton.setFont(new Font(" "111));
    _cancelButton.setFont(new Font(" "112));
    getContentPane().add(_okButton);
    getContentPane().add(_cancelButton);
    _okButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        setVisible(false);
        _clickedOK = true;
      }
    });
    _cancelButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        setVisible(false);
        _clickedOK = false;
      }
    });
  }

  private void showSample() {
    int g = 0;
    try g = Integer.parseInt(_sizeList.getSelectedValue())}
    catch (NumberFormatException nfe) { /* do nothing */ }
    String st = _styleList.getSelectedValue();
    int s = Font.PLAIN;
    if (st.equalsIgnoreCase("Bold")) s = Font.BOLD;
    if (st.equalsIgnoreCase("Italic")) s = Font.ITALIC;
    if (st.equalsIgnoreCase("Bold Italic")) s = Font.BOLD | Font.ITALIC;
    _sampleText.setFont(new Font(_fontList.getSelectedValue(), s, g));
    _sampleText.setText("The quick brown fox jumped over the lazy dog.");
    _sampleText.setVerticalAlignment(SwingConstants.TOP);
  }

  /** Returns whether the user clicked OK when the dialog was closed. (If false, the user clicked cancel.) */
  public boolean clickedOK() { return _clickedOK; }

  /** Returns the currently selected Font. */
  public Font getFont() { return _sampleText.getFont()}

  /** Private inner class for a list which displays a list of options in addition to a label indicating the currently
    * selected item.
    */
  public class NwList extends JPanel {
    JList jl;
    JScrollPane sp;
    JLabel jt;
    String si = " ";

    public NwList(String[] values) {
      setLayout(null);
      jl = new JList(values);
      sp = new JScrollPane(jl);
      jt = new JLabel();
      jt.setBackground(Color.white);
      jt.setForeground(Color.black);
      jt.setOpaque(true);
      jt.setBorder(new JTextField().getBorder());
      jt.setFont(getFont());
      jl.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
          jt.setText((Stringjl.getSelectedValue());
          si = (Stringjl.getSelectedValue();
          showSample();
        }
      });
      add(sp);
      add(jt);
    }

    public void setBounds(int x, int y, int w, int h) {
      super.setBounds(x, y, w, h);
      sp.setBounds(0, y + 16, w, h - 23);
      sp.revalidate();
      jt.setBounds(00, w, 20);
    }

    public String getSelectedValue() { return (si)}

    public void setSelectedItem(String s) { jl.setSelectedValue(s, true);}
  }
}

   
    
    
  
Related examples in the same category
1. Word like special font chooser
2. Font Chooser Source CodeFont Chooser Source Code
3. JFreeChart: Font DialogJFreeChart: Font Dialog
4. Font Chooser extends javax.swing.JDialogFont Chooser extends javax.swing.JDialog
5. Font Dialog from clariboleFont Dialog from claribole
6. Font Loader Dialog
7. FontChooser by Noah w
8. JFont Chooser
9. Font dialog
10. A dialog allow selection and a font and its associated info.A dialog allow selection and a font and its associated info.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.