Results 1 to 5 of 5
  1. #1
    purplepare is offline Member
    Join Date
    Aug 2014
    Posts
    4
    Rep Power
    0

    Red face swt canvas.redraw doesn't work for clicking button

    the code is :
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.PaintEvent;
    import org.eclipse.swt.events.PaintListener;
    import org.eclipse.swt.events.SelectionAdapter;
    import org.eclipse.swt.events.SelectionEvent;
    import org.eclipse.swt.layout.GridData;
    import org.eclipse.swt.layout.GridLayout;
    import org.eclipse.swt.widgets.Button;
    import org.eclipse.swt.widgets.Canvas;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Shell;
    public class caseButton {
    private Shell shell;
    public static void main(String[] args) {
    try {
    caseButton window = new caseButton();
    window.open();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    /**
    * Open the window.
    */
    public void open() {
    Display display = Display.getDefault();
    createContents();
    shell.open();
    shell.layout();
    while (!shell.isDisposed()) {
    if (!display.readAndDispatch()) {
    display.sleep();
    }
    }
    }
    /**
    * Create contents of the window.
    */
    protected void createContents() {
    shell = new Shell();
    shell.setText("SWT Application");
    GridLayout gridLayout = new GridLayout();
    gridLayout.numColumns = 1;
    gridLayout.marginWidth = 0;
    gridLayout.marginHeight = 0;
    gridLayout.verticalSpacing = 4;
    gridLayout.makeColumnsEqualWidth=false;
    shell.setLayout(gridLayout);

    GridData gridData=new GridData();
    gridData.verticalSpan = 0;
    gridData.horizontalSpan = 3;
    gridData.verticalAlignment = GridData.FILL;
    gridData.grabExcessVerticalSpace= true;
    gridData.horizontalAlignment = GridData.FILL;
    gridData.grabExcessHorizontalSpace = true;

    final Button button = new Button(shell, SWT.PUSH);
    button.setText("draw Rectangle");

    final Canvas canvas = new Canvas(shell, SWT.NONE);
    canvas.setLayoutData(gridData);

    canvas.addPaintListener(new PaintListener() {
    public void paintControl(PaintEvent e) {
    e.gc.drawRoundRectangle(10, 10, 180, 80, 10, 10);
    }
    });
    button.addSelectionListener(new SelectionAdapter() {
    @Override
    public void widgetSelected(SelectionEvent e) {
    System.out.println("buttton_start ");
    canvas.redraw(); // it can' work
    System.out.println("buttton_end ");

    }
    });
    }

    }

    the question is : when I click this button, it will print "buttton_start" and "buttton_end", the "canvas.redraw()" can't work. I don't know the reason , who can help me? thanks

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: swt canvas.redraw doesn't work for clicking button

    Please edit your post and place the code between [code][/code] tags. And just curious as to why you are using SWT?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    purplepare is offline Member
    Join Date
    Aug 2014
    Posts
    4
    Rep Power
    0

    Red face Re: swt canvas.redraw doesn't work for clicking button

    Quote Originally Posted by jim829 View Post
    Please edit your post and place the code between [code][/code] tags. And just curious as to why you are using SWT?

    Regards,
    Jim
    Sorry, i don't find the place which is "edit".And do you think which is better than "SWT".and do you know how to change this code to let it work?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: swt canvas.redraw doesn't work for clicking button

    First, if your post is too old, you can't edit it. There are two ways to add the code tags. One is simply type them in.
    The other is to go the advanced editor, highlight your code and then click on '#'.

    Now, I finally got your code to compile and run. I am not familiar with the SWT API. I am more familiar with Swing
    and a little familiar with AWT (which I don't use anymore). Read about them here --> Standard Widget Toolkit - Wikipedia, the free encyclopedia.

    So your program prints out as you describe and draws a rounded rectangle. What exactly is it supposed
    to do? (Note: I may not be able to help you but providing that information will allow someone else to since they
    would probably request the same information).

    Edit: Your program seems to work as designed. Try this:
    1. Declare two int instance fields, w and h and set them to the initial width and height of the rectangle.
    2. Replace the two constants of width and height where you draw the rectangle with w and h.
    3. In your button listener, simply put h += 10 and w += 10 somewhere in the widgetSelected method.
    4. Now each time you click the button your rectangle will grow in width and height by 10 pixels.


    Regards,
    Jim
    Last edited by jim829; 08-04-2014 at 05:35 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    purplepare is offline Member
    Join Date
    Aug 2014
    Posts
    4
    Rep Power
    0

    Default Re: swt canvas.redraw doesn't work for clicking button

    Quote Originally Posted by jim829 View Post
    First, if your post is too old, you can't edit it. There are two ways to add the code tags. One is simply type them in.
    The other is to go the advanced editor, highlight your code and then click on '#'.

    Now, I finally got your code to compile and run. I am not familiar with the SWT API. I am more familiar with Swing
    and a little familiar with AWT (which I don't use anymore). Read about them here --> Standard Widget Toolkit - Wikipedia, the free encyclopedia.

    So your program prints out as you describe and draws a rounded rectangle. What exactly is it supposed
    to do? (Note: I may not be able to help you but providing that information will allow someone else to since they
    would probably request the same information).

    Edit: Your program seems to work as designed. Try this:
    1. Declare two int instance fields, w and h and set them to the initial width and height of the rectangle.
    2. Replace the two constants of width and height where you draw the rectangle with w and h.
    3. In your button listener, simply put h += 10 and w += 10 somewhere in the widgetSelected method.
    4. Now each time you click the button your rectangle will grow in width and height by 10 pixels.


    Regards,
    Jim
    thanks you very much,jim. that function cann't work, because, i don't update the heigh and width. when i update them ,they can work, thanks again.

Similar Threads

  1. Clear button doesn't work
    By BobbyH in forum AWT / Swing
    Replies: 1
    Last Post: 10-09-2013, 12:45 AM
  2. Replies: 11
    Last Post: 03-14-2012, 07:30 PM
  3. Button size doesn't work.
    By rokit boy in forum AWT / Swing
    Replies: 6
    Last Post: 02-11-2012, 09:38 PM
  4. Replies: 6
    Last Post: 08-24-2011, 02:52 PM
  5. Replies: 6
    Last Post: 12-23-2009, 09:10 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •