Results 1 to 5 of 5
  1. #1
    pndiwakar is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Default [New to java]: HoW to set font from the available list in the system

    Hi Experts.

    I am new to java. with the help of available resource, trying to complete the project in java. In one of the module FONTSELECT, finding difficulty to select the font from the available list in the system. Please help me to resolve & simplify the code.

    CODE is as below



    public GTfontselect(Frame parent)
    {
    super(parent, "Choose Font", true);
    myparent = (GTwindow) parent;
    mylayout = new GridBagLayout();
    myconstraints = new GridBagConstraints();
    this.setLayout(mylayout);

    p1 = new Panel();

    p1.add(new JLabel("Font Type: "));
    p1.setLayout(mylayout);

    GraphicsEnvironment gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
    fonts = new JComboBox(gEnv.getAvailableFontFamilyNames());
    fonts.setSelectedItem(fontChoice);
    fonts.setMaximumRowCount(10);
    fonts.addItemListener(this);
    p1.add(fonts);

    new Label("Font type:");

    p1.add(new JLabel("Font Style: "));
    p1.setLayout(mylayout);

    String[] styleNames = {"Plain", "Bold", "Italic", "Bold+Italic", "2D text", "3D text"};
    styles = new JComboBox(styleNames);
    styles.addItemListener(this);
    p1.add(styles);


    p1.add(new JLabel(" Font Size: "));
    p1.setLayout(mylayout);
    String[] sizes = {"10", "12", "14", "16", "18", "20", "24", "34", "36", "48"};
    size = new JComboBox(sizes);
    size.addItemListener(this);
    p1.add(size);

    this.add(p1);

    p1.setFont(new Font(fontChoice, styleChoice, sizeChoice));


    public void actionPerformed(ActionEvent E)
    {
    if (E.getSource() == this.ok)
    {
    draw();
    this.dispose();
    return;
    }

    else if (E.getSource() == this.cancel)
    {
    this.dispose();
    return;
    }
    }

    public void itemStateChanged(ItemEvent E)
    {
    if (E.getSource() == fonts) {
    fontChoice = (String)fonts.getSelectedItem();
    } else {
    styleChoice = styles.getSelectedIndex();
    }

    p1.setFont(new Font(fontChoice, styleChoice, styleChoice));
    sample.repaint();
    if (E.getSource() == styles)
    {
    if (styles.getSelectedIndex() == 0) {
    myparent.drawstyle = Font.PLAIN;
    sample.repaint();
    return;
    }
    else if (styles.getSelectedIndex() == 1) {
    myparent.drawstyle = Font.BOLD;
    sample.repaint();
    return;
    }
    else if (styles.getSelectedIndex() == 2) {
    myparent.drawstyle = Font.ITALIC;
    sample.repaint();
    return;
    }
    else if (styles.getSelectedIndex() == 3) {
    myparent.drawstyle = Font.BOLD + Font.ITALIC;
    sample.repaint();
    return;
    }
    }

    // End


    else if (E.getSource() == this.size)
    {
    if (size.getSelectedIndex() == 0) myparent.drawsize = 10;
    if (size.getSelectedIndex() == 1) myparent.drawsize = 12;
    if (size.getSelectedIndex() == 2) myparent.drawsize = 14;
    if (size.getSelectedIndex() == 3) myparent.drawsize = 16;
    if (size.getSelectedIndex() == 4) myparent.drawsize = 18;
    if (size.getSelectedIndex() == 5) myparent.drawsize = 20;
    if (size.getSelectedIndex() == 6) myparent.drawsize = 24;
    if (size.getSelectedIndex() == 7) myparent.drawsize = 34;
    if (size.getSelectedIndex() == 8) myparent.drawsize = 36;
    if (size.getSelectedIndex() == 9) myparent.drawsize = 48;
    sample.repaint();
    return;
    }
    }

    public void keyPressed(KeyEvent E)
    {
    if (E.getSource() == this.ok)
    {
    if (E.getKeyCode() == E.VK_ENTER)
    {
    this.draw();
    this.dispose();
    return;
    }
    }

    else if (E.getSource() == this.cancel)
    {
    if (E.getKeyCode() == E.VK_ENTER)
    {
    this.dispose();
    return;
    }
    }

    else if (E.getSource() == this.text)
    {
    if (E.getKeyCode() == E.VK_ENTER)
    {
    this.draw();
    this.dispose();
    return;
    }
    else if (E.getKeyCode() == E.VK_TAB)
    {
    this.text.transferFocus();
    return;
    }
    else
    {
    sample.text = new String(this.text.getText());
    sample.repaint();
    return;
    }
    }
    }

    public void keyReleased(KeyEvent E)
    {
    }

    public void keyTyped(KeyEvent E)
    {
    }

    public void windowActivated(WindowEvent E)
    {
    }

    public void windowClosed(WindowEvent E)
    {
    }

    public void windowClosing(WindowEvent E)
    {
    this.dispose();
    return;
    }

    public void windowDeactivated(WindowEvent E)
    {
    }

    public void windowDeiconified(WindowEvent E)
    {
    }

    public void windowIconified(WindowEvent E)
    {
    }

    public void windowOpened(WindowEvent E)
    {
    }
    }
    Last edited by pndiwakar; 03-10-2009 at 06:35 AM. Reason: correction

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    Nobody can read that and guess what you're "having difficulty" with, so why don't you ask whoever gave you that code? Assuming, of course, that the code was given to you and you didn't lift it without the owner's permission.

    Or lift some more code, with my permission, and compare it with yours to see what might be wrong.
    Visual Font Designer Java Tips Weblog

    Another web page you really need to read:
    How To Ask Questions The Smart Way

    db

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    pndiwakar is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Replies: 5
    Last Post: 04-25-2009, 08:00 PM
  2. How to Search a List in Java
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:38 PM
  3. List of System properties
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-29-2007, 04:56 PM
  4. Replies: 2
    Last Post: 08-07-2007, 07:22 PM
  5. Replies: 1
    Last Post: 07-27-2007, 05:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •