Share this Page URL

Chapter 6. SWT Buttons > Using Images with Buttons - Pg. 102

n++; t1.setText(new Integer(n).toString( )); t1.selectAll( ); } }); b2.addSelectionListener(new SelectionAdapter( ) { public void widgetSelected(SelectionEvent e) { int n = new Integer(t1.getText( )).intValue( ); n--; t1.setText(new Integer(n).toString( )); t1.selectAll( ); } }); When the up arrow button is selected, the code in the widgetSelected( ) method of the SelectionListener attached to Button b1 executes. In that code, the current contents of Text t1 is obtained using getText() and converted to an int value for mathematical operations. The int value is then incremented by one, and the result is converted to a String that is then displayed in Text t1 using setText( ) . Using Images with Buttons Buttons can use more than text to explain to the user the purpose of the button-- images can also be used. Those of you familiar with Borland's C++ development en- vironment may recall the popularity of the OK button that displayed a green checkmark image. You can use images associated with buttons to achieve a similar effect in your