Added working ClearableTextField

additionally designed the clear button icon both in black and white
This commit is contained in:
delvh 2020-06-27 15:35:35 +02:00
parent 6754465aff
commit 63aa7859d3
3 changed files with 252 additions and 0 deletions

View File

@ -0,0 +1,252 @@
package envoy.client.ui;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.StringProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Background;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import envoy.client.data.Settings;
/**
* This class offers a text field that is automatically equipped with a clear
* button.
* <p>
* Project: <strong>envoy-client</strong><br>
* File: <strong>ClearableTextField.java</strong><br>
* Created: <strong>25.06.2020</strong><br>
*
* @author Leon Hofmeister
* @since Envoy Client v0.1-beta
*/
@SuppressWarnings("javadoc")
public class ClearableTextField extends GridPane {
private final TextField textField;
private final Button clearButton;
/**
* Constructs a new {@code ClearableTextField} with no initial text and icon
* size 16.
*
* @since Envoy Client v0.1-beta
*/
public ClearableTextField() { this("", 16); }
/**
* Constructs a new {@code ClearableTextField} with initial text and a
* predetermined icon size.
*
* @param text the text that should be displayed by default
* @param size the size of the icon
* @since Envoy Client v0.1-beta
*/
public ClearableTextField(String text, int size) {
// initializing the textField and the button
textField = new TextField(text);
clearButton = new Button("",
new ImageView(IconUtil.load(
Settings.getInstance().getCurrentTheme().equals("dark") ? "/icons/clear_button_white.png" : "/icons/clear_button_black.png",
size)));
clearButton.setOnAction(e -> textField.clear());
clearButton.getStyleClass().clear();
clearButton.setBackground(Background.EMPTY);
// Adding the two elements to the GridPane
add(textField, 0, 0, 2, 1);
add(clearButton, 1, 0, 1, 1);
// Setting the percent - widths of the two columns.
// Used to locate the button on the right.
final var columnConstraints = new ColumnConstraints();
columnConstraints.setPercentWidth(85);
getColumnConstraints().add(columnConstraints);
final var columnConstraints2 = new ColumnConstraints();
columnConstraints2.setPercentWidth(15);
columnConstraints2.setPrefWidth(size);
getColumnConstraints().add(columnConstraints2);
setAlignment(Pos.CENTER);
setHgap(10);
setVgap(10);
setPadding(new Insets(5, 5, 5, 5));
setMargin(clearButton, new Insets(5));
}
/**
* @return the underlying {@code textField}
* @since Envoy Client v0.1-beta
*/
public TextField getTextField() { return textField; }
/**
* @return
* @see javafx.scene.control.TextInputControl#fontProperty()
* @since Envoy Client v0.1-beta
*/
public final ObjectProperty<Font> fontProperty() { return textField.fontProperty(); }
/**
* @return
* @see javafx.scene.control.TextField#prefColumnCountProperty()
* @since Envoy Client v0.1-beta
*/
public final IntegerProperty prefColumnCountProperty() { return textField.prefColumnCountProperty(); }
/**
* @return
* @see javafx.scene.control.TextField#getPrefColumnCount()
* @since Envoy Client v0.1-beta
*/
public final int getPrefColumnCount() { return textField.getPrefColumnCount(); }
/**
* @param value
* @see javafx.scene.control.TextField#setPrefColumnCount(int)
* @since Envoy Client v0.1-beta
*/
public final void setPrefColumnCount(int value) { textField.setPrefColumnCount(value); }
/**
* @return
* @see javafx.scene.control.Control#skinProperty()
* @since Envoy Client v0.1-beta
*/
public final ObjectProperty<Skin<?>> skinProperty() { return textField.skinProperty(); }
/**
* @param value
* @see javafx.scene.control.TextInputControl#setFont(javafx.scene.text.Font)
* @since Envoy Client v0.1-beta
*/
public final void setFont(Font value) { textField.setFont(value); }
/**
* @return
* @see javafx.scene.control.TextInputControl#getFont()
* @since Envoy Client v0.1-beta
*/
public final Font getFont() { return textField.getFont(); }
/**
* @return
* @see javafx.scene.control.TextInputControl#promptTextProperty()
* @since Envoy Client v0.1-beta
*/
public final StringProperty promptTextProperty() { return textField.promptTextProperty(); }
/**
* @return
* @see javafx.scene.control.TextInputControl#getPromptText()
* @since Envoy Client v0.1-beta
*/
public final String getPromptText() { return textField.getPromptText(); }
/**
* @param value
* @see javafx.scene.control.TextInputControl#setPromptText(java.lang.String)
* @since Envoy Client v0.1-beta
*/
public final void setPromptText(String value) { textField.setPromptText(value); }
/**
* @return
* @see javafx.scene.control.TextInputControl#getText()
* @since Envoy Client v0.1-beta
*/
public final String getText() { return textField.getText(); }
/**
* @param value
* @see javafx.scene.control.TextInputControl#setText(java.lang.String)
* @since Envoy Client v0.1-beta
*/
public final void setText(String value) { textField.setText(value); }
/**
* @return
* @see javafx.scene.control.TextInputControl#textProperty()
* @since Envoy Client v0.1-beta
*/
public final StringProperty textProperty() { return textField.textProperty(); }
/**
* @param value
* @see javafx.scene.control.TextInputControl#setEditable(boolean)
* @since Envoy Client v0.1-beta
*/
public final void setEditable(boolean value) { textField.setEditable(value); }
/**
* @return
* @see javafx.scene.control.Control#tooltipProperty()
* @since Envoy Client v0.1-beta
*/
public final ObjectProperty<Tooltip> tooltipProperty() { return textField.tooltipProperty(); }
/**
* @param value
* @see javafx.scene.control.Control#setTooltip(javafx.scene.control.Tooltip)
* @since Envoy Client v0.1-beta
*/
public final void setTooltip(Tooltip value) { textField.setTooltip(value); }
/**
* @return
* @see javafx.scene.control.Control#getTooltip()
* @since Envoy Client v0.1-beta
*/
public final Tooltip getTooltip() { return textField.getTooltip(); }
/**
* @return
* @see javafx.scene.control.Control#contextMenuProperty()
* @since Envoy Client v0.1-beta
*/
public final ObjectProperty<ContextMenu> contextMenuProperty() { return textField.contextMenuProperty(); }
/**
* @param value
* @see javafx.scene.control.Control#setContextMenu(javafx.scene.control.ContextMenu)
* @since Envoy Client v0.1-beta
*/
public final void setContextMenu(ContextMenu value) { textField.setContextMenu(value); }
/**
* @return
* @see javafx.scene.control.Control#getContextMenu()
* @since Envoy Client v0.1-beta
*/
public final ContextMenu getContextMenu() { return textField.getContextMenu(); }
/**
* @param minWidth
* @param minHeight
* @see javafx.scene.layout.Region#setMinSize(double, double)
* @since Envoy Client v0.1-beta
*/
@Override
public void setMinSize(double minWidth, double minHeight) { textField.setMinSize(minWidth, minHeight); }
/**
* @param prefWidth
* @param prefHeight
* @see javafx.scene.layout.Region#setPrefSize(double, double)
* @since Envoy Client v0.1-beta
*/
@Override
public void setPrefSize(double prefWidth, double prefHeight) { textField.setPrefSize(prefWidth, prefHeight); }
/**
* @param maxWidth
* @param maxHeight
* @see javafx.scene.layout.Region#setMaxSize(double, double)
* @since Envoy Client v0.1-beta
*/
@Override
public void setMaxSize(double maxWidth, double maxHeight) { textField.setMaxSize(maxWidth, maxHeight); }
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB