SWT combo has two troubles:
- If the last selected content is selected, the addselectionlistener method is triggered.
- If setdata (string key, object Value) is used, the key value must be set. Index is usually used as the key value. However, if an item is deleted, its index is automatically-1 and cannot correspond to the key.
To solve this problem, a combo class is rewritten.
To Solve Problem 1, a new interface is written:
- Public interface selectionchangedlistener {
- /**
- * Similar to selectionlistener
- * @ Param event
- */
- Public void selectionchanged (selectionevent event );
- }
To Solve Problem 2, define a class to save the combo status
- Public class combostate {
- Private int selectionindex =-1;
- Public int getselectionindex (){
- Return selectionindex;
- }
- Public void setselectionindex (INT selectionindex ){
- This. selectionindex = selectionindex;
- }
- }
The combo class is as follows:
- Public class easycombo extends combo {
- Private combostate state;
- Private list <Object> indexdata = new jsonlist <Object> ();
- Private list <selectionchangedlistener> scls = new arraylist <selectionchangedlistener> ();
- Public easycombo (composite parent, int style ){
- Super (parent, style );
- This. setstate (New combostate ());
- }
- /**
- * Encapsulate selectionchangedlistener events
- */
- Public void addselectionchangedlistener (selectionchangedlistener listener ){
- If (scls. Size () = 0) {// The first add operation creates an addselectionlistener event.
- Addselectionlistener (New selectionlistener (){
- Public void widgetdefaselecselected (selectionevent e ){
- }
- Public void widgetselected (selectionevent e ){
- If (getstate (). getselectionindex ()! = Getselectionindex ()){
- Getstate (). setselectionindex (getselectionindex ());
- For (selectionchangedlistener listener: scls ){
- Listener. selectionchanged (E );
- }
- }
- }
- });
- }
- Scls. Add (listener );
- }
- Public void add (string text ){
- Super. Add (text );
- Indexdata. Add (null );
- }
- Public void add (string text, int index ){
- Super. Add (text, index );
- If (index> indexdata. Size ()){
- Indexdata. Add (null );
- } Else {
- Indexdata. Set (index, null );
- }
- }
- Public void remove (INT index ){
- Super. Remove (INDEX );
- Indexdata. Remove (INDEX );
- }
- Public void removeall (){
- Super. removeall ();
- Indexdata. Clear ();
- }
- /**
- * Set the value. Unlike setdata (string, object), it is remove sensitive. <br>
- * For example, if 0-> A 1-> B 2-> C deletes 1, 0-> A 1-> C <br>
- * Therefore, you can use a linked list to store the object values. When the remove operation is called, The linked list is also removed from a node.
- *
- * @ Param Index
- * @ Param Value
- */
- Public void setindexdata (INT index, object Value ){
- Indexdata. Set (index, value );
- }
- Public object getindexdata (INT index ){
- Return indexdata. Get (INDEX );
- }
- Public void setstate (combostate state ){
- This. State = State;
- }
- Public combostate getstate (){
- Return this. State;
- }
- /**
- * Note: you must write an empty method. Otherwise, an exception is reported.
- */
- Protected void checksubclass (){
- }
- }
The last is the test class:
- Public class testeasycombo {
- Public static void main (string [] ARGs ){
- Display display = New Display ();
- Shell shell = new shell (Display );
- Shell. settext ("test window ");
- Shell. setlayout (New filllayout ());
- Easycombo combo = new easycombo (shell, SWT. read_only );
- Combo. Add ("test0 ");
- Combo. setindexdata (0, "test 0 ");
- Combo. Add ("test1 ");
- Combo. setindexdata (1, "Test 1 ");
- Combo. Add ("Test2 ");
- Combo. setindexdata (2, "Test 2 ");
- System. Out. println ("index:" + combo. getindexdata (1 ));
- Combo. Remove (1 );
- System. Out. println ("index:" + combo. getindexdata (1 ));
- Combo. addselectionchangedlistener (New selectionchangedlistener (){
- Public void selectionchanged (selectionevent event ){
- System. Out. println ("selection changed! ");
- }
- });
- Shell. Pack ();
- Shell. open ();
- While (! Shell. isdisposed ()){
- If (! Display. readanddispatch ())
- Display. Sleep ();
- }
- }
- }
Now, the new class is complete.
It provides a selectionchangeevent event. When you select a combo item and it is not the one selected last time, selectionchangeevent will be triggered. In addition, I can obtain the corresponding value based on the index.
This is only one way to solve the problem. If combo is not extended (eclipse does not recommend inheriting SWT widgets), there are other solutions. Next