Results 1 to 3 of 3
  1. #1
    since'50 is offline Member
    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    Default Help with changing of font when pressing JButton

    Hello,

    I am having some troubles with a program fileviewer where you must be able to change font style and size when pressing JButton "Set font". I have solved the most of the program and every menu button is working but I don't know how to set in FileListener class method to change the font when running in main class. So if someone could help me I would be grateful.


    Anyway, this is my FileListener class
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
    import java.awt.Font;
    
    public class FileListener implements ActionListener
    {
    	//attributes
    	private JTextField jtf;
    	private JTextArea area;
    	private JFileChooser fileChoose;
    	Font font;
    	int style= Font.PLAIN;
    	JComboBox fstyle = new JComboBox();
    	JComboBox fsize = new JComboBox();
    
    
    
    
    	public FileListener(JTextField jtfFile, JTextArea area, JComboBox fstyle, JComboBox fsize)
    	{
    		fileChoose = new JFileChooser();
    		this.jtf = jtf;
    		this.area = area;
    		this.fstyle = fstyle;
    		this.fsize = fsize;
    			}
    	public void actionPerformed(ActionEvent ae)
    	{
    		 String cmd = ae.getActionCommand();
          
    		if(cmd.equals("New"))
    		{
    				area.setText("");
    				//jtf.setText("");
    		}
    				 
    		 
    	
    		if(cmd.equals("Open..."))
    		{
    			int result = fileChoose.showOpenDialog(area);
    			File file = fileChoose.getSelectedFile();
    
    			if(file != null && result == JFileChooser.APPROVE_OPTION)
    			{
    				try
    				{
    					BufferedReader in = new BufferedReader(new FileReader(file));
    					String line = null;
    					while ( (line = in.readLine()) != null)
    					{	
    						area.append(line + "\n");
    					}
    				}
    				catch(IOException ioe)
    				{
    				JOptionPane.showMessageDialog( null,"IO Exception!");
    				}
    				
    			}	
    		}//end if cmd equals open
    	
    	
    		if(cmd.equals("Save"))
    		{
    			
    		}//end if cmd equals save
    	
    	
    	
    		if(cmd.equals("Save As..."))
    			 {
    			 	int result = fileChoose.showSaveDialog(area);
    				File file = fileChoose.getSelectedFile();
    				if(file != null && result == JFileChooser.APPROVE_OPTION)
    				try
    					{
    					BufferedWriter out = new BufferedWriter(new FileWriter(file));
    					out.write(area.getText());
    					out.close();
    					}
    				catch(IOException ioe)
    					{	
    					JOptionPane.showMessageDialog( null,"IO Exception!");
    					}
    			}//end if cmd equals saveas
    
    	
    	
    		if(cmd.equals("Word Count"))
    		 {
    					String myText = area.getText();
    					Scanner sc = new Scanner(myText);
    		 
    					int counter = 0;
    					String words;
    					String storedLine;
    		 
    					while (sc.hasNextLine()) 
    					{
    						storedLine = sc.nextLine();
    						Scanner line = new Scanner(storedLine);
    		 
    						while (line.hasNext()) 
    						{
    							words = line.next();
    							counter++;
    						}
    						
    						JOptionPane.showMessageDialog(null, "Word Count: "+ counter);
    		 
    				}
    				
    				//int wCount = jtaText.getText().length() - jtaText.getText().replaceAll(" ","").length(); 
    				//JOptionPane.showMessageDialog( null,"Found " + wCount + " Words");
    				//JOptionPane.showMessageDialog(null, "" + "" + "Word Count is " , "Word Count", JOptionPane.INFORMATION_MESSAGE);
    			
    			}//end if cmd equals word count
    	   
    		
    	
    		if(cmd.equals("Bold"))
    		{
    			style = Font.BOLD;
    			
    			font = new Font("SansSerif",style, 20);
    			 area.setFont(font);
    		}//end if cmd equals Bold
    		
    	
    	
    	
    		if(cmd.equals("Italic"))
    		{
    			style = Font.ITALIC;
    			font = new Font("SansSerif", style, 10);
    		}//end if cmd equals Italic
    		
    		
    		
    		if(cmd.equals("Italic and Bold"))
    		{
    			style = (Font.ITALIC + Font.BOLD);
    			font = new Font("SansSerif", style, 10);
    		}//end if cmd equals Italic and Bold
    		
    		
    		if(cmd.equals("10"))
    		{	
    			font = new Font("SansSerif", style, 10);
    		}//end if cmd equals 10
    	
    	
    		if(cmd.equals("12"))
    		{
    			font = new Font("SansSerif", style, 12);
    		}//end if cmd equals 12
    		
    		
    		if(cmd.equals("15"))
    		{
    			Font font = new Font("SansSerif", style, 15);
    		}//end if cmd equals 15
    		
    		
    		if(cmd.equals("Set Font"))
    		{	
    			
    			area.setFont(font);
    		}//end if cmd equals Set Font*/
    		
    				
    		
    		
    		
    		if(cmd.equals("Exit"))
    		{
    			System.exit(0);
    		}//end if cmd equals exit
    	
    	
    	}
    
    }
    
    
    
    
    and this is my main FileViewer class bellow
    
    
    import java.awt.Font;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    
    
    public class FileViewer extends JFrame 
    {
    	//atributes
    	private JTextField jtf;
    	private JTextArea area;
    	private JButton button;
    	private JLabel filename;
    	private JComboBox fstyle;
    	private JComboBox fsize;
    	
    	@SuppressWarnings("unchecked")
    	
    	public FileViewer()
    	{
    		 setTitle("FileViewer");
    		 setSize(700,500);
    		
    		 jtf = new JTextField("",20);
    		 jtf.setEditable(false);
    		 area = new JTextArea(25,45);
    		 area.setLineWrap(true);
    		
    		 filename = new JLabel("File Name: ");
    		
    		 button = new JButton("Set Font");
    		 
    		
    		 FileListener fl = new FileListener(jtf,area,fstyle,fsize);
    		
    		 JPanel jp1 = new JPanel();
    		 JPanel jp2 = new JPanel();
    		 JPanel jp3 = new JPanel();
    		
    		
    		 jp1.setLayout(new GridLayout(12,2));
    		
    		 JScrollPane jsp = new JScrollPane(area);
    		 jp2.add(jsp);
    		
    		 jp3.add(filename);
    		 jp3.add(jtf);
    		
    		
    		 add(jp1, "East");
    		 add(jp2, "Center");
    		 add(jp3, "South");
    		
    		
    		
    		 JComboBox fstyle = new JComboBox();
    		 //fstyle.setPreferredSize(new Dimension(50,20));
    		 fstyle.setPrototypeDisplayValue("Italic and Bold"); 
    		 
    		 fstyle.addItem("Regular");
           fstyle.addItem("Bold");
           fstyle.addItem("Italic");
           fstyle.addItem("Italic and Bold");
    		 
    		 
    		 fsize = new JComboBox();
    		 fsize.addItem("10");
           fsize.addItem("12");
           fsize.addItem("15");
                     
           button.addActionListener(fl);
           fstyle.addActionListener(fl);
           fsize.addActionListener(fl);
                     
           jp1.add(fstyle);
           jp1.add(fsize);
           jp1.add(button);
     
           //menus
           JMenuBar jmb = new JMenuBar();
           setJMenuBar(jmb);
     
           JMenu jmFile = new JMenu("File");
           jmFile.setMnemonic('F');
           jmb.add(jmFile);
     
           JMenuItem jmiNew = new JMenuItem("New");
           jmiNew.setMnemonic('N');
           jmFile.add(jmiNew);
     
           JMenuItem jmiOpen = new JMenuItem("Open...");
           jmiOpen.setMnemonic('O');
           jmFile.add(jmiOpen);
                     
           JMenuItem jmiSave = new JMenuItem("Save");
           jmiSave.setMnemonic('S');
           jmFile.add(jmiSave);
                     
           JMenuItem jmiSaveAs = new JMenuItem("Save As...");
           jmiSaveAs.setMnemonic('K');
           jmFile.add(jmiSaveAs);
     
           JMenuItem jmiExit = new JMenuItem("Exit");
           jmiOpen.setMnemonic('X');
           jmFile.add(jmiExit);
                     
           JMenu jmTools = new JMenu("Tools");
           jmTools.setMnemonic('T');
           jmb.add(jmTools);
                     
           JMenuItem jmiWord = new JMenuItem("Word Count");
           jmTools.add(jmiWord);
    		 jmiWord.setMnemonic('W');
                     
           JMenu jmHelp = new JMenu("Help");
           jmHelp.setMnemonic('H');
           jmb.add(jmHelp);
                   
    	    JMenuItem jmiAbout = new JMenuItem("About FileViewer");
    		 jmiAbout.setMnemonic('V');
           jmHelp.add(jmiAbout);
                     
           HelpListener hl = new HelpListener(jmiAbout);          
                     
           jmiNew.addActionListener(fl);
           jmiAbout.addActionListener(hl);
           jmiSave.addActionListener(fl);
           jmiSaveAs.addActionListener(fl);
           jmiWord.addActionListener(fl);
           jmiOpen.addActionListener(fl);
           jmiExit.addActionListener(fl);
                     
           setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           
           setVisible(true);
    		
    		
    	}
    	
    	public static void main(String []args) 
    		{
    	       new FileViewer();
    	   }
    
    
    }//end class

    Someone please help !
    Last edited by JosAH; 02-15-2014 at 06:36 PM. Reason: added [code] ... [/code] tags

  2. #2
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: Help with changing of font when pressing JButton

    Can you wrap your code in [CODE] tags so there is syntax highlighting and correct indentation?

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help with changing of font when pressing JButton

    Quote Originally Posted by pj6444 View Post
    Can you wrap your code in [CODE] tags so there is syntax highlighting and correct indentation?
    Consider it done.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Changing the font size in java GUI
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 1
    Last Post: 11-16-2011, 04:59 PM
  2. ActionListener for JButton after changing Button to JButton
    By ravi.joshi53 in forum Java Applets
    Replies: 2
    Last Post: 10-07-2011, 07:35 AM
  3. Changing the font of Message box
    By venkateshcoolmoon in forum AWT / Swing
    Replies: 2
    Last Post: 03-16-2010, 05:18 AM
  4. Changing println font color
    By xcallmejudasx in forum New To Java
    Replies: 4
    Last Post: 05-21-2009, 04:05 PM
  5. changing font size
    By diggitydoggz in forum New To Java
    Replies: 1
    Last Post: 12-25-2008, 07:48 AM

Posting Permissions

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