home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-05-23 | 2.3 KB | 95 lines |
- // FontRequestor
- // A pop-up window for choosing a font
- import java.awt.*;
-
- public class FontRequestor extends FixedFrame
- {
- Component parent;
- List name, size;
- Checkbox plain, bold, italic;
- Button ok, can;
-
- // Create an font requestor
- FontRequestor(Component p)
- {
- super(300, -1);
- parent = p;
-
- // Create interface
- setLayout(new BorderLayout());
- Panel top = new Panel();
- top.setLayout(new BorderLayout());
- Panel fontp = new Panel();
- fontp.setLayout(new BorderLayout());
- fontp.add("North", new Label("Font"));
- fontp.add("Center", name = new List());
- top.add("Center", fontp);
- Panel sizep = new Panel();
- sizep.setLayout(new BorderLayout());
- sizep.add("North", new Label("Size"));
- sizep.add("Center", size = new List());
- top.add("East", sizep);
- Panel style = new Panel();
- style.setLayout(new FlowLayout(FlowLayout.LEFT));
- CheckboxGroup stg = new CheckboxGroup();
- style.add(plain = new Checkbox("Plain", stg, true));
- style.add(bold = new Checkbox("Bold", stg, false));
- style.add(italic = new Checkbox("Italic", stg, false));
- top.add("South", style);
- add("Center", top);
- Panel bot = new Panel();
- bot.setLayout(new FlowLayout(FlowLayout.RIGHT));
- bot.add(ok = new Button("Ok"));
- bot.add(can = new Button("Cancel"));
- add("South", bot);
-
- // Fill font names and sizes
- String names[] = Toolkit.getDefaultToolkit().getFontList();
- for(int i=0; i<names.length; i++)
- name.addItem(names[i]);
- for(int i=4; i<128; i++)
- size.addItem(String.valueOf(i));
-
- setTitle("Choose a font");
- pack();
- show();
- }
-
- // Create a font requestor, with the given font chosen
- FontRequestor(Component p, Font f)
- {
- this(p);
- for(int i=0; i<name.countItems(); i++)
- if (name.getItem(i).equals(f.getName()))
- name.select(i);
- for(int i=0; i<size.countItems(); i++)
- if (Integer.parseInt(size.getItem(i)) == f.getSize())
- size.select(i);
- plain.setState(f.isPlain());
- bold.setState(f.isBold());
- italic.setState(f.isItalic());
- }
-
- // getfont
- // Returns the currently chosen font
- Font getfont()
- {
- return new Font(name.getSelectedItem(),
- plain.getState() ? Font.PLAIN :
- bold.getState() ? Font.BOLD : Font.ITALIC,
- Integer.parseInt(size.getSelectedItem()));
- }
-
- public boolean action(Event evt, Object obj)
- {
- if (evt.target == ok) {
- parent.postEvent(new Event(this, Event.ACTION_EVENT, null));
- dispose();
- }
- else if (evt.target == can)
- dispose();
- return true;
- }
- }
-
-