Creating a Working Combo List
Knowing
how to create a
Combo
object and add items to it makes it easy to
create a functional ComboExample
class.
How do I do that?
Example 8-1 creates a Combo
object with five items added to the list.
Example 8-1. A Combo example class
import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.*; public class ComboExample { Display d; Shell s; ComboExample( ) { d = new Display( ); s = new Shell(d); s.setSize(250,250); s.setImage(new Image(d, "c:\\icons\\JavaCup.ico")); s.setText("A Combo Example"); final Combo c = new Combo(s, SWT.READ_ONLY); c.setBounds(50, 50, 150, 65); String items[] = {"Item One", "Item Two", "Item Three", "Item Four", "Item Five"}; c.setItems(items); s.open( ); while(!s.isDisposed( )){ if(!d.readAndDispatch( )) d.sleep( ); } d.dispose( ); } }
The setItems( )
method of adding items to the list
is used here. The add( )
method could have been
used just as easily, although with a net increase in the number of
lines of code required.
Everything you learned about creating, sizing, positioning, and
adding items to an instance of the List
class
applies to the Combo
class as well, without
change. In fact, if you compare this code to the code that created
the initial ListExample
class in the previous
chapter, you see that they are identical. Creating an instance of
ComboExample
displays Figure 8-1.
Figure 8-1. A ...
Get SWT: A Developer's Notebook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.