Solved

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

Posted on 2003-12-03
8
1,267 Views
Last Modified: 2013-12-03
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
Comment
Question by:Roman_Kaminski
  • 4
  • 3
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9867106
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 9867139
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
 

Author Comment

by:Roman_Kaminski
ID: 9867174
i dont understand what to do could u show me in that part of the code
0
 

Author Comment

by:Roman_Kaminski
ID: 9867421
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 37

Expert Comment

by:zzynx
ID: 9867940
ugoslovic
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 9867957
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
 

Author Comment

by:Roman_Kaminski
ID: 9868067
there was no war between germania and uglaslavic??? lol

thank you CEHJ i gave the points to u
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9868176
:-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now