I have an issue with GWT SuggestBox widget.
I have a handler on every field of the form to submit it with enter key.
However I'd like to fill suggest box with enter key too.
Thus I'm trying to find a proper way to :
1. when I choose an item in suggest box (suggestion menu shown) and press enter, select this item
2. when suggestion menu is not show validate form
Any idea
When you look at SuggestBox constructors, you will find this:
public SuggestBox(SuggestOracle oracle,
ValueBoxBase<java.lang.String> box,
SuggestBox.SuggestionDisplay suggestDisplay)
oracle - supplies suggestions based upon the current contents of the text widget
box - the text widget
suggestDisplay - the class used to display suggestions
SuggestionDisplay is what you need to check if isSuggestionListShowing().
Example code:
#UiField
FormPanel uiFormPanel;
#UiField
TextBox uiTextBox;
#UiField(provided = true)
SuggestBox uiSuggestBox;
public SubmitTest() {
// provided fields
MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
oracle.add("Cat");
oracle.add("Dog");
oracle.add("Horse");
oracle.add("Canary");
TextBox box = new TextBox();
final DefaultSuggestionDisplay suggestDisplay = new DefaultSuggestionDisplay();
uiSuggestBox = new SuggestBox(oracle, box, suggestDisplay);
initWidget(uiBinder.createAndBindUi(this));
uiFormPanel.addSubmitHandler(new SubmitHandler() {
#Override
public void onSubmit(SubmitEvent event) {
event.cancel();
Window.alert("Submit!");
}
});
uiTextBox.addKeyDownHandler(new KeyDownHandler() {
#Override
public void onKeyDown(KeyDownEvent event) {
if(event.getNativeKeyCode() == KeyCodes.KEY_ENTER)
uiFormPanel.submit();
}
});
uiSuggestBox.addKeyDownHandler(new KeyDownHandler() {
#Override
public void onKeyDown(KeyDownEvent event) {
if(event.getNativeKeyCode() == KeyCodes.KEY_ENTER)
if(!suggestDisplay.isSuggestionListShowing())
uiFormPanel.submit();
}
});
}
UiBinder:
<g:FormPanel ui:field="uiFormPanel">
<g:HTMLPanel>
<g:TextBox ui:field="uiTextBox" />
<g:SuggestBox ui:field="uiSuggestBox" />
</g:HTMLPanel>
</g:FormPanel>
Related
private void createEvents()
{
menuFileExit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
System.exit(0);
}
});
////// Events on tree selection
jtStoryViewer.addTreeSelectionListener(new TreeSelectionListener()
{
public void valueChanged(TreeSelectionEvent arg0)
{
DefaultMutableTreeNode selection = (DefaultMutableTreeNode) jtStoryViewer.getLastSelectedPathComponent();
Object nodeObject = selection.getUserObject();
////// Checks if selected node is a String (only story title is a string)
if(selection.getUserObject().getClass().getName() == "java.lang.String" )
{
tfTitle.setText(nodeObject.toString());
////// Action listener for Change Button
btnChange.addActionListener(new ActionListener()
{
////// Title text swap
public void actionPerformed(ActionEvent arg0)
{
selection.setUserObject(tfTitle.getText());
((DefaultTreeModel)jtStoryViewer.getModel()).nodeChanged(selection);
}
});
}
///// checks if the object is a chapter object
if(selection.getUserObject().getClass().getName() == "ISW.common.Chapter")
{
Chapter chapter = (Chapter) selection.getUserObject();
tfTitle.setText(chapter.toString());
////// Action listener for Change Button
btnChange.addActionListener(new ActionListener()
{
////// Title text swap
public void actionPerformed(ActionEvent arg0)
{
chapter.setTitle(tfTitle.getText());
((DefaultTreeModel)jtStoryViewer.getModel()).nodeChanged(selection);
}
});
}
}
});
}
I am using JTree to display and modify some objects. I added a TreeSelectionListener to get the object data on selection. For now I want to be able to change the title of an object, it works fine on first selection on the tree , I change the value in the text box and the "Change" button works just fine, but when I move on to next objects, the change button also modifies the value of all previously selected objects.
I guess it is caused due to my improper usage of the ActionListeners but I can't tell for sure and at this point I'm stuck.
Will be grateful for any hints.
Don't keep adding an ActionListener to the btnChange JButton within the TreeSelectionListener#valueChanged method.
This will cause the button to call EVERY ActionListener you have previously
Instead, give the btnChange a single ActionListener, when clicked, can act on the currently selected node (by checking the JTree it self). You could have the TreeSelectionListener#valueChanged method enable or disable the btnChange based on the validity of the selection
Also, if(selection.getUserObject().getClass().getName() == "ISW.common.Chapter") isn't how String comparison is done in Java, instead you should use something more like if("ISW.common.Chapter".equals(selection.getUserObject().getClass().getName()))
I'm studying the GWT framework and I'm trying to create a custom widget: this widget is a button that contain inside a menu of operations.
if you click in the area of the triangle I want a menu with some options (that are possible operations) and if I click in the other parts of the buttons I want that the operation is the first from the list.
I have put a ListBox widget inside a Button widget and I want 2 different clickListener. The problem is that the listener of the listBox inside the button don't work.
Do you know why?
Following the code
public class MyClass extends Composite {
private ListBox options;
private Button saveButton;
private HorizontalPanel savePanel;
private int indexHandler;
public MyClass(String label, List<String> operationList, final List<Command> commandList) {
savePanel = new HorizontalPanel();
initWidget(savePanel);
options = new ListBox();
saveButton = new Button(label);
for(String operation : operationList){
options.addItem(operation);
}
options.sinkEvents(Event.ONCLICK);
options.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
System.out.println("Test1");
indexHandler = options.getSelectedIndex();
commandList.get(indexHandler).execute();
options.setItemSelected(0, true);
}
});
saveButton.getElement().appendChild(options.getElement());
saveButton.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
System.out.println("Test2");
commandList.get(0).execute();
options.setItemSelected(0, true);
}
});
savePanel.add(saveButton);
}
}
Don't use ClickHandler on the ListBox. Use ChangeHandler instead.
Also, I don't think you need to mess with Elements here. Simply add your Button widget and your ListBox widget to a container (i.e. some panel). You can add button on top of ListBox, if you want.
I have created a popup box that extends DialogBox and uses a cellTable that contains a list of values, one of which will be selected and inserted into a textBox.
-I have an onSelectionChange handler which is fired when one of the rows is clicked.
-I have an onDoubleClick handler which is fired when the same rows are double clicked.
both work when the other is commented out. But when they are both in the live code, whichever one is written first gets overwritten by the other one and no longer gets called.
Any way around this?
Code snipbit:
final SingleSelectionModel<popUpBoxContent> selectionModel= new <popUpBoxContent>();
cellTable.setSelectionModel(selectionModel);
selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler(){
public void onSelectionChange (selectionChangeEvent event){
//Do something
}});
final SingleSelectionModel<popUpBoxContent> selectionModel2= new <popUpBoxContent>();
cellTable.setSelectionModel(selectionMode2);
cellTable.addDomHandler(new DoubleClickHandler(){
public void onDoubleClick(final DoubleClickEvent event){
//Do something else
}},
DoubleClickEvent.getType());
Thank you!
Yes they get overwritten from what I can see in the snippet. Assuming "popUpBoxContent" is the data type with which the CellTable (I presume cellTable is a CellTable) is being populated you could try this and see if it works:
final SingleSelectionModel<PopUpBoxContent> selectionModel = new SingleSelectionModel<PopUpBoxContent>();
cellTable.setSelectionModel(selectionModel);
cellTable.addDomHandler(new DoubleClickHandler() {
public void onDoubleClick(final DoubleClickEvent event) {
PopUpBoxContent selected = selectionModel.getSelectedObject();
if (selected != null) {
System.out.println("double clicked");
}
}
},
DoubleClickEvent.getType());
selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
public void onSelectionChange(SelectionChangeEvent event) {
System.out.println("clicked");
}
});
There's a text field and when lost focus it will validate the inputs, if not passed, print out the error message (to be simple here just has an empty check). And there's a button next to the text field, it will print out the text once click on it.
As I tried, when input some text and then click the button it will trigger both the focus lost event of text field and the event of button. In a other word, it will do the validation first and then print out the input text.
Here comes my question, what is the good approach to prevent printing out the text if the validation not passed? Or is there a way to "ignore" the click event on button if validation not passed?
I tried to use a boolean flag which indicate the validation result and check the flag when perform the action for button, but I do not think it is a good approach. As I know there's an event dispatcher thread in Swing which deal with the events, is it possible I can cancel the events from here?
Below is a piece of code which explain the question:
public class SimpleDemo
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel content = new JPanel(new FlowLayout());
frame.setContentPane(content);
final JTextField textField = new JTextField(10);
textField.addFocusListener(new FocusAdapter()
{
#Override
public void focusLost(FocusEvent e)
{
String text = textField.getText();
// do some validation here, if not validated
// do not trigger the event on button.
if ("".equals(text))
{
System.out.print("please input a text!");
}
}
});
content.add(textField);
JButton button = new JButton("Print Text");
button.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
// action performed for button
String text = textField.getText();
System.out.println(text);
}
});
content.add(button);
frame.setVisible(true);
frame.pack();
}
}
I faces similar issue while working on an application. I solved it like below
I created a abstract class ApplicationFrame which every frame in the application extends
public abstract class ApplicationFrame extends JFrame implements ActionListener {
#Override
final public void actionPerformed(ActionEvent event) {
if(validateInput()){
performAction(event);
}
}
/*
* Sub class should override this method to receive any action
*/
protected void performAction(ActionEvent event) {};
/*
* Sub class should override this method to perform validation
*/
abstract protected boolean validateInput();
}
All Frames will now extend this base frame, as below:
public class Frame1 extends ApplicationFrame{
#Override
protected void performAction(ActionEvent event) {
// perform action
}
#Override
protected boolean validateInput() {
// return true or false depending upon the validation results
}
// if you want to add Action Listener, you need to add like this:
btnSomeButton.addActionListener(this);
}
If you need to handle Focus events, you can make ApplicationFrame or the base frame implement FocusListener.
This is my custom implementation to solve the problem, hope this helps.
Make the button disabled on start-up
Upon lost focus, validate the text & enable button only when the input passes validation.
Upon start of text change, disable the button
It's always makes sense to make ui to communicate with user. So you can show "please input a text" as the default text of the textField when nothing is entered by user.
Here is the code for such custom textField:
public class TextFieldWithDefaultText extends JTextField implements FocusListener{
private final String hint;
public TextFieldWithDefaultText (String $text)
{
super($text);
this.hint = $text;
addFocusListener(this);
}
#Override
public void focusGained (FocusEvent $e)
{
if (this.getText().isEmpty())
{
super.setText("");
}
}
#Override
public void focusLost (FocusEvent $e)
{
if (this.getText().isEmpty())
{
super.setText(hint);
}
}
#Override
public String getText ()
{
String typed = super.getText();
return typed.equals(hint) ? "" : typed;
}
}
Write the acttionListerner for your button like this:
JButton button = new JButton("Print Text");
button.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
if(!textField.getText().isEmpty())
System.out.println(textField.getText());
}
});
And ur textField implementation should be :
final TextFieldWithDefaultText textField = new TextFieldWithDefaultText ("please input a text");
Hope this helps :)
I want to add in a SWT/JFace application a search functionality that filter a TableViewer as the user enter text in the search text field.
final Text filterText = new Text(parent, SWT.NONE);
filterText.addModifyListener(new ModifyListener() {
#Override
public void modifyText(ModifyEvent arg0) {
//TODO how to update the viewer filter with the new text ?
}
});
TableViewer tableViewer = new TableViewer(...);
ViewerFilter filterViewer = new ViewerFilter() {
#Override
public boolean select(Viewer viewer, Object parentElement, Object element) {
if (filterText.getText() == "") {
return true;
}
//do my stuff to know if element need to be filtered or not
return false;
}
};
tableViewer.addFilter(filterViewer);
Do I need to remove the filter and create a new one in the modify listener or is there a better solution?
Basically, you need to have a way of passing the entered text to the filter, in your select method you should filter based on this text, and in your text widget's listener pass the text to the filter and call viewer.refresh() on your table.
This example should help you: http://www.vogella.com/tutorials/EclipseJFaceTableAdvanced/article.html#jfacetable_filter
org.eclipse.ui.dialogs.FilteredTree is specifically available for that purpose. Why can't you use that?