Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Type mismatch: cannot convert from void to JTextField

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    29
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Type mismatch: cannot convert from void to JTextField

    my gui class:
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.JPasswordField;
    import javax.swing.JOptionPane;
    import javax.swing.JButton;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JOptionPane;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
     
    public class GUI extends JFrame{
     
    	private JTextField tf;
    	private Font pf;
    	private Font bf;
    	private Font itf;
    	private Font biff;
    	private JRadioButton pb;
    	private JRadioButton bb;
    	private JRadioButton ib;
    	private JRadioButton bib;
    	private ButtonGroup group;
     
     
    	public GUI(){
    		super("The Title");
    		setLayout(new FlowLayout());
     
    		tf= new JTextField("Nikolai is awesome", 25);
    		add(tf);
     
    		pb=new JRadioButton("plain", true);
    		bb=new JRadioButton("bold", false);
    		ib=new JRadioButton("italic", false);
    		bib=new JRadioButton("bold and italic", false);
    		add(pb);
    		add(bb);
    		add(ib);
    		add(bib);
     
    		group=new ButtonGroup();
    		group.add(pb);
    		group.add(bb);
    		group.add(ib);
    		group.add(bib);
     
     
    		pf=new Font("Serif", Font.PLAIN, 14);
    		bf=new Font("Serif", Font.BOLD, 14);
    		itf=new Font("Serif", Font.ITALIC, 14);
    		biff=new Font("Serif", Font.BOLD + Font.ITALIC, 14);
    		tf=setFont(pf);//<--this is the line reference for GUI line 58
     
    		pb.addItemListener(new HandlerClass(pf));
    		bb.addItemListener(new HandlerClass(bf));
    		ib.addItemListener(new HandlerClass(itf));
    		bib.addItemListener(new HandlerClass(biff));
    	}
    	private class HandlerClass implements ItemListener{
    		private Font font;
     
    		public HandlerClass(Font f){
    			font=f;
    		}
     
    		public void itemStateChanged(ItemEvent event){
    			tf.setFont(font);
    		}
    	}
     
    }

    eclipse error message:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    	Type mismatch: cannot convert from void to JTextField
     
    	at GUI.<init>(GUI.java:58)
    	at Apples.main(Apples.java:7)

    A fix and explanation would be much appreciated. I copied this from a thenewboston tutorial and he uses an old version of java but im pretty sure ive copied character for character!
    Last edited by nikolaiL; June 9th, 2014 at 12:30 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Type mismatch: cannot convert from void to JTextField

    For issues like this, one of the best places to consult is the API. The line where the error occurs calls setFont for JFrame (which is a parent method of Container):
    Container (Java Platform SE 7 )
    Looking at the API docs page above - what does this method return? Does your code deal properly with the returned value (if any is returned)?

  3. The Following User Says Thank You to copeg For This Useful Post:

    nikolaiL (June 9th, 2014)

  4. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Type mismatch: cannot convert from void to JTextField

    Well, the error message gives all away, void can not be converted to JTextField.
    This is the evil line, right here:
    tf=setFont(pf);
    What it does:
    You have a variable called "tf", this variable can reference objects of type "JTextField". You try to assign the value returned by a call to the method "setFont(...)" to your variable "tf".
    The method "setFont(...)" is called on your GUI object and returns "void" (that means it returns nothing). Since you try to assign "void" to "tf" and "tf" can only hold objects of type "JTextField" the compiler gives you an error.

    What you want is either:
    setFont(pf);
    or
    tf.setFont(pf);

    Maybe play around with this a little and see what gives you the desired effect.

  5. The Following User Says Thank You to Cornix For This Useful Post:

    nikolaiL (June 9th, 2014)

  6. #4
    Junior Member
    Join Date
    May 2014
    Posts
    29
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Type mismatch: cannot convert from void to JTextField

    *facepalm* should have used a dot seperator oops. anyway thanks for the help guys!!

Similar Threads

  1. type mismatch problem
    By thegorila78 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 14th, 2013, 12:55 PM
  2. Type mismatch assigning factory class result to subclass
    By roger1990 in forum Object Oriented Programming
    Replies: 5
    Last Post: July 28th, 2013, 11:23 PM
  3. Type mismatch: cannot convert from void to ClassX.MethodX
    By RevMoses77 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 16th, 2012, 12:05 PM
  4. [SOLVED] Cannot Convert from Void to JTextField
    By StandbyExplosion in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 24th, 2011, 04:56 PM