Is it possible to limit the input length in a JavaFX HTMLEditor? I tried to add an event handler to the editor and consume the event when the content reaches a predefined limit but it doesn't seem to work.
editor.addEventHandler(KeyEvent.KEY_TYPED, new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent arg0) {
if (editor.getHtmlText().length() >= MY_LIMIT) {
arg0.consume();
}
}
});
Did anybody manage to achieve this? Is it even possible?
Thanks in advance.
I'm sure there is a more elegant way to solve this problem, but the following code should work:
You need the two private class attributes
private static final int MAX_LENGTH = 250;
private String content;
and the following event handler
editor.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
if(editor.getHtmlText().length() <= MAX_LENGTH) {
content = editor.getHtmlText();
}
}
});
editor.setOnKeyReleased(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
if(editor.getHtmlText().length() > MAX_LENGTH) {
editor.setHtmlText(content);
}
}
});
Related
I would like to create an event handler that listens for multiple key combinations such as holding Ctrl and C at the same time.
Why doesn't something like if((... == Control) && (... == C)) work?
Here is the code I trying to work with:
textField.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
public void handle(KeyEvent event) {
if ((event.getCode() == KeyCode.CONTROL) && (event.getCode() == KeyCode.C)) {
System.out.println("Control pressed");
}
};
});
You can try this solution, it worked for me!
final KeyCombination keyCombinationShiftC = new KeyCodeCombination(
KeyCode.C, KeyCombination.CONTROL_DOWN);
textField.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
if (keyCombinationShiftC.match(event)) {
logger.info("CTRL + C Pressed");
}
}
});
One way to tackle this problem is to create a KeyCombination object and set some of its properties to what you see below.
Try the following:
textfield.getScene().getAccelerators().put(new KeyCodeCombination(
KeyCode.C, KeyCombination.CONTROL_ANY), new Runnable() {
#Override public void run() {
//Insert conditions here
textfield.requestFocus();
}
});
This would be of some help. KeyCombination.
final KeyCombination keyComb1=new KeyCodeCombination(KeyCode.C,KeyCombination.CONTROL_DOWN);
https://code.google.com/p/javafx-demos/source/browse/trunk/javafx-demos/src/main/java/com/ezest/javafx/demogallery/KeyCombinationDemo.java?r=27
A bit more concise (avoids new KeyCombination()):
public void handle(KeyEvent event) {
if (event.isControlDown() && (event.getCode() == KeyCode.C)) {
System.out.println("Control+C pressed");
}
};
There are methods of the type KeyEvent.isXXXDown() for the other modifier keys as well.
my layout like :
i want focus at Company Name Texfield when i press TAB button at first time , but right now i get focus at Add Button how can i manage it ?
i try code like
ChangeFocus(mTextFieldCompanyName, mTextAreaAboutUs);
ChangeFocus(mTextAreaAboutUs, mTextAreaContactUs);
ChangeFocus(mTextAreaContactUs, mButtonVideo);
ChangeFocus(mButtonVideo, mButtonImage);
ChangeFocus(mButtonImage, mButtonSave);
public void ChangeFocus(Control mControlFrom,final Control mControlTo)
{
mControlFrom.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event)
{
if (event.getCode() == KeyCode.TAB)
{
System.out.println("TAB pressed");
mControlTo.requestFocus();
event.consume(); // do nothing
}
}
});
}
Try wrapping your mTextFieldCompanyName inside Platform.runLater()
Platform.runLater(new Runnable() {
#Override
public void run() {
mTextFieldCompanyName.requestFocus();
}
});
Hope it helps :)
TextField f = new TextField();
Button b = new Button("Save");
b.setClickShortcut(KeyCode.ENTER); // For quick saving from text field itself
TextArea longText = new TextArea(); // "Enter" is garbled here
Hot to make the shortcut to work only in the from text field?
Use focus and blur listeners to remove and add the shortcut key:
f.addFocusListener(new FocusListener() {
#Override
public void focus(FocusEvent event) {
b.setClickShortcut(KeyCode.ENTER);
}
});
f.addBlurListener(new BlurListener() {
#Override
public void blur(BlurEvent event) {
b.removeClickShortcut();
}
});
Newer versions of Vaadin require the following code as addListener() is deprecated now.
f.addFocusListener(new FocusListener() {
private static final long serialVersionUID = -6733373447805994139L;
#Override
public void focus(FocusEvent event) {
b.setClickShortcut(KeyCode.ENTER);
}
});
f.addBlurListener(new BlurListener() {
private static final long serialVersionUID = -3673311830300629513L;
#Override
public void blur(BlurEvent event) {
b.removeClickShortcut();
}
});
Talking in terms of Vaadin 14,
I was looking for the answer and for me, this worked well
Button search = new Button("Search");
search.addClickShortcut(Key.ENTER);
As of Vaadin 23 (and probably for sometime before) the requirements have changed again.
private ShortcutRegistration primaryShortCut;
void customShortCutHandling()
{
myTextAreaField.addFocusListener((e) ->
{
System.out.println("disable");
primaryShortCut = primaryButton.addClickShortcut(Key.ENTER);
});
myTextAreaField.addBlurListener((e) ->
{
System.out.println("enable");
primaryShortCut.remove();
});
}
}
This code assumes that primaryShortCut was set when the form is created.
Right now I have the following code working:
#UiHandler("usernameTextBox")
void onUsernameTextBoxKeyPress(KeyPressEvent event) {
keyPress(event);
}
#UiHandler("passwordTextBox")
void onPasswordTextBoxKeyPress(KeyPressEvent event) {
keyPress(event);
}
void keyPress(KeyPressEvent event) {
if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
submit();
}
}
I would like the ability to have just one listener for all elements on the view without duplicating an event for each textbox.
The end goal is that if they press enter, regardless of where they are on the page, it should submit the form.
Thanks!
What works, but still requires you to specify it for each widget, but doesn't require duplicate code:
#UiHandler({"usernameTextBox", "passwordTextBox"})
void onPasswordTextBoxKeyPress(KeyPressEvent event) {
keyPress(event);
}
Yes jackcrews is correct. Also you can try the following. It may be VerticalPanel, DockLayoutPanel etc....
UiBinder.ui.xml
<gwt:VerticalPanel ui:field="mainPanel">
<gwt:Label>Name</gwt:TextBox>
<gwt:TextBox ui:field="textBox">
</gwt:VerticalPanel>
Main.java
#UiField
VerticalPanel mainPanel;
public Main() {
focushandler();
}
void focusHandler() {
mainPanel.addDomHandler(new Handler(), KeyPressEvent.getType());
}
final class Handler implements KeyPressHandler {
#Override
public void onKeyPress(KeyPressEvent event) {
//Code what you expect
}
}
Actually this has more number of lines. But it is good practice.
Regards,
Gnik
I found out that the g:FocusPanel allows me to capture events for everything inside the panel.
#UiHandler("focusPanel")
void onFocusPanelKeyPress(KeyPressEvent event) {
if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
submit();
}
}
I have an editable JTable and have set a DefaultCellEditor like so:
colModel.getColumn( 1 ).setCellEditor( new DefaultCellEditor( txtEditBox ) {
// ...
#Override
public void cancelCellEditing() {
super.cancelCellEditing();
// handling the event
}
// ...
}
However, when pressing escape while editing a cell in this column, though the editing mode is finished, this method is not called. Any ideas why? Am I doing something wrong? Is there a way to handle this (other than manually adding a KeyListener that is)?
The official way: You can register a CellEditorListener: AbstractCellEditor.addCellEditorListener(...). If the editing is canceled, editingCanceled(ChangeEvent e) should be called. Due to a SUN bug http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6788481, editingCanceled is not called :(
As workaround you can register your own action for the ESCAPE key and handle it yourself. But it will not work for resize events.
Another solution (quick and dirty;-)): Overwrite the methode JTable.removeEditor() and insert your code after the super call.
I had this problem too. I wrote another workaround that involves ActionListener and FocusListener. This is it:
public class TableEditorListenerHelper {
// dealing with events
private final EventListenerList listeners = new EventListenerList();
private ChangeEvent changeEvent;
// cell editor that we're helping
private CellEditor editor;
// transient state
private boolean editing = false;
private JTable table;
public TableEditorListenerHelper(CellEditor editor, JTextField field) {
this.editor = editor;
field.addActionListener(new ActionListener() {
#Override public void actionPerformed(ActionEvent e) {
fireEditingStopped();
}
});
field.addFocusListener(new FocusListener() {
#Override public void focusGained(FocusEvent e) {
editing = true;
}
#Override public void focusLost(FocusEvent e) {
JTable table = TableEditorListenerHelper.this.table;
if (editing && isEditing(table)) {
fireEditingCanceled();
}
}
private boolean isEditing(JTable table) { // a hack necessary to deal with focuslist vs table repaint
return table != null && table.isEditing();
}
});
}
public void setTable(JTable table) {
this.table = table;
}
public void addCellEditorListener(CellEditorListener l) {
listeners.add(CellEditorListener.class, l);
}
public void removeCellEditorListener(CellEditorListener l) {
listeners.remove(CellEditorListener.class, l);
}
public CellEditorListener[] getCellEditorListeners() {
return listeners.getListeners(CellEditorListener.class);
}
protected void fireEditingCanceled() {
for (CellEditorListener l : getCellEditorListeners()) {
l.editingCanceled(getOrCreateEvent());
}
resetEditingState();
}
protected void fireEditingStopped() {
for (CellEditorListener l : getCellEditorListeners()) {
l.editingStopped(getOrCreateEvent());
}
resetEditingState();
}
private void resetEditingState() {
table = null;
editing = false;
}
private ChangeEvent getOrCreateEvent() {
return changeEvent = changeEvent == null ? new ChangeEvent(editor) : changeEvent;
}
Here you can find a little more complete solution.
Another way fix this bug:
jTable.addPropertyChangeListener("tableCellEditor", e -> {
Object o = e.getOldValue();
if (o instanceof DefaultCellEditor) {
((DefaultCellEditor) o).cancelCellEditing();
}
});