Reputation: 769
I am writing a program in java swt that plays tic tac toe, and I cannot find a way to display an image with an x or o over the button, without using the button.setImage(image) method. When I do that, the image becomes gray and I don't want that. Is there any way to make it so when I click the button, the button becomes disabled and the image displays over it, or can I at least make the button not grayed out when it is disabled?
Also it should be noted I'm using SWT for my GUI.
Here is the portion of the code I'm having trouble with if it is any help:
public static void drawX(Button b, Shell s, Image x){ //draws an X image
int topLeft_X=b.getLocation().x;
int topLeft_Y=b.getLocation().y;
GC gc = new GC(b);
gc.drawImage(x, topLeft_X, topLeft_Y);
}
public static void drawO(Button b, Shell s, Image o){ //draws an O image
int topLeft_X=b.getLocation().x;
int topLeft_Y=b.getLocation().y;
GC gc = new GC(b);
gc.drawImage(o, topLeft_X, topLeft_Y);
}
static double turnCount = 1;
public static void button(final Button b, final Shell s, final int i, final int j, final Image g, final Image h){ //the method that would make the image appear
b.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
b.setEnabled(false);
turnCount++;
if(p1()){
a[i][j]++;
drawX(b, s, g);
b.setVisible(false);
}
else{
a[i][j]--;
drawX(b, s, h);
b.dispose();
}
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
Upvotes: 0
Views: 969
Reputation: 922
Why not put the button in a composite, with FillLayout, and instead of disabling the button, disable the parent composite. Button will look enabled, but mouse click and tab traverse will not reach it.
Warning: it might be annoying for users to see an enabled button that cannot be clicked, but this is what you asked.
Upvotes: 0
Reputation: 3085
Instead of using Button.setEnabled(boolean), where ever you want to enable/disable button, you can filter events on the Button display.
button.getDisplay().addFilter(eventType,listener); button.getDisplay().removeFilter(eventType,listener);
When you want to disable/enable button, add/remove Filter on mouse events and keyboard events.
Upvotes: 0