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

Thread: Need help with button handlers

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Need help with button handlers

    Hello I am taking a java class and I hit a problem with JButtons, button handlers, and action listeners.

    I have to do this:

    Step 0
    // declare & initialize symbolic constants
    This step has been implemented for you as we've declared symbolic int values to determine the size of the
    JFrame and we've declared a symbolic String value for each of the JButtons that will appear on our form.
    Step 1
    // declare & initialize an action listener object
    This step is similar to that which was performed in the previous exercise, except that you should create only a
    single instance of ButtonHandler to support each JButton.
    Steps 2-3
    // declare & initialize 2 labels
    // declare & initialize 6 button objects
    These are the last of the initializations that should appear above the constructor TravlChk(). Be sure to use
    the symbolic String constants from Step 0 to identify your JButton objects.

    Step 11
    // register the listener class for each JButton
    The single instance of class ButtonHandler that was created at Step 1 should be registered with each JButton
    that was instantiated at Step 3.


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class TravlChk extends JFrame
    {
    private static final int WIDTH = 400, Height = 300;
     
    private static final String
     
    PESO_S = "Mexican peso",
    FRANC_S = "Swiss franc",
    EURO_S = "EURO dollar",
    US_S = 'U.S. dollar",
    CHECK_S = "Check Balance",
    EXIT_S = "Exit";
    PESO_SHandler pbHandler = new PESO_SHandler();
    FRANC_SHandler fbHandler = new FRANC_SHandler();
    EURO_SHandler obHandler = new EURO_SHandler();
    US_SHandler ubHandler = new US_SHandler();
    CHECK_SHandler cbHandler = new CHECK_SHandler();
    EXIT_SHandler ebHandler = new EXIT_SHandler();
    JLabel cashL = new JLabel("Cash Your Traveler's Check"' SwingConstants.RIGHT);
    JLabel clickL = new JLabel("Click the button that matches your currency type", SwingConstants.RIGHT);
    JButton PESO_S = new JButton("Mexican peso");
    JButton*FRANC_S = new Jbutton("Swiss franc");
    JButton EURO_S = new JButton("Euro dollar");
    JButton US_S = new JButton("U.S. dollar");
    JButton CHECK_S = new JButton("Check Balance");
    JButton EXIT_S = new JButton("Exit");
     
    Public TravlChk()
    {
    Container pane = getContentPane();
    pane.setLayout(new GridLayout(8,1));
    pane.add(cashL);
    pane.add(clickL);
    pane.add(PESO_S);
    pane.add(FRANC_S);
    pane.add(EURO_S);
    pane.add(US_S);
    pane.add(CHECK_S);
    pane.add(EXIT_S);
     
    setTItle("Traveler'sCheck Machine);
    setSize(400,300);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    PESO_S.addActionListener(pbHandler);
    FRANC_S.addActionListener(fbHandler);
    EURO_S.addActionListener(obHandler);
    US_S.addActionListener(ubHandler)
    CHECK_S.addActionListener(cbHandler);
    EXIT_S.addActionListener(ebHandler);
    }
     
    if (e.getActionCommand().equals(EXIT_S))
       System.exit(0);
     
    public static void main(String[] args)
    {
     TravlChk tc = new TravlChk();
    }
     
    }


    //My brackets for the public class (the first and last) wont match up but when I put a bracket before the public static void main it ill match up with the open bracket for the class and close it. any idea why it is doing that? And are my buttons, button handlers, and action listeners correctly coded?
    Last edited by jps; November 11th, 2012 at 04:43 PM. Reason: added code tags


  2. #2
    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: Need help with button handlers

    You may want to show us more code and describe more context. You are assuming that we know what the rest of your code looks like and what it's doing, and that prevents us from being able to give intelligent advice.

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Need help with button handlers

    Try to edit the code to be properly indented, and do something about the extra commas near the top. They do not belong there.
    Once the indentation is corrected it will be easier to fix the brackets.

  4. The Following 2 Users Say Thank You to jps For This Useful Post:

    curmudgeon (November 11th, 2012), iggyggy (November 18th, 2012)

  5. #4
    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: Need help with button handlers

    Thanks for posting your code.

    I agree with jps: properly formatted code is much easier to debug and understand than improperly formatted code. The formatting is not for the compiler's benefit, since it doesn't care what formatting you use. It is for YOUR benefit so that you can better understand and fix your code. So put some effort into your code formatting and in particular the indentation so that it is uniform and logical, and both your mistake will become obvious, and the dividends will be enormous.

  6. The Following User Says Thank You to curmudgeon For This Useful Post:

    iggyggy (November 18th, 2012)

  7. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help with button handlers

    Thanks guys, the problem was that I had an extra parenthesis in the pane.add section which didn't let my brackets connect.

  8. #6
    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: Need help with button handlers

    Glad you've got it solved and fully understand the power and importance of code formatting.

Similar Threads

  1. Button help
    By lf2killer in forum Java Theory & Questions
    Replies: 2
    Last Post: November 10th, 2012, 10:09 AM
  2. Handlers and Classes/Interfaces
    By lightOfDay in forum Object Oriented Programming
    Replies: 0
    Last Post: August 26th, 2012, 02:41 AM
  3. Cancel button ..??
    By smasm in forum Loops & Control Statements
    Replies: 3
    Last Post: November 5th, 2011, 06:12 AM
  4. Incrementing button by 1
    By joshft91 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 1st, 2011, 10:45 AM
  5. Multiple Identical Scripts - Variables and Load Handlers
    By cas22 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 22nd, 2011, 11:37 AM

Tags for this Thread