la viper | cherche JFontChooser sur google
Code :
- /*
- * JFontChooser.java
- *
- * The JFontChooser class is in the Public Domain, the code may be used
- * for any purpose. It is provided as is with no warranty.
- *
- * Author: James Bardsley (torasin@torasin.com)
- * Last Modfied: 27th January 2005
- */
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Container;
- import java.awt.Font;
- import java.awt.GraphicsEnvironment;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JDialog;
- import javax.swing.JLabel;
- import javax.swing.JList;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTextField;
- import javax.swing.border.TitledBorder;
- import javax.swing.event.ListSelectionEvent;
- import javax.swing.event.ListSelectionListener;
- public class JFontChooser extends JDialog implements ActionListener, ListSelectionListener
- {
- public static final long serialVersionUID = 62256323L;
- private static String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
- private static String[] style = {"Regular", "Bold", "Italic", "Bold Italic"};
- private static String[] size = {"5", "6", "7", "8", "9", "10", "11", "12", "14", "16", "18", "20", "22", "24", "26", "28", "36", "48", "72" };
- private Font font;
- private int option;
- private String textType = "";
- private int textStyle = 0;
- private int textSize = 0;
- private JList fList = new JList(fonts);
- private JList stList = new JList(style);
- private JList sizeList = new JList(size);
- private JTextField jtfFonts = new JTextField();
- private JTextField jtfStyle = new JTextField();
- private JTextField jtfSize = new JTextField();
- private JLabel jlbFonts = new JLabel("fonts" );
- private JLabel jlbStyle = new JLabel("style" );
- private JLabel jlbSize = new JLabel("size" );
- private JScrollPane jspFont = new JScrollPane(fList);
- private JScrollPane jspStyle = new JScrollPane(stList);
- private JScrollPane jspSize = new JScrollPane(sizeList);
- private JButton jbtOK = new JButton("valider" );
- private JButton jbtCancel = new JButton("annuler" );
- private JTextField jtfTest = new JTextField("test" );
- private JButton jbtColor = new JButton();
- public static final int OK_OPTION = 1;
- public static final int CANCEL_OPTION = 2;
- /**
- * Constructs a JFontChooser that uses the default font.
- */
- public JFontChooser()
- {
- this(new Font("Courier New", Font.PLAIN, 7),Color.BLACK);
- }
- /**
- * Constructs a JFontChooser using the given font.
- */
- public JFontChooser(Font aFont, Color aColor)
- {
- Container container = getContentPane();
- JPanel panel = new JPanel();
- TitledBorder panelBorder = new TitledBorder("demo" );
- this.setFont(aFont);
- jbtColor.setBackground(aColor);
- fList.setSelectionMode(0);
- stList.setSelectionMode(0);
- sizeList.setSelectionMode(0);
- jspFont.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
- jspStyle.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
- jspSize.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
- panel.setBorder(panelBorder);
- jtfFonts.setBounds(8, 32, 121, 20);
- jspFont.setBounds(8, 56, 121, 82);
- jtfStyle.setBounds(136, 32, 121, 20);
- jspStyle.setBounds(136, 56, 121, 82);
- jtfSize.setBounds(264, 32, 41, 20);
- jspSize.setBounds(264, 56, 41, 82);
- jbtOK.setBounds(320, 8, 89, 17);
- jbtCancel.setBounds(320, 32, 89, 17);
- jbtColor.setBounds(264,8,41,17);
- panel.setBounds(320, 64, 89, 73);
- container.add(jlbFonts);
- container.add(jtfFonts);
- container.add(jspFont);
- container.add(jlbStyle);
- container.add(jtfStyle);
- container.add(jspStyle);
- container.add(jlbSize);
- container.add(jtfSize);
- container.add(jspSize);
- container.add(jbtOK);
- container.add(jbtCancel);
- container.add(jbtColor);
- container.add(panel);
- jtfTest.setBounds(8, 25, 73, 30);
- panel.add(jtfTest);
- container.setLayout(null);
- panel.setLayout(null);
- setSize(424, 177);
- setResizable(false);
- setModal(true);
- jtfFonts.addActionListener(this);
- jtfSize.addActionListener(this);
- jtfStyle.addActionListener(this);
- jbtCancel.addActionListener(this);
- jbtOK.addActionListener(this);
- jbtColor.addActionListener(this);
- fList.addListSelectionListener(this);
- stList.addListSelectionListener(this);
- sizeList.addListSelectionListener(this);
- }
- /**
- * Displays the font dialog on the screen positioned relative to
- * the parent and blocks until the dialog is hidden.
- */
- public int showDialog(Component parent, String title)
- {
- boolean found = false;
- option = CANCEL_OPTION;
- this.setTitle(title);
- jtfTest.setFont(new Font(textType, textStyle, textSize));
- /*
- * Traverse through the lists and find the values that correspond
- * to the selected font. If it can't find the values then clear the
- * selection.
- */
- for (int i = 0; i < fList.getModel().getSize(); i++)
- {
- fList.setSelectedIndex(i);
-
- if (font.getName().equals((String)fList.getSelectedValue()))
- {
- i = fList.getModel().getSize() + 1;
- found = true;
- }
- }
- if (!found)
- {
- fList.clearSelection();
- }
- stList.setSelectedIndex(font.getStyle());
- found = false;
- for (int i = 0; i < sizeList.getModel().getSize(); i++)
- {
- sizeList.setSelectedIndex(i);
- if (font.getSize() == Integer.parseInt((String)sizeList.getSelectedValue()))
- {
- i = sizeList.getModel().getSize() + 1;
- found = true;
- }
- }
- if (!found)
- {
- sizeList.clearSelection();
- }
- this.setLocationRelativeTo(parent);
- this.setVisible(true);
- return option;
- }
- /**
- * Sets the current font of the font chooser.
- */
- public void setFont(Font aFont)
- {
- if(aFont!=null) {
- font = aFont;
- textType = aFont.getFontName();
- textStyle = aFont.getStyle();
- textSize = aFont.getSize();
- }
- }
- /**
- * Gets the current font of the font chooser.
- */
- public Font getFont()
- {
- return font;
- }
- public Color getColor() {
- return jbtColor.getBackground();
- }
-
- public void setColor(Color c) {
- if(c!=null) {
- jbtColor.setBackground(c);
- } else {
- jbtColor.setBackground(Color.BLACK);
- }
- }
- /**
- * Gets the name of the font chooser's current font.
- */
- public String getFontName()
- {
- return font.getFontName();
- }
- /**
- * Gets the style of the font chooser's current font.
- */
- public int getFontStyle()
- {
- return font.getStyle();
- }
- /**
- * Gets the size of the font chooser's current font.
- */
- public int getFontSize()
- {
- return font.getSize();
- }
- public void actionPerformed(ActionEvent e)
- {
- boolean found = false;
- if (e.getSource() == jtfFonts)
- {
- textType = jtfFonts.getText();
- for (int i = 0; i < fList.getModel().getSize(); i++)
- {
- if (((String)fList.getModel().getElementAt(i)).startsWith(jtfFonts.getText().trim()))
- {
- fList.setSelectedIndex(i);
- i = fList.getModel().getSize() + 1;
- found = true;
- }
- }
- if (!found)
- {
- fList.clearSelection();
- }
- else
- {
- jtfTest.setFont(new Font(textType, textStyle, textSize));
- }
- found = false;
- }
- else if (e.getSource() == jtfSize)
- {
- textSize = (Integer.parseInt(jtfSize.getText().trim()));
- jtfTest.setFont(new Font(textType, textStyle, textSize));
- for (int i = 0; i < sizeList.getModel().getSize(); i++)
- {
- if (jtfSize.getText().trim().equals((String)sizeList.getModel().getElementAt(i)))
- {
- sizeList.setSelectedIndex(i);
- i = sizeList.getModel().getSize() + 1;
- found = true;
- }
- }
- if (!found)
- {
- sizeList.clearSelection();
- }
- found = false;
- }
- else if (e.getSource() == jtfStyle)
- {
- if (jtfStyle.getText().equals("Regular" )) //$NON-NLS-1$
- {
- textStyle = 0;
- }
- else if (jtfStyle.getText().equals("Bold" )) //$NON-NLS-1$
- {
- textStyle = 1;
- }
- else if (jtfStyle.getText().equals("Italic" )) //$NON-NLS-1$
- {
- textStyle = 2;
- }
- else if (jtfStyle.getText().equals("Bold Italic" )) //$NON-NLS-1$
- {
- textStyle = 3;
- }
- stList.setSelectedIndex(textStyle);
- jtfTest.setFont(new Font(textType, textStyle, textSize));
- }
- else if (e.getSource() == jbtOK)
- {
- option = OK_OPTION;
- font = new Font(textType, textStyle, textSize);
- this.setVisible(false);
- }
- else if (e.getSource() == jbtCancel)
- {
- option = CANCEL_OPTION;
- this.setVisible(false);
- }
- else if(e.getSource() == jbtColor)
- {
- Color currentColor = jbtColor.getBackground();
- if(currentColor == Color.BLACK) {
- jbtColor.setBackground(Color.BLUE);
- } else if(currentColor == Color.BLUE) {
- jbtColor.setBackground(Color.CYAN);
- } else if(currentColor == Color.CYAN) {
- jbtColor.setBackground(Color.GRAY);
- } else if(currentColor == Color.GRAY) {
- jbtColor.setBackground(Color.GREEN);
- } else if(currentColor == Color.GREEN) {
- jbtColor.setBackground(Color.MAGENTA);
- } else if(currentColor == Color.MAGENTA) {
- jbtColor.setBackground(Color.ORANGE);
- } else if(currentColor == Color.ORANGE) {
- jbtColor.setBackground(Color.PINK);
- } else if(currentColor == Color.PINK) {
- jbtColor.setBackground(Color.RED);
- } else if(currentColor == Color.RED) {
- jbtColor.setBackground(Color.WHITE);
- } else if(currentColor == Color.WHITE) {
- jbtColor.setBackground(Color.BLACK);
- } else {
- jbtColor.setBackground(Color.BLUE);
- }
- }
- }
- public void valueChanged(ListSelectionEvent e)
- {
- if (e.getSource() == fList)
- {
- if (fList.getSelectedValue() != null)
- {
- jtfFonts.setText(((String)(fList.getSelectedValue())));
- }
- textType = jtfFonts.getText();
- jtfTest.setFont(new Font(textType, textStyle, textSize));
- }
- else if (e.getSource() == stList)
- {
- jtfStyle.setText(((String)(stList.getSelectedValue())));
- if (jtfStyle.getText().equals("Regular" )) //$NON-NLS-1$
- {
- textStyle = 0;
- }
- else if (jtfStyle.getText().equals("Bold" )) //$NON-NLS-1$
- {
- textStyle = 1;
- }
- else if (jtfStyle.getText().equals("Italic" )) //$NON-NLS-1$
- {
- textStyle = 2;
- }
- else if (jtfStyle.getText().equals("Bold Italic" )) //$NON-NLS-1$
- {
- textStyle = 3;
- }
- jtfTest.setFont(new Font(textType, textStyle, textSize));
- }
- else if (e.getSource() == sizeList)
- {
- if (sizeList.getSelectedValue() != null)
- {
- jtfSize.setText(((String)(sizeList.getSelectedValue())));
- }
- textSize = (Integer.parseInt(jtfSize.getText().trim()));
- jtfTest.setFont(new Font(textType, textStyle, textSize));
- }
- }
- }
|
Message édité par la viper le 19-09-2005 à 10:39:03
|