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.wid th/4, wndSize.height/4,
wndSize.width/2, wndSize.height/2);
setFileMenu();
setEditMenu();
setHelpMenu();
this.setJMenuBar(menuMB);
setDefaultCloseOperation(E XIT_ON_CLO SE);
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.addActionListen er(this);
font.add(fontChange);
fontChange.setBackground(n ew 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.addActionL istener(th is);
fontStyleChange.setBackgro und(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(KeySt roke.getKe yStroke('O ', Event.CTRL_MASK));
saveI = new JMenuItem("Save");
fileM.add(saveI);
saveI.setAccelerator(KeySt roke.getKe yStroke('S ', Event.CTRL_MASK));
exitI = new JMenuItem("Exit");
fileM.add(exitI);
exitI.setAccelerator(KeySt roke.getKe yStroke('E ', Event.CTRL_MASK));
//register a listeners for menu items
openI.addActionListener(th is);
saveI.addActionListener(th is);
exitI.addActionListener(th is);
}//end setFileMenu
private void setEditMenu()
{
editM = new JMenu("Edit");
menuMB.add(editM);
editM.setMnemonic('E');
cutI = new JMenuItem("Cut");
editM.add(cutI);
cutI.setAccelerator(KeyStr oke.getKey Stroke('D' , Event.CTRL_MASK));
copyI = new JMenuItem("Copy");
editM.add(copyI);
copyI.setAccelerator(KeySt roke.getKe yStroke('C ', Event.CTRL_MASK));
pasteI = new JMenuItem("Paste");
editM.add(pasteI);
pasteI.setAccelerator(KeyS troke.getK eyStroke(' P', Event.CTRL_MASK));
selectI = new JMenuItem("Select All");
editM.add(selectI);
selectI.setAccelerator(Key Stroke.get KeyStroke( 'L', Event.CTRL_MASK));
cutI.addActionListener(thi s);
copyI.addActionListener(th is);
pasteI.addActionListener(t his);
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(KeyS troke.getK eyStroke(' A', Event.CTRL_MASK));
aboutI.addActionListener(t his);
}//end setHelpMenu
public class OpenFile
{
public OpenFile()
{
//place code in a try block to handle exceptions
JFileChooser chooser = new JFileChooser();
int temp = chooser.showOpenDialog(nul l);
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(nul l);
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(ActionEven t 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("",pag eTA.getSel ectionStar t(),
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.showMessageDia log(null," Roman Kaminski");
}
else
{
JComboBox tempcb = (JComboBox)e.getSource();
int fontS = Integer.parseInt(tempcb.ge tSelectedI tem().toSt ring());
}
}//end actionPerformed
public static void main(String[]args)
{
Project3 app1 = new Project3();
}//end main
}//end class
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.wid
wndSize.width/2, wndSize.height/2);
setFileMenu();
setEditMenu();
setHelpMenu();
this.setJMenuBar(menuMB);
setDefaultCloseOperation(E
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.addActionListen
font.add(fontChange);
fontChange.setBackground(n
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.addActionL
fontStyleChange.setBackgro
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(KeySt
saveI = new JMenuItem("Save");
fileM.add(saveI);
saveI.setAccelerator(KeySt
exitI = new JMenuItem("Exit");
fileM.add(exitI);
exitI.setAccelerator(KeySt
//register a listeners for menu items
openI.addActionListener(th
saveI.addActionListener(th
exitI.addActionListener(th
}//end setFileMenu
private void setEditMenu()
{
editM = new JMenu("Edit");
menuMB.add(editM);
editM.setMnemonic('E');
cutI = new JMenuItem("Cut");
editM.add(cutI);
cutI.setAccelerator(KeyStr
copyI = new JMenuItem("Copy");
editM.add(copyI);
copyI.setAccelerator(KeySt
pasteI = new JMenuItem("Paste");
editM.add(pasteI);
pasteI.setAccelerator(KeyS
selectI = new JMenuItem("Select All");
editM.add(selectI);
selectI.setAccelerator(Key
cutI.addActionListener(thi
copyI.addActionListener(th
pasteI.addActionListener(t
selectI.addActionListener(
}//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(KeyS
aboutI.addActionListener(t
}//end setHelpMenu
public class OpenFile
{
public OpenFile()
{
//place code in a try block to handle exceptions
JFileChooser chooser = new JFileChooser();
int temp = chooser.showOpenDialog(nul
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(nul
File fileOne = chooser.getSelectedFile();
public SaveFile()
{
try
{
pw = new PrintWriter(new FileOutputStream(fileOne),
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(ActionEven
{
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("",pag
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.showMessageDia
}
else
{
JComboBox tempcb = (JComboBox)e.getSource();
int fontS = Integer.parseInt(tempcb.ge
}
}//end actionPerformed
public static void main(String[]args)
{
Project3 app1 = new Project3();
}//end main
}//end class
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.
ASKER
i dont understand what to do could u show me in that part of the code
ASKER
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???
who won the war between germania and ugoslovic in 1376???
ugoslovic
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
there was no war between germania and uglaslavic??? lol
thank you CEHJ i gave the points to u
thank you CEHJ i gave the points to u
:-)
You don't check the type in your ActionListener(it may not be a menu item)
You get the font size incorrectly