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

Thread: java Final Project Error

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java Final Project Error

    I cannot figure out why it isnt displaying the result when i click the proper button....I need this fixed by tomorrow please help!!!! Below is my code!
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    public class FinalProject extends JFrame implements ActionListener
    {
    	JLabel textPrompt;
    	JTextArea inputText;
    	JLabel keyPrompt;
    	JTextField inputKey;
    	Container container;
     
    	public FinalProject()
    	{
    		super( "Final Project " );
    		JTabbedPane tab = new JTabbedPane();
     
    		// constructing the Pool Panel
     
    		JPanel p1 = new JPanel();
    		JLabel label4,label5,lable6;
    		final JTextField text4,text5,text6,text13;
    		label4 = new JLabel();
    		label4.setText("Height:");
    		p1.add(label4);
    		text4 = new JTextField(20);
    		p1.add(text4);
    		JLabel space3 = new JLabel("                                 ");
    		p1.add(space3);
    		label5 = new JLabel();
    		label5.setText("Width:");
    		p1.add (label5);
    		text5 = new JTextField(20);
    		p1.add(text5);
    		JLabel space5 = new JLabel("                           ");
    		p1.add(space5);
    		lable6 = new JLabel();
    		lable6.setText("Depth:");
    		p1.add(lable6);
    		text6 = new JTextField(20);
    		p1.add(text6);
    		JLabel space2 = new JLabel(" ");
    		p1.add(space2);
    		JButton button = new JButton("Enter");
    		p1.add(button);
    		tab.addTab( "Pool", null, p1, " Pool" );
    		text13 = new JTextField();
    		p1.add(text13);
    		button.addActionListener(this);
     
    		new ActionListener(){
    			public void actionPerformed(ActionEvent e) {
    e.getActionCommand();
    				int length=Integer.parseInt(text4.getText());
    				int width=Integer.parseInt(text5.getText());
    				int depth=Integer.parseInt(text6.getText());
     
    				if (e.getActionCommand().equals("Enter")) {
    					int volume = ((length * width) * depth);
    					JOptionPane.showMessageDialog(null, "The Volume is:"+volume,"for your pool.", JOptionPane.INFORMATION_MESSAGE);
    				}
    				}
    		};
    // constructing the Hot Tube panel
     
    		new JLabel("Hot Tube");
    		JPanel p2 = new JPanel();
    		JLabel label7,label8,lable9;
    		final JTextField text7,text8,text9;{
    			label7 = new JLabel();
    			label7.setText("Height:");
    			p2.add(label7);
    			text7 = new JTextField(20);
    			p2.add(text7);
    			JLabel space6 = new JLabel("                       ");
    			p2.add(space6);
    			label8 = new JLabel();
    			label8.setText("Width:");
    			p2.add(label8);
    			text8 = new JTextField(20);
    			p2.add(text8);
    			JLabel space7 = new JLabel("                       ");
    			p2.add(space7);
    			lable9 = new JLabel();
    			lable9.setText("Depth:");
    			p2.add(lable9);
    			text9 = new JTextField(20);
    			p2.add(text9);
    			JLabel space8 = new JLabel("              ");
    			p2.add(space8);
    			JButton rbutton = new JButton("Oval");
    			p2.add(rbutton);
    			JButton rbutton1 = new JButton ("Round");
    			p2.add(rbutton1);
     
    			tab.addTab( "Hot Tube", null, p2, " Hot Tube" );
    			rbutton.addActionListener(this);
    			rbutton1.addActionListener(this);
     
    			new ActionListener(){
    				public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
     
    					int length=Integer.parseInt(text7.getText());
    					int width=Integer.parseInt(text8.getText());
    					int depth=Integer.parseInt(text9.getText());
     
    					if(e.getActionCommand().equals("Oval")){
    						int volume = ((length * width) * depth);
    						JOptionPane.showMessageDialog(null, "The Volume is:"+volume,"" +
    								"for your oval hot tube.", JOptionPane.INFORMATION_MESSAGE);
    					}
    					else if(e.getActionCommand().equals("Round")){
    						int volume1= (((length/2)^2) * depth);
    						JOptionPane.showMessageDialog(null, "The Volume is:"+volume1,"" +
    								"for your round hot tube.", JOptionPane.INFORMATION_MESSAGE);
    					}
    				}
    			};
    // constructing the Temperature Change panel
     
    			new JLabel( " Temperature Change!" );
    			JPanel p3 = new JPanel();
    			JLabel label10;
    			final JTextField text10;
    			label10 = new JLabel();
    			label10.setText("Temperature:");
    			p3.add(label10);
    			text10 = new JTextField(20);
    			p3.add(text10);
    			JButton button3 = new JButton("Enter");
    			p3.add(button3);
    			JCheckBox boxF = new JCheckBox("F");
    			p3.add(boxF);
    			JCheckBox boxC = new JCheckBox("C");
    			p3.add(boxC);
    			tab.addTab( "Temperature Change", null, p3, " Temperature Change" );
    			button3.addActionListener(this);
    			boxF.addActionListener(this);
    			boxC.addActionListener(this);
     
    			new ActionListener(){	
    				public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    					int temperature=Integer.parseInt(text10.getText());
     
    					if(e.getActionCommand().equals("C")){
    						int temp = (( temperature* 9)/5+32);
    						JOptionPane.showMessageDialog(null, "The Celcuis temperature is changed to:"
    								+temp,"" +
    								"in fahrenheit", JOptionPane.INFORMATION_MESSAGE);
    					}
    					else if(e.getActionCommand().equals("F")){
    						int temp1= (((temperature - 32)/9)*5);
    						JOptionPane.showMessageDialog(null, "The farhrenheight temperature is changed to:"+temp1,""+
    								"in Celcius.", JOptionPane.INFORMATION_MESSAGE);
    					}
    				}
    			};
    // constructing the Customer panel
     
    			JPanel p4 = new JPanel();
    			JLabel label1,label2;
    			final JTextField text1,text2;
    			label1 = new JLabel();
    			label1.setText("Name:");
    			p4.add(label1);
    			text1 = new JTextField(20);
    			p4.add(text1);
    			JLabel space = new JLabel("                  ");//MDB
    			p4.add(space);
    			label2 = new JLabel();
    			label2.setText("Address:");
    			p4.add(label2);
    			text2 = new JTextField(20);
    			p4.add(text2);
    			JButton button4 = new JButton("Enter");
    			p4.add(button4);
    			tab.addTab( "Customer", null, p4, " Customer" );
    			button4.setActionCommand ("Enter");
     
    			new ActionListener(){
    				public void actionPerformed(ActionEvent e) {
    e.getActionCommand();
    					int Name=Integer.parseInt(text1.getText()) ;
    					int Address=Integer.parseInt(text2.getText());
    					if (e.getActionCommand().equals("Enter")) {	
    						JOptionPane.showMessageDialog(null, "Customer's name is:"+Name,"Their address is:"
    								+Address, JOptionPane.INFORMATION_MESSAGE);
    					}
    				}
    			};
    // constructing the General panel
     
    			new JLabel( "General" );
    			JPanel p5 = new JPanel();
    			Date date = new Date();
    			SimpleDateFormat dateformatMMDDYYYY = new SimpleDateFormat("MM/dd/yyyy");
    			new StringBuilder( dateformatMMDDYYYY.format( date ) );
    			JLabel space11 = new JLabel(" ");
    			p5.add(space11);
    			JLabel space12 = new JLabel(" ");
    			p5.add(space12);
    			JLabel space13 = new JLabel(" ");
    			p5.add(space13);
    			JLabel label11;
    			label11 = new JLabel();
    			label11.setText("Today's Date is:");
    			p5.add(label11);	
    			JLabel texta1;
    			texta1 = new JLabel();
    			texta1.setText(dateformatMMDDYYYY.format(date));
    			p5.add(texta1);
    			tab.addTab( "General", null, p5, " Today's Date");
     
    			//constructing the Manufacture panel
     
    			JPanel p6 = new JPanel();
    			JLabel label3;
    			final JTextField text3;
    			label3 = new JLabel();
    			label3.setText("Manufacture:");
    			p6.add(label3);
    			text3 = new JTextField(20);
    			p6.add(text3);
    			JLabel space1 = new JLabel(" ");
    			p6.add(space1);
    			tab.addTab( "Manufacture", null, p6, " Manufacture" );
    			JButton button6 = new JButton("Enter");
    			p6.add(button6);
    			button6.addActionListener(this);
    			{
    				button6.setActionCommand ("Enter");
     
    				new ActionListener(){
    					public void actionPerformed(ActionEvent e) {
    e.getActionCommand();
    						int Manufacture=Integer.parseInt(text3.getText()) ;
     
    						if (e.getActionCommand().equals("Enter")) {
    							JOptionPane.showMessageDialog(null, "Manufacture's name is:"
    									+Manufacture,"", JOptionPane.INFORMATION_MESSAGE);
    						}
    					}
    				};
    //constructing the Comment panel
     
    				new JLabel("Customer Comments?");
    				JPanel p7 = new JPanel();
    				new JPanel( );
    				textPrompt = new JLabel("Customer comments:");
    				inputText = new JTextArea(5, 15);
    				inputText.setLineWrap(true);
    				inputText.setWrapStyleWord(true);
     
    //add components to window
    				container = getContentPane();
    //use borderlayout to position our two i/o panels
    				container.setLayout(new BorderLayout());
    				p7.setLayout(new FlowLayout());
    //add components to panels
    				p7.add(textPrompt);
    				p7.add(inputText);
    //add panels to our primary borderlayout
    				add(p7, BorderLayout.NORTH);
    // add JTabbedPane to container
    				getContentPane().add( tab );
    				setSize( 400, 250 );
    				setVisible( true );
    				JButton button7 = new JButton("Enter");
    				p7.add(button7);
    				tab.addTab ("Customer Comments?",null,p7,"Customer Comments?");
    				button7.addActionListener(this);}
     
    			new ActionListener(){
    				public void actionPerformed(ActionEvent e) {
    e.getActionCommand();
    					int Comments=Integer.parseInt(inputText.getText()) ;
     
    					if (e.getActionCommand().equals("Enter")) {
    						JOptionPane.showMessageDialog(null, "Customer's comment:"+Comments,"", JOptionPane.INFORMATION_MESSAGE);
    					}
    				}
     
    			};
    		}
    	}
    @Override
    	public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    	}
     
    	public static void main( String args[] )
    	{
    		FinalProject demo = new FinalProject();
    		demo.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    	}
    }
    Last edited by copeg; August 21st, 2010 at 07:35 PM. Reason: Please use the code tags


  2. #2
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: java Final Project Error

    Surround your code in [highlight=java][/highlight] it makes your code alot easier to read!

  3. #3
    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: java Final Project Error

    The way the code is currently written, you are adding an empty action listener to your elements ('this' aka FinalProject class has an empty implementation of ActionListener), so all those new ActionListener statements don't do anything because they are not added as listeners to your gui elements. Try creating inner or anonymous classes and add those to the buttons
    //anonymous 
    button.addActionListener(new ActionListener(){
        public void actionListener(ActionEvent e){
     
        }
    });
    //inner class
    private class ButtonExample implements ActionListener{
        public void actionPerformed(ActionEvent e){
     
        }
    }
    ....
    button.addActionListener(new ButtonExample());
    Last edited by copeg; August 21st, 2010 at 07:43 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java Final Project Error

       new ActionListener(){
                public void actionPerformed(ActionEvent e) {

    This code creates a listener object that isn't used???

  5. #5
    Junior Member
    Join Date
    Aug 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java Final Project Error

    How would I go about doing that???? Sorry I have been wracking my brain all day and now i am like brain dead

  6. #6
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: java Final Project Error

    Do what copeg said. Make another class.

    Example class
    private class ButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
          //Code here
        }
    }

    and instead of typing
    button.addActionListener(this);

    type:
    button.addActionListener(new ButtonListener());

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java Final Project Error

    Do you see which actionPerformed method is being executed?
    If not add a println() to all of them with different messages, compile and execute the code, press the button and see which method is called: System.out.println("aP=" + e); // shown the event for this action

    You have many orphaned ActionListeners that need to be added to a button. I asked about them in my post#4
    Last edited by Norm; August 21st, 2010 at 08:17 PM.

  8. #8
    Junior Member
    Join Date
    Aug 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java Final Project Error

    None of them are working... the only panel that is working is the date and I didnt use actionperformed method for that

  9. #9
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: java Final Project Error

    Post an updated version of your code.

  10. #10
    Junior Member
    Join Date
    Aug 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java Final Project Error

    That is... I dont know where I am putting the code the copeg said to place in mine.... I have so much code and I really dont want to mess it up.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java Final Project Error

    You can connect some of the ActionListeners by changing this:
      button.addActionListener(this);
     
            new ActionListener(){
    to this:
      button.addActionListener(
     
            new ActionListener(){
    and add a needed ) at the end of the ActionListener definition.

    The code change makes the following ActionListener the actionlistener vs having the main class (this) be the action listener.

    Did you add this statement to your actionPerformed() method:
    System.out.println("aP=" + e); // shown the event for this action

    It will show you when a button is pressed and what button was pressed
    Last edited by Norm; August 21st, 2010 at 08:22 PM.

  12. #12
    Junior Member
    Join Date
    Aug 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java Final Project Error

    When I take away the (this) it creates errors

  13. #13
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: java Final Project Error

    Are you sure that you're doing exactly what Norm is suggesting?

  14. #14
    Junior Member
    Join Date
    Aug 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java Final Project Error

    yes... exactly the same

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java Final Project Error

    it creates errors
    Please copy and paste the full text of the error here. It saves a lot of guessing

  16. #16
    Junior Member
    Join Date
    Aug 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java Final Project Error

    The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments ()

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java Final Project Error

    Please copy and paste the full text of the error here. You have left out some of the important info in the message!!!

    Did you look at what I posted?????
    Go look again. I did NOT put the ) beside the (.
    The ) must go at the end. For example:
           button.addActionListener(
     
            new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    e.getActionCommand();
                    int length=Integer.parseInt(text4.getText());
                    int width=Integer.parseInt(text5.getText());
                    int depth=Integer.parseInt(text6.getText());
     
                    if (e.getActionCommand().equals("Enter")) {
                        int volume = ((length * width) * depth);
                        JOptionPane.showMessageDialog(null, "The Volume is:"+volume,"for your pool.",                     JOptionPane.INFORMATION_MESSAGE);
                    }
                    }
            }[COLOR="Red"])[/COLOR];

    That is how you create an anonymous class for the listener.
    Last edited by Norm; August 21st, 2010 at 09:18 PM.

  18. #18
    Junior Member
    Join Date
    Aug 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java Final Project Error

    OH MY GOD!!!!! i just notice what i did.... I placed the ) after the ; instead of before it!! I am sorry!! I worked!! THank yoU!

  19. #19
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: java Final Project Error

    Quote Originally Posted by JavaStudent_09 View Post
    OH MY GOD!!!!! i just notice what i did.... I placed the ) after the ; instead of before it!! I am sorry!! I worked!! THank yoU!
    Which means you didn't copy him exactly.

    If your problem is solved you can now click on: Thread tools>mark this thread as solved
    Last edited by Brt93yoda; August 21st, 2010 at 09:33 PM.

Similar Threads

  1. Replies: 2
    Last Post: August 1st, 2010, 06:29 AM
  2. [SOLVED] I Need some help for a java project
    By george in forum Algorithms & Recursion
    Replies: 8
    Last Post: April 24th, 2010, 11:12 AM
  3. Java project
    By monika88a in forum AWT / Java Swing
    Replies: 5
    Last Post: January 20th, 2010, 12:10 AM
  4. static final object
    By kalees in forum Object Oriented Programming
    Replies: 3
    Last Post: December 21st, 2009, 12:29 PM
  5. final class, final <variable> or <data member>
    By chronoz13 in forum Object Oriented Programming
    Replies: 9
    Last Post: September 20th, 2009, 08:19 AM