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 3 of 3

Thread: Error "TitleChanger is abstract; cannot be instantiated"

  1. #1
    Junior Member
    Join Date
    May 2009
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Error "TitleChanger is abstract; cannot be instantiated"

    Hey, I'm fairly new to java, been reading sams teach yourself java 6 in 21 days.

    I ran into an error with a code in the day 12 section.

    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
     
    [COLOR="DarkRed"]public class TitleChanger extends JFrame implements ActionListener[/COLOR]
    {
    	JButton b1 = new JButton("Rosencrantz");
    	JButton b2 = new JButton("Guildenstern");
     
    	public TitleChanger()
    	{
    		super("Title Bar");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		b1.addActionListener(this);
    		b2.addActionListener(this);
    		FlowLayout flow = new FlowLayout();
    		setLayout(flow);
    		add(b1);
    		add(b2);
    		pack();
    		setVisible(true);
    	}
     
    	public void actionPreformed(ActionEvent evt)
    	{
    		Object source = evt.getSource();
    		if (source == b1)
    		{
    			setTitle("Rosencrantz");
    		}
    		else if (source == b2)
    		{
    			setTitle("Guildenstern");
    		}
    		repaint();
    	}
     
    	public static void main(String[] arguments)
    	{
    		TitleChanger frame = new TitleChanger();
    	}
    }

    Now whenever i try to compile this, it will say that TitleChanger is not abstract and cannot override abstract method actionPreformed(java.awt.event.ActionEvent).

    After i declare the main class as abstract however.

    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
     
    public abstract class TitleChanger extends JFrame implements ActionListener
    {
    	JButton b1 = new JButton("Rosencrantz");
    	JButton b2 = new JButton("Guildenstern");
     
    	public TitleChanger()
    	{
    		super("Title Bar");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		b1.addActionListener(this);
    		b2.addActionListener(this);
    		FlowLayout flow = new FlowLayout();
    		setLayout(flow);
    		add(b1);
    		add(b2);
    		pack();
    		setVisible(true);
    	}
     
    	public void actionPreformed(ActionEvent evt)
    	{
    		Object source = evt.getSource();
    		if (source == b1)
    		{
    			setTitle("Rosencrantz");
    		}
    		else if (source == b2)
    		{
    			setTitle("Guildenstern");
    		}
    		repaint();
    	}
     
    	public static void main(String[] arguments)
    	{
    		[COLOR="DarkRed"]TitleChanger frame = new TitleChanger();[/COLOR]
    	}
    }

    The compiler will say that TitleChanger is abstract; cannot be instantiated.
    TitleChanger frame = new TitleChanger();

    I am currently using Windows Vista, and was wondering if anyone might have a solution, or be able to point me to another source.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Abstract problem in java using vista

    Hello Uzual, welcome to the Java Programming Forums.

    You have made a very small mistake which has stopped the entire program from running properly.

    I'm sure you will kick yourself any minute now!!

    The first code you submitted was correct, except you misspelled the actionPerformed method.

    Change:

    public void actionPreformed(ActionEvent evt)

    to:

     public void actionPerformed(ActionEvent evt)

    Sorted!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

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

    Uzual (May 26th, 2009)

  4. #3
    Junior Member
    Join Date
    May 2009
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Abstract problem in java using vista

    aight thanks

Similar Threads

  1. Java GUI problem in paintComponent?
    By Richard_ in forum AWT / Java Swing
    Replies: 2
    Last Post: May 1st, 2009, 08:19 AM
  2. [SOLVED] JAVA JList Problem in visual images
    By antitru5t in forum AWT / Java Swing
    Replies: 4
    Last Post: April 15th, 2009, 03:09 PM
  3. Java program with abstract class along with two subclasses
    By crazydeo in forum Collections and Generics
    Replies: 2
    Last Post: June 10th, 2008, 11:45 AM