简体   繁体   中英

How To Extend Java's SWT Label?

I wanted to extend the java SWT Label object, and found out it's not allowed.
So I created a class 'MyLabel' that contains a Label object.
It looks something like this:

public class MyLabel {

    Label label;

    //more instance variables here...

    public MyLabel(Composite parent, int type) {
        label=new Label(parent,type);
        //more initialization here...
    }

    public void setText(String label_text) {
        label.setText(label_text);
    }

    public void setImage(Image label_image) {
        label.setImage(label_image);
    }

    public void addMouseListener(MouseListener board_listener) {
        label.addMouseListener(board_listener);
    }

    public void removeMouseListener(MouseListener board_listener) {
        label.removeMouseListener(board_listener);
    }

    //more staff here...
}

and it worked fine for a while, until I started listening to events, and realized this:

public class SWTTest {

    public static void main(String[] args) {

        Display display=new Display();
        Shell shell=new Shell(display);
        shell.setLayout(new RowLayout());


        MyLabel my_label=new MyLabel(shell, SWT.SHADOW_OUT);
        my_label.setText("this is my label");

        my_label.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseUp(MouseEvent e) {
                e.getSource(); //returns the Label object instead of MyLabel object...
            }

        });

        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) 
                display.sleep();
        }
    }
}

this may seems not too bad, but I have several MyLabel objects, and I need to know the source of the mouse click in terms of MyLabel.
Is there some solution to this situation?

**EDIT

I'm trying to create my own label. more specifically, I want a Label with an addition field X in it, and a getX() and setX(X x_var) in it.

**EDIT2

OK, I'll be more specific:
I have a chess board with 64 labels on it, ordered in a 8x8 grid, each represents a position on the board.
Now, what I'm trying to achieve is that whenever one of the 64 positions on the board was clicked - I'll know where it was clicked, in terms of x and y board-coordinates .
So, I want to extend Label, and add Position instance variable to it. (Position is a class that has x and y fields in it).
Now, whenever one of the labels were clicked, I can invoke the "getPoint()" method from within the "mouseUp(MouseEvent e)" method, and to know which position on the board was clicked.

Hope it is more clear now, and sorry for the ambiguity.

You can use the setData method of Label to save a reference to your class.

So when you create your label use:

label.setData(myLabel);

and in the mouse event you can use

MyLabel myLabel = (MyLabel)((Widget)e.getSource()).getData();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM