I have a checkbox button based on which I want to set a variable as true or false. But I don't know how to handle the event. Here's my code:
Boolean check = false;
Button checkBox = new Button(composite,SWT.CHECK);
checkBox.setText("CheckBox");
checkBox.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent event) {
if (event.detail == SWT.CHECK) {
// Now what should I do here to get
// Whether it is a checked event or unchecked event.
}
}
});
To validate selection use getSource() method of event to get object(Button) and check is it selected:
checkBox.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent event) {
Button btn = (Button) event.getSource();
System.out.println(btn.getSelection());
}
});
Related
I have a game that displays an archer and I am trying to set it up so that when my button is pressed, the user is then allowed to click anywhere on the screen and then a new archer would be set to where the click happen. My code currently allows me to click the screen and set a new archer regardless of whether the button was pressed or not. Could someone explain what is wrong becuase I though MouseEvent would occur on the scene once the button was pressed.
myButton.setOnMouseClicked(
new EventHandler<MouseEvent>()
{
public void handle(MouseEvent e)
{
gc.setFill(color);
gc.fillRect(0,0,width,height);
scene.setOnMouseClicked(new EventHandler<MouseEvent>()
{
public void handle(MouseEvent e)
{
archer.setX((int)e.getSceneX());
archer.setY((int)e.getSceneY());
archer.drawCharStand(gc);
}
});
}
});
You could use a ToggleButton, so that you only place the archer when the toggle button is selected:
private ToggleButton myButton = new ToggleButton("Place Archer");
// ...
scene.setOnMouseClicked(e -> {
if (myButton.isSelected()) {
archer.setX((int)e.getSceneX());
archer.setY((int)e.getSceneY());
archer.drawCharStand(gc);
myButton.setSelected(false);
}
});
The last line will unselect the toggle button "automatically" after placing the archer. If you want the user to be able to place multiple archers easily (and have to manually switch off that mode), omit that line.
You will need to change your code slightly. Perhaps try with a variable that tracks if the button was clicked:
boolean archerPlacementMode = false;
....
myButton.setOnMouseClicked(new EventHandler<MouseEvent>(){
public void handle(MouseEvent e)
{
if(!archerPlacementMode) {
archerPlacementMode = true;
gc.setFill(color);
gc.fillRect(0,0,width,height);
archerPlacementMode = true;
return;
}
}
});
scene.setOnMouseClicked(new EventHandler<MouseEvent>() {
public void handle(MouseEvent e)
{
if(archerPlacementMode) {
archer.setX((int)e.getSceneX());
archer.setY((int)e.getSceneY());
archer.drawCharStand(gc);
archerPlacementMode = false;
}
}
});
I have a button that decorates user ClickHandler with my one which controls button state -> makes it disabled on click preventing multiple clicks. When user clicks on it - corresponding DialogBox is opened and button becomes disabled. Here is my button:
public class MyButton extends Button {
private boolean isButtonClicked = false;
private ClickHandler clickHandler;
public MyButton(String html) {
this(html, null);
}
public MyButton(String html, final ClickHandler handler) {
super(html);
addClickHandler(handler);
}
public HandlerRegistration addClickHandler(final ClickHandler handler) {
clickHandler = handler;
ClickHandler ch = new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
if(!isButtonClicked) {
isButtonClicked = true;
setEnabled(false);
clickHandler.onClick(event); //Here is a click handler initiated on fly
}
}
};
return super.addClickHandler(ch);
}
}
And this is how it is used:
public TestClass {
protected OneClickButton button = new OneClickButton("Test Button);
//...
button.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
SomeDialogWindow dialog = new SomeDialogWindow(/*args*/);
dialog.center();
}
});
}
When I click on a button it becomes disabled and dialog appears. But when I close the dialog, my button remains disabled. How to set button enable back on dialog close? What event and where should I handle on order to achieve this?
If your SomeDialogWindow extendes DIalogBox..you can do this
dialog.addCloseHandler(new CloseHandler<PopupPanel>() {
public void onClose(com.google.gwt.event.logical.shared.CloseEvent<PopupPanel> event) {
yourbutton.setEnabled(true);
};
});
I have a code which adds four check box button to SWT shell , i have added selection Listener to the checkBox buttons , which i want to remove on click of OK,CANCEL and CLOSE button of SWT Shell , How can i dispose such event , this is my code
grpVersionTreeComponents.setLayoutData(componentsRenderer.createGridData(490, 220, 4));
for(int versionCount = 0; versionCount < versionSplitters.length ; versionCount++ ){
String splitter= versionSplitters[versionCount];
Button cbVersionSplitter = new CheckBoxWrapper().getButton(grpVersionTreeComponents,splitter.toString() , "");
cbVersionSplitter.setEnabled(true);
versionSplitterCheckBoxList.add(cbVersionSplitter);
versionSplitterCheckBoxList.get(versionCount).addSelectionListener(addSplitterCheckBoxListner(cbVersionSplitter));
}
this my selection Adapter method
public SelectionAdapter addSplitterCheckBoxListner(final Button button){
return new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
String[] defaultVersionSplitters = PropertyClass.getPropertyLabel(QTLConstants.VERSIONING_ASSISTENT_PAGE_SPLLITER).split(QTLConstants.MULTIPLE_EXTENSIONS_SPLITER);
//check if button.getText() is equal to "Custom Splitter" String
if(button.getText().equalsIgnoreCase(defaultVersionSplitters[3])){
if(button.getSelection()){
customVersionSplitterText.setEnabled(true);
}else{
customVersionSplitterText.setEnabled(false);
}
}
}
};
}
and OK cancel Buttons are added in this fashion
private void addOkCancelButtonOnVersionTreePopup(final Shell versionTreeComponentsShell){
Button ok = componentsRenderer.createButtonWidget(versionTreeComponentsShell, SWT.PUSH,
PropertyClass.getPropertyLabel(QTLConstants.OK_BUTTON));
Button cancel = componentsRenderer.createButtonWidget(versionTreeComponentsShell, SWT.PUSH,
PropertyClass.getPropertyLabel(QTLConstants.CANCEL_BUTTON));
ok.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
}
});
}
cancel.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
/*disposeSelectionListener(cbVersionSplitter);*/
versionTreeComponentsShell.close();
}
});
}
}
so how can i remove this selection listener
Regards
You can do the following:
1). Create class which will extend the SelectionAdapter class.
2). Override widgetSelected() method as you have done in your code.
3). Override equals(Object other) method with the following:
#Override
public boolean equals(Object other) {
if (other == null) {
return false;
}
if (other instanceof MyOwnSelectionAdapter) {
return true;
}
}
where MyOwnSelectionAdapter is your own implementation of SelectionAdapter.
4). Now you can do the following: cancel.removeSelectionListener(new MyOwnSelectionAdapter()); and it will delete that selection listener.
How can I update the state (enable/disable) of my button when the user changes the selected item in a combo box.?
The button has a reference to the combo box, but the combo box does not know anything about the button.
If the button has a reference to the combo box, then the button can register an action listener at the combo box in which you can change the state of your button.
final JButton button = new JButton();
final JComboBox comboBox = new JComboBox();
comboBox.addActionListener( new ActionListener() {
#Override
public void actionPerformed( final ActionEvent event ) {
// Your logic to determine when to enable/disable:
final boolean enabled = comboBox.getSelectedIndex() == 0;
button.setEnabled( enabled );
}
} );
The combobox is not required to know about the button. You need to add a listener to the combobox events like this:
public class ComboBoxDemo ... implements ActionListener {
. . .
petList.addActionListener(this) {
. . .
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox)e.getSource();
String petName = (String)cb.getSelectedItem();
yourButton.setEnabled(true/false);
}
. . .
}
The JButton could simply add its own ActionListener on the JComboBox and in the ActionListener you could then change the state of the JButton according to the selected item of the combo box.
I have previously written code where enabling or disabling a button depends on filling a textfield and selecting an item of a combobox together. It may be helpful here.
jComboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jComboBoxActionPerformed(e);
}
});
...
jTextField.getDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
jTextFieldDocumentListener(e);
}
public void removeUpdate(DocumentEvent e) {
jTextFieldDocumentListener(e);
}
public void changedUpdate(DocumentEvent e) {
jTextFieldDocumentListener(e);
}
});
jTextField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jTextFieldActionPerformed(e);
}
});
...
private void jComboBoxActionPerformed(ActionEvent e){
if(jComboBox.getSelectedIndex() == -1)
jButton.setEnabled(false);
else if(!jTextField.getText().equals(""))
jButton.setEnabled(true);
}
private void jTextFieldDocumentListener(DocumentEvent e){
if(jTextField.getText().equals("") || jComboBox.getSelectedIndex() == -1){
jButton.setEnabled(false);
}
else{
jButton.setEnabled(true);
}
}
private void jTextFieldActionPerformed(ActionEvent e){
if(jTextField.getText().equals("")){
jButton.setEnabled(false);
}
if(!(jTextField.getText().equals(""))){
jButton.setEnabled(true);
}
}
If the combobox is selected and the textfield is filled, then the button will be enabled. Otherwise it will not be enabled.
I've one Label in my custom FlowPanel which implements HasDoubleClickHandlers.
final Label label = new Label("Click here to write");
label.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
clicked();
}
});
final CustomFlowPanel customFlowPanel=new CustomFlowPanel();
customFlowPanel.addDoubleClickHandler(new DoubleClickHandler() {
#Override
public void onDoubleClick(DoubleClickEvent event) {
if (event.getSource() instanceof FlowPanel) {
doubleClicked();
}
}
});
custoFlowPanel.add(label);
The problem is when i double click to the label doubleClicked() should not execute.
How to prevent executing doubleClicked() when label is double clicked?
Thanks in advance!!!
You could just check the DoubleClickEvent if the label was clicked and if not you call doubleClicked().
customFlowPanel.addDoubleClickHandler(new DoubleClickHandler() {
#Override
public void onDoubleClick(DoubleClickEvent event) {
Element clicked = event.getNativeEvent();
if (!clicked.Equals(label.getElement())
{
doubleClicked();
}
}
});
I haven't tried it yet, but try adding a double click handler on the label and use Event.stopPropagation() on it. This prevents the event from being propagated to the parent.