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

Thread: Clarification needed for addActionListener and actionPerformed

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

    Default Clarification needed for addActionListener and actionPerformed

    Hi, I am trying to figure out something about the addActionListener and actionPerformed duo.

    Below you will find an excerpt from code which is made available by my (stock) broker, the InteractiveBrokers API kit. The resulting GUI (from all the code) has buttons, of which only two interest me: butMktData and butPlaceOrder. If you click on the "request data" button, stock market data is displayed in a text box in a continuous loop. If you click the "generate order" button, you can place an order (only once). This is logical, as you don't want to place orders every second until your account is empty.

    I am having hard time figuring out how you get out of the continuous loop that is silently created in background when you use addActionListener, actionPerformed.

    As I compare the two code sections below, I don't see why section 1 will execute an infinite loop, while section 2 will execute only once. The java book for dummies and youtube videos don't help.

    The ultimate goal behind all this: be able to modify the code so a form of monitoring is executed when I submit an order. When condition X is met, execute order. If not, wait, don't submit the order. Right now clicking the submit button creates an order instantaneously.

    Any clarification will be appreciated.

    ------------- SECTION 1 -----------------

    JButton butMktData = new JButton( "Req Mkt Data");
    butMktData.addActionListener( new ActionListener() {
    public void actionPerformed( ActionEvent e) {
    onReqMktData();
    }
    });

    void onReqMktData() {
    // run m_orderDlg
    m_orderDlg.show();
    if( !m_orderDlg.m_rc ) {
    return;
    }

    // req mkt data
    m_client.reqMktData( m_orderDlg.m_id, m_orderDlg.m_contract,
    m_orderDlg.m_genericTicks, m_orderDlg.m_snapshotMktData);
    }
    ------------ SECTION 2 --------------------------------------------

    JButton butPlaceOrder = new JButton( "Place Order");
    butPlaceOrder.addActionListener( new ActionListener() {
    public void actionPerformed( ActionEvent e) {
    onPlaceOrder();
    }
    });

    void onPlaceOrder() {
    placeOrder(false);
    }

    void placeOrder(boolean whatIf) {
    // run m_orderDlg
    m_orderDlg.show();
    if( !m_orderDlg.m_rc ) {
    return;
    }

    Order order = m_orderDlg.m_order;

    // save old and set new value of whatIf attribute
    boolean savedWhatIf = order.m_whatIf;
    order.m_whatIf = whatIf;

    // place order
    m_client.placeOrder( m_orderDlg.m_id, m_orderDlg.m_contract, order );

    // restore whatIf attribute
    order.m_whatIf = savedWhatIf;
    }


  2. #2
    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: Clarification needed for addActionListener and actionPerformed

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.


    the continuous loop that is silently created in background when you use addActionListener, actionPerformed.
    Can you explain where this loop is?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Can't use button.addActionListener(this); ?
    By xdega in forum AWT / Java Swing
    Replies: 2
    Last Post: April 23rd, 2012, 08:44 AM
  2. Not sure how to deal with addActionListener! :3
    By Asteroid555 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 1st, 2011, 01:57 PM
  3. Needed help in actionPerformed statements
    By S-NESH in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 7th, 2011, 06:36 PM
  4. non-static variable, addActionListener()
    By bobbyrne01 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 31st, 2010, 10:58 AM
  5. [SOLVED] NullPointerException.CLARIFICATION
    By chronoz13 in forum Exceptions
    Replies: 8
    Last Post: August 28th, 2009, 03:24 PM