Link to home
Start Free TrialLog in
Avatar of Roman_Kaminski
Roman_Kaminski

asked on

Problems with setting the font and size for my Project. ActionEvent

hey
i have a problem with my java program. The problem is that the font and font size wont change when i try changing them it gives me an error could anyone please help me????

pageTA.setFont(new Font(fontType,fontStyle, fontSize));
not sure how to use this in my actionEvent

here is my program:


//Description: a very simple text editor program using
//menus to allow the user to manipulate text.
//Author:Roman Kaminski               Date: Dec 2 /2003


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import java.io.*;
import java.awt.Toolkit;

public class Project3 extends JFrame implements ActionListener
{
  private JMenuBar menuMB = new JMenuBar();
  private JMenu fileM, editM, helpM;
  private JMenuItem openI, saveI, exitI;
  private JMenuItem cutI, copyI, pasteI, selectI;
  private JMenuItem aboutI;
  private JTextArea pageTA = new JTextArea();
  private String scratchpad = "";
  private JPanel font;
  private JLabel fSize, fStyle, blank1, blank2, blank3, blank4, blank5, blank6, blank7, blank8, blank9, blank10, blank11, blank12, blank13;
  private Font fonta;




  //constructor method
  public Project3()
  {
     this.setTitle("Text Editor");
     Toolkit theKit = this.getToolkit();
     Dimension wndSize = theKit.getScreenSize();
     this.setBounds(wndSize.width/4, wndSize.height/4,
                      wndSize.width/2, wndSize.height/2);
     setFileMenu();
     setEditMenu();
     setHelpMenu();
     this.setJMenuBar(menuMB);
     setDefaultCloseOperation(EXIT_ON_CLOSE);
     fonta = new Font("Arial Black",Font.PLAIN, 10);


     Container pane = this.getContentPane();
     pane.setLayout(new GridLayout(2,1));

     pageTA.setSize(590, 390);
     pane.add(pageTA);
     pane.add(new JScrollPane(pageTA));
     pageTA.setLineWrap(true);
     pageTA.setFont(fonta);
     pageTA.setBackground(new Color(190,203,255));


     //create panel for font changes
     font = new JPanel();
     font.setLayout(new GridLayout(2,2));
  // font.setLayout(new FlowLayout);
     fSize = new JLabel("Size");
     blank1 = new JLabel("");
     blank2 = new JLabel("");
     blank3 = new JLabel("");
     blank4 = new JLabel("");
   //  font.add(blank1);
  //   font.add(blank2);
    // font.add(blank3);
  //   font.add(blank4);
     font.add(fSize);
     font.setBackground(new Color(190,203,255));

     //create JComboBox for font sizes
     String[] fontSizes = {"8","9","10", "12", "14", "16", "18", "20", "22", "24", "26", "28","36","48","72"};
     JComboBox fontChange = new JComboBox(fontSizes);
     fontChange.addActionListener(this);
     font.add(fontChange);
     fontChange.setBackground(new Color(190,203,255));

     blank9 = new JLabel("");
    blank10 = new JLabel("");
    blank11 = new JLabel("");
    blank12 = new JLabel("");
    blank13 = new JLabel("");
//    font.add(blank9);
//    font.add(blank10);
 //   font.add(blank11);
 //   font.add(blank12);
//    font.add(blank13);

     fStyle = new JLabel("Style");
     blank5 = new JLabel("");
     blank6 = new JLabel("");
     blank7 = new JLabel("");
     blank8 = new JLabel("");
 //    font.add(blank5);
 //    font.add(blank6);
 //    font.add(blank7);
   //  font.add(blank8);

     font.add(fStyle);

     //create JComboBox for font styles
     String[] fontStyles = {"Times New Roman", "Arial", "Comic Sans MS"};
     JComboBox fontStyleChange = new JComboBox(fontStyles);
     font.add(fontStyleChange);
     fontStyleChange.addActionListener(this);
     fontStyleChange.setBackground(new Color(190,203,255));

     pane.add(font);


     this.show();
  }//end constructor

  //some other methods used in the program
  //this is a menu to add items to the File menu
  private void setFileMenu()
  {
    fileM = new JMenu ("File");
    fileM.setMnemonic('F');
    menuMB.add(fileM);

    openI = new JMenuItem("Open");
    fileM.add(openI);
    openI.setAccelerator(KeyStroke.getKeyStroke('O', Event.CTRL_MASK));


    saveI = new JMenuItem("Save");
    fileM.add(saveI);
    saveI.setAccelerator(KeyStroke.getKeyStroke('S', Event.CTRL_MASK));

    exitI = new JMenuItem("Exit");
    fileM.add(exitI);
    exitI.setAccelerator(KeyStroke.getKeyStroke('E', Event.CTRL_MASK));

    //register a listeners for menu items
    openI.addActionListener(this);
    saveI.addActionListener(this);
    exitI.addActionListener(this);

  }//end setFileMenu

  private void setEditMenu()
  {
    editM = new JMenu("Edit");
    menuMB.add(editM);
    editM.setMnemonic('E');


    cutI = new JMenuItem("Cut");
    editM.add(cutI);
    cutI.setAccelerator(KeyStroke.getKeyStroke('D', Event.CTRL_MASK));


    copyI = new JMenuItem("Copy");
    editM.add(copyI);
    copyI.setAccelerator(KeyStroke.getKeyStroke('C', Event.CTRL_MASK));

    pasteI = new JMenuItem("Paste");
    editM.add(pasteI);
    pasteI.setAccelerator(KeyStroke.getKeyStroke('P', Event.CTRL_MASK));

    selectI = new JMenuItem("Select All");
    editM.add(selectI);
    selectI.setAccelerator(KeyStroke.getKeyStroke('L', Event.CTRL_MASK));

    cutI.addActionListener(this);
    copyI.addActionListener(this);
    pasteI.addActionListener(this);
    selectI.addActionListener(this);
  }//end setEditMenu

  public void setHelpMenu()
  {
    helpM = new JMenu("Help");
    menuMB.add(helpM);
    helpM.setMnemonic('H');

    aboutI= new JMenuItem("Text Editor");
    helpM.add(aboutI);
    aboutI.setAccelerator(KeyStroke.getKeyStroke('A', Event.CTRL_MASK));
    aboutI.addActionListener(this);
  }//end setHelpMenu

  public class OpenFile
  {
    public OpenFile()
    {
      //place code in a try block to handle exceptions
        JFileChooser chooser = new JFileChooser();
        int temp = chooser.showOpenDialog(null);
        File file = chooser.getSelectedFile();
        BufferedReader br = null;
        String one = "";
        String two = "";
        if(file!=null)
        {
          try
          {
            br = new BufferedReader(new FileReader(file));
          }//end try

          catch(Exception a)
          {
          }
          pageTA.setText("");
          try
          {
            while ((two = br.readLine()) != null)
            {
              try
              {
                pageTA.append(two);
              }//ent try
              catch(Exception d)
              {
                break;
              }//end catch
            }//end while
            br.close();
          }//end tty

          catch(IOException b)
          {
            try
            {
              br.close();
            }
            catch(IOException c)
            {

            }//end catch
          }//end catch
        }//end if
      }//end constructor
 }//end inner class

 public class SaveFile
{
   PrintWriter pw = null;
   JFileChooser chooser = new JFileChooser();
   int temp = chooser.showSaveDialog(null);
   File fileOne = chooser.getSelectedFile();

   public SaveFile()
   {
     try
     {
       pw = new PrintWriter(new FileOutputStream(fileOne), true);
       pw.write(pageTA.getText());
     }//end try
     catch (FileNotFoundException ex)
     {
       System.out.println("Catch message is: " + ex.getMessage());
     }//emd catch
     pw.close();

   }//end constructor

 }//end inner class



  public void actionPerformed(ActionEvent e)
  {
    JMenuItem mItem = (JMenuItem)e.getSource();

    if(mItem == exitI)
    {
      System.exit(0);
    }
    else if (mItem == openI)
    {
      OpenFile open = new OpenFile();
    }
    else if (mItem == saveI)
    {
      SaveFile save = new SaveFile();
    }
    else if (mItem == cutI)
    {
      scratchpad = pageTA.getSelectedText();
      pageTA.replaceRange("",pageTA.getSelectionStart(),
                            pageTA.getSelectionEnd());
    }
    else if (mItem == copyI)
    {
      scratchpad = pageTA.getSelectedText();
    }

    else if (mItem == pasteI)
    {
      pageTA.insert(scratchpad, pageTA.getCaretPosition());
    }

    else if (mItem == selectI)
    {
      pageTA.selectAll();
    }
    else if (mItem == aboutI)
    {
      JOptionPane.showMessageDialog(null,"Roman Kaminski");
    }
    else
    {
      JComboBox tempcb = (JComboBox)e.getSource();
      int fontS = Integer.parseInt(tempcb.getSelectedItem().toString());
    }
}//end actionPerformed



  public static void main(String[]args)
  {
     Project3 app1 = new Project3();
  }//end main

}//end class
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You have two immediate problems:

You don't check the type in your ActionListener(it may not be a menu item)
You get the font size incorrectly
Your combo boxes should have instance scope and should be interrogated in your action listener. A new font should be created based on the values of these boxes and set if not equal to the current one.
Avatar of Roman_Kaminski
Roman_Kaminski

ASKER

i dont understand what to do could u show me in that part of the code
No One IS Geting Points cause no one helped me fix this. but i will give out points to who ever answers this question:

who won the war between germania and ugoslovic in 1376???
ugoslovic
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
there was no war between germania and uglaslavic??? lol

thank you CEHJ i gave the points to u
:-)