• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1302
  • Last Modified:

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
0
Roman_Kaminski
Asked:
Roman_Kaminski
  • 4
  • 3
1 Solution
 
CEHJCommented:
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
0
 
CEHJCommented:
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.
0
 
Roman_KaminskiAuthor Commented:
i dont understand what to do could u show me in that part of the code
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Roman_KaminskiAuthor Commented:
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???
0
 
zzynxSoftware engineerCommented:
ugoslovic
0
 
CEHJCommented:
I've put in a few fixes:

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;
  private JComboBox fontChange;
  private JComboBox fontStyleChange;
  private int currentFontSize;
  private String currentFontName;
  private Font currentFont;





  //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"};
     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"};
     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)
  {
    if (e.getSource() instanceof JMenuItem)
    {
      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();
      if (tempcb == fontChange)
      {
        currentFontSize = Integer.parseInt(tempcb.getSelectedItem().toString());
      }
      else
      {
        currentFontName = tempcb.getSelectedItem().toString();
      }
      Font f = new Font(currentFontName, Font.PLAIN, currentFontSize);
      if (f.equals(currentFont) == false)
      {
        currentFont = f;
        setEachFont(this);
      }
    }
}//end actionPerformed

public void setAllFonts(Font f)
{
  Component[] comps = getContentPane().getComponents();
  for(int i = 0;i < comps.length;i++)
  {
    comps[i].setFont(f);
  }
  //fontStyleChange.setFont(f);

}

public void setEachFont(Component c) {
  if (c instanceof Container)
  {
    Component[] comps = ((Container)c).getComponents();
    for(int i = 0;i < comps.length;i++)
    {
      setEachFont(comps[i]);
    }
    c.setFont(currentFont);
  }
  else
  {
    c.setFont(currentFont);
  }
}



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

}//end class
0
 
Roman_KaminskiAuthor Commented:
there was no war between germania and uglaslavic??? lol

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

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now