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

Thread: Can someone help me out

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can someone help me out

    hey, I am fairly new to Java and I have to write a program that uses the mvc design pattern. It calculates the maximum data rate when the user changes the signal to noise and bandwidth. I need to create an action listener for when the user changes the values but I am not sure how to do that.

    private JPanel createBandwidthPanel(){
    	JPanel bandwidthPanel = new JPanel();
    	bandwidthPanel.setLayout(new BoxLayout(bandwidthPanel, BoxLayout.LINE_AXIS));
    	JLabel bandwidthLBL = new JLabel("Bandwidth(in hertz):");
    	JTextField bandwidthTXTFLD = new JTextField();
     
    	bandwidthTXTFLD.addActionListener(new ActionListener(){
     
     
    	public void actionPerformed(ActionEvent e) {
    		Double.parseDouble(bandwidthTXTFLD.getText());
     
     
    	}
    	});
     
    	bandwidthPanel.add(bandwidthLBL);
    	bandwidthPanel.add(bandwidthTXTFLD);
     
    	return bandwidthPanel;
    }

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Can someone help me out

    You've already written an ActionListener. I'm not sure what your question is? I'd start by adding a print statement in there at least.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can someone help me out

    it's complaining that the txtfield should be final and I don't think it's supposed to be.

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

    Default Re: Can someone help me out

    variables defined/declared inside of a method go away when the method exits. Use of final tells the jvm to keep their value around so code like in a listener can access them.
    Or move their definition out of the method and it should work.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Can someone help me out

    Quote Originally Posted by Norm View Post
    variables defined/declared inside of a method go away when the method exits. Use of final tells the jvm to keep their value around so code like in a listener can access them.
    Or move their definition out of the method and it should work.
    Actually, I believe that it's because an inner class will make copies of the local variables that they use, and if the variable is not final, there's a chance that one version will change and not match the other.