sencha display tooltip of a text field when it is in focus - java

How do I display tooltip of a text field when it is in focus, not when mouse is hovering over?
I use TooltipConfig .
TextField email = getEditor().getEmail();
ToolTipConfig toolTipConfig = new ToolTipConfig();
toolTipConfig.setBodyText("TEXT BODY");
toolTipConfig.setAnchor(Style.Side.LEFT);
toolTipConfig.setMouseOffsetY(0);
toolTipConfig.setMouseOffsetX(0);

Are you using extjs? If so, here's my answer:
You need to create custom tooltip to disable the default mouseover event.
Ext.define('nutnull.ToolTip', function() {
return {
extend: 'Ext.tip.ToolTip',
xtype: 'nutnull-tooltip',
// Set this as emptyFn so that it will not create mouse event to show the tooltip
setTarget: Ext.emptyFn
};
});
Then you can override the textfield to show tooltip during focus event.
Ext.define('nutnull.override.Text', {
override: 'Ext.form.field.Text',
tip: null,
onFocus: function() {
// show tooltip if exist
if(this.tip) {
if(!this.tipCmp) {
this.tipCmp = Ext.create('nutnull.ToolTip', {
target: this.id,
html: this.tip
});
}
// Get the position of the textfield instead of mouse position
this.tipCmp.targetXY = this.getPosition();
// Adjust the position.
this.tipCmp.targetXY[0] += this.getWidth();
this.tipCmp.targetXY[1] -= (this.getHeight()/2);
// Manually show the tooltip
this.tipCmp.show();
}
this.callParent(arguments);
}
});
In the textfield item, just add tip: 'Sample Description' config.
{
xtype: 'textfield',
fieldLabel: 'Name',
tip: 'Sample Description'
}

I resolved this problem. I made new Tooltip object and wrapped tooltip config into this object.
TextField email = getEditor().getEmail();
ToolTipConfig toolTipConfig = new ToolTipConfig();
toolTipConfig.setBodyText("TEXT BODY");
toolTipConfig.setAnchor(Style.Side.LEFT);
toolTipConfig.setMouseOffsetY(0);
toolTipConfig.setMouseOffsetX(0);
Tooltip tooltip = new ToolTip(email,toolTipConfig);
tooltip.show();

Related

How to clear ComboBoxes that are in an HBox when clicking a button?

I have a simple JavaFX GUI where I have an HBox across the top that contains several ComboBoxes that will eventually act as filters. I can't figure out how to reset the value of the ComboBoxes to an empty string when clicking a "clear" button. Any tips would be appreciated.
Update: Here's my code that works for me
// private EventHandler to pass to the clearButton's action
EventHandler<ActionEvent> clearAction = new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
List<Node> nodes = topPane.getChildren();
for (Node node : nodes) {
if (node instanceof ComboBox) {
((ComboBox) node).getSelectionModel().clearSelection();
}
}
}
};
clearButton.setOnAction(clearAction);
To clear the selection of a ComboBox, you need to access the SelectionModel. In the SelectionModel you'll find a method clearSelection() which can be used in the action handler for button. Assuming you're familiar with everything else involved, you'll want something like the following.
ComboBox<String> box = new ComboBox<>();
box.getItems().addAll( "Choice 1", "Choice 2", "Choice 3" );
Button clearButton = new Button( "Clear Selection" );
clearButton.setOnAction( e -> {
box.getSelectionModel().clearSelection();
} );

Get currently displaying tooltip

Is there some way to copy the currently displayed tooltip to the clipboard as a string without complex XY-coord calculation that maps to the tooltip text area? This is especially challenging on a chart with tooltip displayed at an angle, also to only capture if being displayed. For example to get ctl-c to copy the displaying tooltip to clipboard:
PlotThisDaysData extends JFrame implements ... KeyListener{
#Override
public void keyTyped( KeyEvent e ) {
char typed = e.getKeyChar();
if ( typed == KeyEvent.VK_C ) /*VK_C?*/ {
String tooltipStr = myChart.???(); // <<<<<<<<<<<<< get displaying tooltip <<<<
StringSelection selection = new StringSelection( tooltipStr );
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents( selection, selection );
}
}
Perhaps there is some event when a tooltip gets displayed so I can store a String pointer and use when ctl-c is entered?
Tooltips are displayed in response to mouse events received by the chart's enclosing ChartPanel. To copy the currently displayed tooltip to the clipboard as the mouse moves,
Add a ChartMouseListener to the chart panel, as shown here.
When your listener sees a desired ChartEntity, ask the ChartPanel for the relevant text and copy it to the clipboard.
Toolkit toolkit = Toolkit.getDefaultToolkit();
Clipboard clipboard = toolkit.getSystemClipboard();
…
#Override
public void chartMouseMoved(ChartMouseEvent cme) {
…
String t = chartPanel.getToolTipText(cme.getTrigger());
clipboard.setContents(new StringSelection(t), null);
}
A similar approach can be used in a key binding, as shown here. Use the chart panel's getMousePosition() to construct the required MouseEvent trigger.
Get the chart panel's InputMap, ActionMap, and the platform's shortcut mask.
InputMap im = chartPanel.getInputMap();
ActionMap am = chartPanel.getActionMap();
int mask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
Put the desired KeyStroke in the chart panel's InputMap
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_C, mask), "copytip");
Put the corresponding Action in the chart panel's ActionMap
am.put("copytip", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
Point p = chartPanel.getMousePosition();
String t = chartPanel.getToolTipText(new MouseEvent(chartPanel,
0, System.currentTimeMillis(), 0, p.x, p.y, 0, false));
clipboard.setContents(new StringSelection(t), null);
}
});
Avoid KeyListener, as it requires keyboard focus.

JavaFX default focused button in Alert Dialog

Since jdk 8u40, I'm using the new javafx.scene.control.Alert API to display a confirmation dialog. In the example below, "Yes" button is focused by default instead of "No" button:
public boolean showConfirmDialog(String title, String header, String content, AlertType alertType) {
final Alert alert = new Alert(alertType);
alert.setTitle(title);
alert.setHeaderText(header);
alert.setContentText(content);
alert.getButtonTypes().clear();
alert.getButtonTypes().addAll(ButtonType.YES, ButtonType.NO);
final Optional<ButtonType> result = alert.showAndWait();
return result.get() == ButtonType.YES;
}
And I don't know how to change it.
EDIT :
Here a screenshot of the result where "Yes" button is focused by default :
I am not sure if the following is the way to usually do this, but you could change the default button by looking up the buttons and setting the default-behavior yourself:
public boolean showConfirmDialog(String title, String header, String content, AlertType alertType) {
final Alert alert = new Alert(alertType);
alert.setTitle(title);
alert.setHeaderText(header);
alert.setContentText(content);
alert.getButtonTypes().clear();
alert.getButtonTypes().addAll(ButtonType.YES, ButtonType.NO);
//Deactivate Defaultbehavior for yes-Button:
Button yesButton = (Button) alert.getDialogPane().lookupButton( ButtonType.YES );
yesButton.setDefaultButton( false );
//Activate Defaultbehavior for no-Button:
Button noButton = (Button) alert.getDialogPane().lookupButton( ButtonType.NO );
noButton.setDefaultButton( true );
final Optional<ButtonType> result = alert.showAndWait();
return result.get() == ButtonType.YES;
}
A simple function thanks to crusam:
private static Alert setDefaultButton ( Alert alert, ButtonType defBtn ) {
DialogPane pane = alert.getDialogPane();
for ( ButtonType t : alert.getButtonTypes() )
( (Button) pane.lookupButton(t) ).setDefaultButton( t == defBtn );
return alert;
}
Usage:
final Alert alert = new Alert(
AlertType.CONFIRMATION, "You sure?", ButtonType.YES, ButtonType.NO );
if ( setDefaultButton( alert, ButtonType.NO ).showAndWait()
.orElse( ButtonType.NO ) == ButtonType.YES ) {
// User selected the non-default yes button
}
If you have a look at (private) ButtonBarSkin class, there is a method called doButtonOrderLayout() that performs the layout of the buttons, based in some default OS behavior.
Inside of it, you can read this:
/* now that all buttons have been placed, we need to ensure focus is
set on the correct button. [...] If so, we request focus onto this default
button. */
Since ButtonType.YES is the default button, it will be the one focused.
So #ymene answer is correct: you can change the default behavior and then the one focused will be NO.
Or you can just avoid using that method, by setting BUTTON_ORDER_NONE in the buttonOrderProperty(). Now the first button will have the focus, so you need to place first the NO button.
alert.getButtonTypes().setAll(ButtonType.NO, ButtonType.YES);
ButtonBar buttonBar=(ButtonBar)alert.getDialogPane().lookup(".button-bar");
buttonBar.setButtonOrder(ButtonBar.BUTTON_ORDER_NONE);
Note that YES will still have the default behavior: This means NO can be selected with the space bar (focused button), while YES will be selected if you press enter (default button).
Or you can change also the default behavior following #crusam answer.

Nebula gridTableViewer cell content should wrap

I am using nebula gridTableViewer for my application, in its cell some time content is so lengthy, so I want to show as WRAP (in multiline). I did changes for it but it is not reflecting:
I added SWT.WRAP but not works, I tried to wrap the text in LabelProvider too but it also not works so how could i do it?
Should i need to add listener for this.
Adding SWT.WRAP to the gridTableViewer would not cause the Text in the grid cells to wrap.
gridColumn.setWordWrap(true) causes the text entered in the cell to be wrapped after the entered value is applied to the editor(setvalue called). To wrap the text interactively, SWT.WRAP should be added as a style to the TextCellEditor or to Text widget placed in the cells.
gridViewerColumn.getColumn.setWordWrap(true);
gridTableViewer.setEditingSupport(new EditingSupport(gridTableViewer) {
....
#Override
protected CellEditor getCellEditor(Object element) {
TextCellEditor cellEditor = new TextCellEditor(gridTableViewer.getGrid(),
SWT.WRAP)
return cellEditor;
}
Or
....
final GridItem item = grid.getItem(grid.getTopIndex());
for (int i = 0; i < grid.getColumnCount(); i++) {
final Text text = new Text(grid, SWT.WRAP);
Listener textListener = new Listener() {
public void handleEvent(final Event e) {
switch (e.type) {
case SWT.FocusOut:
item.setText(column, text.getText());
....
}
}
}
}
I have used the SWT.WRAP in the TextCellEditor before, and it works.

JavaFX TableColumn Graphic not Hiding

I'm creating a custom header for my TableColumns that is the label of the column plus a TextField that will allow users to perform searches. I'm setting the column headers like so:
getColumns().addListener(new ListChangeListener<TableColumn<S, ?>>() {
#Override
public void onChanged(final ListChangeListener.Change<? extends TableColumn<S, ?>> change) {
while (change.next()) {
Label label;
TextField search;
VBox graphic;
for (TableColumn<S, ?> column : change.getAddedSubList()) {
label = new Label(column.getText());
search = new TextField();
graphic = new VBox();
graphic.getStyleClass().add("k-column-graphic");
graphic.getChildren().addAll(label, search);
column.setGraphic(graphic);
}
}
}
});
So the column's graphic is what is displayed. I'm using the following CSS (the graphic itself has a "k-column-graphic" CSS class, while the TableView has a "k-table-view" CSS class)
/** Hide default text label in KTableView */
.k-table-view .column-header > .label {
-fx-content-display: graphic-only;
}
.k-column-graphic {
-fx-alignment: center-left;
-fx-spacing: 5;
-fx-padding: 2;
}
This works great, but I'm also allowing the columns to be hidden by enabling the TableView.setTableMenuButtonVisible(true); property, which adds a button to easily hide columns.
Whenever I try to hide a column, it hides successfully, but the graphic (the Label/TextField) remain. Both seem to have a width of 0 or 1, and are very small, but you can still see them.
How, either through CSS or somewhere in my code, do I make it to where the graphic Node for the TableColumn will hide as well?
When you toggle the CheckMenuItem to show/hide the column, your customized controls won't automatically change their values of VisibleProperty. So what you need to do is simply bind the VisibleProperty of your own controls to the TableColumn's VisibleProperty.
Following sample is based on your code. Hoping it can help.
getColumns().addListener(new ListChangeListener<TableColumn<S, ?>>() {
#Override
public void onChanged(final ListChangeListener.Change<? extends TableColumn<S, ?>> change) {
while (change.next()) {
Label label;
TextField search;
VBox graphic;
for (TableColumn<S, ?> column : change.getAddedSubList()) {
label = new Label(column.getText());
search = new TextField();
graphic = new VBox();
graphic.getStyleClass().add("k-column-graphic");
graphic.getChildren().addAll(label, search);
column.setGraphic(graphic);
/* ======= add the following two lines ============== */
label.visibleProperty().bind(column.visibleProperty());
search.visibleProperty().bind(column.visibleProperty());
}
}
}
});

Categories

Resources