home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / Javacup / PR8ADPL7.TAR / productivity_tools / PR8ADPL7 / FontRequestor.java < prev    next >
Encoding:
Java Source  |  1996-05-23  |  2.3 KB  |  95 lines

  1. // FontRequestor
  2. // A pop-up window for choosing a font
  3. import java.awt.*;
  4.  
  5. public class FontRequestor extends FixedFrame
  6. {
  7.     Component parent;
  8.     List name, size;
  9.     Checkbox plain, bold, italic;
  10.     Button ok, can;
  11.  
  12.     // Create an font requestor
  13.     FontRequestor(Component p)
  14.     {
  15.     super(300, -1);
  16.     parent = p;
  17.  
  18.     // Create interface
  19.     setLayout(new BorderLayout());
  20.     Panel top = new Panel();
  21.     top.setLayout(new BorderLayout());
  22.     Panel fontp = new Panel();
  23.     fontp.setLayout(new BorderLayout());
  24.     fontp.add("North", new Label("Font"));
  25.     fontp.add("Center", name = new List());
  26.     top.add("Center", fontp);
  27.     Panel sizep = new Panel();
  28.     sizep.setLayout(new BorderLayout());
  29.     sizep.add("North", new Label("Size"));
  30.     sizep.add("Center", size = new List());
  31.     top.add("East", sizep);
  32.     Panel style = new Panel();
  33.     style.setLayout(new FlowLayout(FlowLayout.LEFT));
  34.     CheckboxGroup stg = new CheckboxGroup();
  35.     style.add(plain = new Checkbox("Plain", stg, true));
  36.     style.add(bold = new Checkbox("Bold", stg, false));
  37.     style.add(italic = new Checkbox("Italic", stg, false));
  38.     top.add("South", style);
  39.     add("Center", top);
  40.     Panel bot = new Panel();
  41.     bot.setLayout(new FlowLayout(FlowLayout.RIGHT));
  42.     bot.add(ok = new Button("Ok"));
  43.     bot.add(can = new Button("Cancel"));
  44.     add("South", bot);
  45.  
  46.     // Fill font names and sizes
  47.     String names[] = Toolkit.getDefaultToolkit().getFontList();
  48.     for(int i=0; i<names.length; i++)
  49.         name.addItem(names[i]);
  50.     for(int i=4; i<128; i++)
  51.         size.addItem(String.valueOf(i));
  52.  
  53.     setTitle("Choose a font");
  54.     pack();
  55.     show();
  56.     }
  57.  
  58.     // Create a font requestor, with the given font chosen
  59.     FontRequestor(Component p, Font f)
  60.     {
  61.     this(p);
  62.     for(int i=0; i<name.countItems(); i++)
  63.         if (name.getItem(i).equals(f.getName()))
  64.             name.select(i);
  65.     for(int i=0; i<size.countItems(); i++)
  66.         if (Integer.parseInt(size.getItem(i)) == f.getSize())
  67.             size.select(i);
  68.     plain.setState(f.isPlain());
  69.     bold.setState(f.isBold());
  70.     italic.setState(f.isItalic());
  71.     }
  72.  
  73.     // getfont
  74.     // Returns the currently chosen font
  75.     Font getfont()
  76.     {
  77.     return new Font(name.getSelectedItem(),
  78.             plain.getState() ? Font.PLAIN :
  79.             bold.getState() ? Font.BOLD : Font.ITALIC,
  80.             Integer.parseInt(size.getSelectedItem()));
  81.     }
  82.  
  83.     public boolean action(Event evt, Object obj)
  84.     {
  85.     if (evt.target == ok) {
  86.         parent.postEvent(new Event(this, Event.ACTION_EVENT, null));
  87.         dispose();
  88.         }
  89.     else if (evt.target == can)
  90.         dispose();
  91.     return true;
  92.     }
  93. }
  94.  
  95.