【问题标题】:Delete lines of text in JavaFX删除 JavaFX 中的文本行
【发布时间】:2019-03-04 01:29:35
【问题描述】:

我已经使用 JavaFX 大约两个星期了,我在删除行时遇到了 TextArea 的问题。我在 TextArea 中附加了以下信息,但如何删除特定行?

  • 玛丽·约翰逊 44.33
  • 劳拉·史密斯 55.12
  • 詹姆斯查尔斯 23.56

如何删除包含 Laura Smith 55.12 的行并将其他两个保留在那里? 我得到了第一个角色,但我不知道从那里去哪里。请帮忙。

for (String line : reservationBox.getText().split("\n")) {
    if(line.contains(nameText.getText() + " " + priceText.getText())) {
    char firstCharacter = nameText.getText().charAt(0); //get character of the first letter of the name
    reservationBox.deleteText( ?? );
    }
}

【问题讨论】:

  • 您希望在每种情况下都只删除第二行?
  • 没有。这取决于名称文本。如果nameText是James Charles,那么我要删除第三行,但是我不知道如何获取该行的第一个和最后一个索引,让程序知道要删除哪一行(从哪里开始,从哪里结束使用 .deleteText() )
  • 一般的想法是找到索引范围然后调用TextInputControl.deleteText(int,int)TextInputControl.deleteText(IndexRange)
  • 这就是我想的Slaw,但我不知道如何获取索引范围。如果我使用 int index1 = line.indexOf(firstCharacter);它采用整个文本的第一个字符,而不是名称的开头。
  • 旁注:这个功能看起来更适合ListView而不是TextArea

标签: java javafx


【解决方案1】:
  1. 初始化两个变量,作为开始和结束索引。
  2. 第一个索引将引用我们需要删除的单词的第一个字母。
  3. 最后一个索引将是起始索引和我们需要的单词长度之和减去 1。
  4. 使用deleteText(startIndex, endIndex) 删除文本。

看看下面的代码:

    public void start(Stage primaryStage) throws Exception {

        int indexStart = 0; //initialize variables
        int indexEnd = 0;

        TextArea textArea = new TextArea();
        textArea.setText("123\nhello\nabc\ntwentyone\n"); //initialize text in box

        VBox vbox = new VBox(textArea);

        Scene scene = new Scene(vbox, 200, 100);
        primaryStage.setScene(scene);
        primaryStage.show();

        for(String line : textArea.getText().split("\n")){
            if(line.contains("abc")) { //change this to whatever you need
                indexStart = textArea.getText().indexOf(line.charAt(0)); 
                indexEnd = indexStart + line.length()-1; 
            }

            textArea.deleteText(indexStart, indexEnd); //Delete between indexes
        }
    }

    public static void main(String[] args) {
        Application.launch(args);
    }

【讨论】:

    【解决方案2】:

    看看下面这个简单的应用程序,并将所有需要的应用到您的代码中:

    /**
     *
     * @author Momir Sarac
     */
    public class DeleteSpecificLineFromTextArea extends Application {
    
        @Override
        public void start(Stage primaryStage) {
    
            TextArea textArea = new TextArea();
            Button button = new Button("Get it");
    
    
            textArea.setText("Adam Smith 32\nJenny Curry 52\nTako Yoshimoto 56\n");
    
            String nameText = "Jenny Curry 52";
    
             button.setOnAction(new EventHandler<ActionEvent>() {
    
                @Override
                public void handle(ActionEvent event) {
    
            for (String line : textArea.getText().split("\n")) {
                if (line.contains(nameText)) {
                    textArea.setText(textArea.getText().replace(line, ""));
                }
            }
            textArea.setText(textArea.getText().replaceAll("[\\\\\\r\\\\\\n]+", "\n"));
                }
            });
    
            StackPane root = new StackPane();
            root.getChildren().addAll(textArea,button);
    
            Scene scene = new Scene(root, 300, 250);
    
            primaryStage.setTitle("Hello World!");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            launch(args);
        }
    
    }
    

    【讨论】:

    • 我完全没有想到替换方法。我在我的代码中修复了我需要的东西并且它起作用了。谢谢。
    • 快乐编码,@nemohack
    猜你喜欢
    • 2016-10-22
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 2014-04-29
    • 2014-09-06
    • 2011-09-22
    相关资源
    最近更新 更多