Results 1 to 5 of 5
- 08-01-2014, 12:03 PM #1
Member
- Join Date
- Aug 2014
- Posts
- 4
- Rep Power
- 0
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
- 08-01-2014, 07:48 PM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 08-04-2014, 04:30 AM #3
Member
- Join Date
- Aug 2014
- Posts
- 4
- Rep Power
- 0
- 08-04-2014, 05:37 AM #4
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimLast edited by jim829; 08-04-2014 at 06:35 AM.
The JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 08-04-2014, 09:31 AM #5
Member
- Join Date
- Aug 2014
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Clear button doesn't work
By BobbyH in forum AWT / SwingReplies: 1Last Post: 10-09-2013, 01:45 AM -
NoughtsAndCrossesGame play button doesn't work to reset the game
By Razorfc in forum Java GamingReplies: 11Last Post: 03-14-2012, 08:30 PM -
Button size doesn't work.
By rokit boy in forum AWT / SwingReplies: 6Last Post: 02-11-2012, 10:38 PM -
how to set a value from a Combobox after Clicking Ok button
By Khan05 in forum AWT / SwingReplies: 6Last Post: 08-24-2011, 03:52 PM -
Keylistenr doesn't work after pressing any button from main aplication
By darkkis in forum New To JavaReplies: 6Last Post: 12-23-2009, 10:10 AM
Bookmarks