- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 swing 应用程序,它覆盖 javax.swing.text.Document 以将基础文档的内容限制为某些字符和文本长度。我想将我的应用程序移植到 Javafx,但我不知道此类是否有 Javafx 等效项。有没有办法在Javafx中替换它?
我的类正在做的是包装文档以添加新功能(在我的情况下是有限的)。许多方法仅遵循我使用的底层文档,但其中一些方法增加了我的限制,这些限制是:
行长度限制
行数限制
大写限制
字符限制
public class ConstrainedDocument implements Document {
protected Document document = null;
private boolean limitLength = false;
private boolean limitLines = false;
private int maxLength = -1;
private int maxLines = -1;
private boolean limitText = false;
private String allowedText = null;
private boolean uppercase = false;
/**
* Constructor.
*
* @param doc the document
*/
public ConstrainedDocument(Document doc) {
this.document = doc;
}
/**
* Constructor.
*/
public ConstrainedDocument() {
this.document = new PlainDocument();
}
public Document getDocument() {
return document;
}
/**
* Limits the allowed length for each line of the document.
*
* @param maxLength the maximum line length
*/
public void setMaximumLength(int maxLength) {
this.maxLength = maxLength;
this.limitLength = true;
}
/**
* Limits the allowed number of lines for the document.
*
* @param maxLines the maximum number of lines
*/
public void setMaximumLines(int maxLines) {
this.maxLines = maxLines;
this.limitLines = true;
}
/**
* Limits the characters allowed to compose the document.
*
* @param allowedText the String defining the allowed characters
*/
public void setAllowedText(String allowedText) {
this.allowedText = allowedText;
this.limitText = true;
}
/**
* Forces only upper case.
*
* @param uppercase true if only upper case are forced
*/
public void setUpperCase(boolean uppercase) {
this.uppercase = uppercase;
}
/**
* Resets all the limitations and settings for this document.
*/
public void resetLimitations() {
this.limitText = false;
this.limitLength = false;
this.limitLines = false;
}
/**
* Returns the allowed length for each line of the document.
*
* @return the maximum line length
*/
public int getMaximumLineLength() {
return maxLength;
}
/**
* Returns the allowed number of lines for the document.
*
* @return the maximum number of lines
*/
public int getMaximumLines() {
return maxLines;
}
/**
* Returns true if the maximum length of the text is set.
*
* @return true if the maximum length of the text is set
*
* @see #getMaximumLineLength()
*/
public boolean isLineLengthLimited() {
return limitLength;
}
/**
* Returns true if the number of allowed lines is limited.
*
* @return true if the maximum number of lines is set
*
* @see #getMaximumLines()
*/
public boolean isLinesLimited() {
return limitLines;
}
/**
* Returns the characters allowed to compose the document.
*
* @return the allowed characters as a string
*/
public String getAllowedText() {
return allowedText;
}
/**
* Returns true if the allowed characters are limited.
*
* @return true if the allowed characters are limited
*
* @see #getAllowedText()
*/
public boolean isTextLimited() {
return limitText;
}
/**
* Returns true if the insertion is forced to upper case characters.
*
* @return true if the insertion is forced to upper case characters
*/
public boolean isUpperCase() {
return uppercase;
}
/**
* Extends the remove method from the parent Document class to fire remove events
* when lines are removed.
*/
@Override
public void remove(int offset, int length) throws BadLocationException {
document.remove(offset, length);
}
/**
* Extends the insertString method from the parent Document class. This adds :
* <ul>
* <li>the ability to force upper case characters insertion if the {@link #isUpperCase()}
* property is set</li>
* <li>the ability to block insertion of unauthorized characters if the {@link #isTextLimited()}
* property is set. In that case, the text is limited to the {@link #getAllowedText()} characters</li>
* <li>the ability to limit the number of lines if the {@link #isLinesLimited()} property is set. In
* that case, the number of allowed lines is limited to {@link #getMaximumLines()}</li>
* <li>the ability to limit the length of each line if the {@link #isLineLengthLimited()} property
* is set. In that case, the length for each line is limited to {@link #getMaximumLineLength()}</li>
* </ul>
* <p>
* It also fire insertion events when lines are inserted.</p>
*/
@Override
public void insertString(int offset, String inputStr, AttributeSet a) throws BadLocationException {
String str = inputStr;
// handle showSpaces, upper case and characters limitation
if (uppercase || limitText) {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (uppercase) {
c = Character.toUpperCase(c);
}
if (limitText) {
if (allowedText.indexOf(c) != -1) {
buf.append(c);
}
} else {
buf.append(c);
}
}
str = buf.toString();
}
Element root = document.getDefaultRootElement();
AbstractDocument.LeafElement elt = (AbstractDocument.LeafElement) root.getElement(root.getElementIndex(offset));
// Gets the text to insert, taking care of adding new lines in the process
str = getTextToInsert(root, elt, str);
if (str.length() > 0) {
document.insertString(offset, str, a);
}
// if only the length of the text is limited
}
/**
* Returns the text to insert on the current line.
*
* @param root the root element of the Document
* @param elt the leaf element, interface the line of text where to insert the text
* @param t the String to insert
*/
protected String getTextToInsert(Element root, AbstractDocument.LeafElement elt, String t) {
// Gets the length of the line where to insert the text
int begin = elt.getStartOffset();
int end = elt.getEndOffset();
int length = end - begin - 1;
// Gets the number of lines
int lineCount = root.getElementCount();
// iterate through the text to insert, taking care of the line length
// and number of lines limitation
StringBuilder buf = new StringBuilder();
for (int i = 0; i < t.length(); i++) {
char c = t.charAt(i);
// if this is a new line, we must check if we are allowed to add a new line
if (c == '\n') {
// we are not allowed to add a new line
if (limitLines && lineCount >= maxLines) {
break;
} else {
// ok, we can add a new line
buf.append(c);
lineCount++;
length = 0;
}
// this is not a new line character, we add the character if we can
} else {
// if no line length limitation, we just add the character
if (!limitLength) {
buf.append(c);
} else if (length >= maxLength) {
// else current length >= maximum line length, we try to add a new line
// not allowed, finish to add
if (limitLines && lineCount >= maxLines) {
break;
} else {
// adding a new line is allowed, there is remaining available lines
buf.append('\n').append(c);
lineCount++;
length = 1;
}
// current length < maximum line length, we just add the character
} else {
buf.append(c);
length++;
}
}
}
return buf.toString();
}
/**
* Return the text of this Document. Special spaces characters will be trasnformed to "real" spaces.
*/
@Override
public String getText(int offset, int length) throws BadLocationException {
String text = document.getText(offset, length);
return text;
}
/**
* get the offset position in the Document for the selected line.
*
* @param line the selected line
* @return the offset position.
*/
public int getOffsetPosition(int line) {
int offset = 0;
int count = 0;
if (line == 0) {
return offset;
}
Element root = document.getDefaultRootElement();
try {
for (int i = 0; i < root.getElementCount(); i++) {
AbstractDocument.LeafElement elt = (AbstractDocument.LeafElement) root.getElement(i);
offset = elt.getStartOffset();
String text = getText(elt.getStartOffset(), elt.getEndOffset() - elt.getStartOffset());
if (count == line) {
break;
}
if (text.charAt(text.length() - 1) == '\n') {
count++;
}
}
} catch (BadLocationException e) {
e.printStackTrace();
}
return offset;
}
@Override
public int getLength() {
return document.getLength();
}
@Override
public void render(Runnable runnable) {
document.render(runnable);
}
@Override
public void addDocumentListener(DocumentListener listener) {
document.addDocumentListener(listener);
}
@Override
public void removeDocumentListener(DocumentListener listener) {
document.removeDocumentListener(listener);
}
@Override
public void addUndoableEditListener(UndoableEditListener listener) {
document.addUndoableEditListener(listener);
}
@Override
public void removeUndoableEditListener(UndoableEditListener listener) {
document.removeUndoableEditListener(listener);
}
@Override
public Element getDefaultRootElement() {
return document.getDefaultRootElement();
}
@Override
public Element[] getRootElements() {
return document.getRootElements();
}
@Override
public Position getEndPosition() {
return document.getEndPosition();
}
@Override
public Position getStartPosition() {
return document.getStartPosition();
}
@Override
public Position createPosition(int i) throws BadLocationException {
return document.createPosition(i);
}
@Override
public void getText(int i, int i0, Segment segment) throws BadLocationException {
document.getText(i, i0, segment);
}
@Override
public Object getProperty(Object key) {
return document.getProperty(key);
}
@Override
public void putProperty(Object key, Object value) {
document.putProperty(key, value);
}
}
最佳答案
可以使用 TextFormatter
在 JavaFX 中实现此功能.
例如,这个快速示例实现了最大文本长度。 Swing 示例中的其他功能可以类似地实现:
package org.jamesd.examples.maxtextlength;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Spinner;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextFormatter;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class HelloApplication extends Application {
@Override
public void start(Stage stage) {
TextArea textArea = new TextArea();
Spinner<Integer> maxLengthSpinner = new Spinner<>(10, 200, 100);
maxLengthSpinner.valueProperty().addListener((obs, oldLength, newLength) ->
textArea.setText(textArea.getText().substring(0, Math.min(newLength, textArea.getLength())))
);
textArea.setTextFormatter(new TextFormatter<String>(change -> {
int proposedTextLength = change.getControlNewText().length();
if (proposedTextLength > maxLengthSpinner.getValue()) {
// truncate text to maximum allowed length:
int newTextLength = change.getText().length();
int delta = proposedTextLength - maxLengthSpinner.getValue();
change.setText(change.getText().substring(0, newTextLength - delta));
// adjust caret:
change.setCaretPosition(change.getCaretPosition() - delta);
change.setAnchor(change.getAnchor() - delta);
}
return change;
}));
Label length = new Label();
length.textProperty().bind(textArea.textProperty().map(String::length).map(len -> "Length: "+len));
length.setPadding(new Insets(5));
HBox controls = new HBox(5, new Label("Max text length:"), maxLengthSpinner);
controls.setPadding(new Insets(5));
controls.setAlignment(Pos.CENTER);
BorderPane root = new BorderPane(textArea);
root.setBottom(length);
root.setTop(controls);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
我有时发现 TextFormatter
的使用有点不直观(尽管它非常强大)。您可能想引用这个excellent tutorial .
关于swing - Javafx 替代 swing javax.swing.text.Document,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77193601/
我不知道这是不是问这种问题的最佳地点, 我看到一些 JavaFX伙计们,重新标记 一些问题通过替换 javafx来自 javafx-2并采用新的 javafx-8 .它被弃用了还是什么? 编辑 : 不
错误本身: Error:java: invalid flag: --add-modules=javafx.fxml,javafx.graphics,javafx.controls,javafx.bas
这个想法是让一个应用程序在每个显示器上显示两个不同的窗口(阶段),该应用程序应该知道计算机有多少个显示器及其分辨率。 javafx有可能吗? 最佳答案 对于当前版本的 JavaFX (2.2),您可以
我正在将我的项目从 javafx 1.3 转换为 javafx 2.1。但我对 javafx.lang 有疑问包裹。 最佳答案 JavaFX 1.3 lang 包内容被拆分并移至下一个位置: 时长变为
当我尝试将标签添加到 gridpane 中时,如第二张图片所示,它不起作用。我已经尝试了很多东西,比如添加 CSS,但它仍然无法正常工作。为什么第 113 和 114 行不起作用? (opcje.se
我有一个JavaFX ContextMenu分配给滚动面板的鼠标右键单击。它会打开,但在滚动 Pane 外部单击时不会关闭。我可以在滚动 Pane 中添加另一个鼠标事件以将其隐藏,但这只能解决1个问题
我有一个tableview,其中附有一个可观察到的自定义类对象的列表(类类型:SalesInvoiceNetSale)。该表中的所有数据都可以正常显示。可观察列表中的最后一项是总计行(类类型:Sale
关闭。这个问题需要更多 focused .它目前不接受答案。 想改进这个问题?更新问题,使其仅关注一个问题 editing this post . 2年前关闭。 Improve this questi
我想知道如何在JavaFX中绘制半圆。我尝试使用Shape和QuadCurve,但无法制作出完美的半圆。 这是我要绘制的图片: 最佳答案 您链接的图片实际上是一个半圆环。您可以通过绘制嵌套的2条圆弧和
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我正在寻找 JavaFX 支持的图像类型(最新)列表,例如PNG、JPEG、TIFF。不同的搜索引擎没有帮助......知道从哪里开始吗? 更特别的是,我对 16 位灰度图像(不同格式)和罕见的受支持
我希望在 javafx 中让标签每 0.1 秒闪烁一次。文本显示在后台运行的 ImageView gif 的顶部。我将如何去做,或者您对最佳方法有什么建议? 谢谢 最佳答案 @fabian 的解决方案
我需要测试所选项目的值以调用不同的方法,因此我编写了添加侦听器的代码,但是该代码生成语法错误 @FXML private JFXComboBox cmbComp; cmbComp.valuePrope
我正在 Javafx 中编写一个非常简单的应用程序,其中舞台上有一个带有文本框的按钮作为一个场景。现在,我想要的行为是,当我单击按钮时,我可以使用另一个按钮加载另一个场景和舞台上的一个文本框,然后删除
编辑:如果用户单击“删除”以删除 ListView 中的项目,我会弹出一个警告框。它有效,但我希望它能超越原来的舞台。它出现在我的第一台显示器上。有什么方法可以设置警报显示时的位置吗? 请注意,“所有
我想使用 JavaFX 编写一个笔画绘图应用程序。我有一个压敏绘图板,如果能够读取笔的压力和倾斜值,那就太好了。 JavaFX 有一个 API 可以处理鼠标、触摸和滑动输入,但似乎没有任何东西可以产生
我在 JavaFX 中使用条形图和折线图。当我使两个图表大小相同并将它们放在同一位置时,它们完美地相互重叠。我如何使折线图显示在条形图的顶部。 目前我已将它们的不透明度设置为 0.7,这样它们“看起来
此问题与 this 相关。现在我想为字段值等于某个值的行着色。 @FXML private TableView tv_mm_view; @FXML private Ta
我有一个程序,可以生成高度图(0-255 的整数的 2D 数组),并使用 Shape3D“Box”对象为每个“像素”构建 3D View ,其高度与其在高度图中的值成比例。这会创建一个看起来很酷的四四
我想为 JavaFX 创建上下文菜单。这是我测试过的代码。但是由于某种原因,当我右键单击树节点时没有上下文菜单。你能帮我找出我的错误吗。 import java.util.Arrays; import
我是一名优秀的程序员,十分优秀!