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;
}