Java 在ListView中用空格填充字符串

Java 在ListView中用空格填充字符串,java,javafx,Java,Javafx,拜托,你能帮我吗 我有java的listview 在ObservableList或ArrayList中,我有如下字符串 CTAudSvc 2760 ctfmon 6176 dllhost 6464 dllhost 14656 DLLML 10920 数字媒体6768 dwm 1104 探险家6492 chrome 2964 但当我把它放到listview中时,我看到了如下内容: CTAudSvc 2760 ctfmon 6176 dllhost 656 DLLML 10920 数字媒体676

拜托,你能帮我吗

我有java的listview 在ObservableList或ArrayList中,我有如下字符串

CTAudSvc 2760
ctfmon 6176
dllhost 6464
dllhost 14656
DLLML 10920
数字媒体6768
dwm 1104
探险家6492
chrome 2964

但当我把它放到listview中时,我看到了如下内容:


CTAudSvc 2760
ctfmon 6176
dllhost 656
DLLML 10920
数字媒体6768
dwm 1104
探险家6492
chrome 2964

在代码中,我并没有什么特别之处,所以若你们知道是什么让它忽略了一些空格,请帮助我

ArrayList<String> processListUnsorted = new ArrayList<String>();
...
Sting input...
processListUnsorted.add(input.trim());
...
List<String> sortedApps = processListUnsorted.stream() .sorted(String.CASE_INSENSITIVE_ORDER)
.collect(Collectors.toList());
...
ObservableList<String> sortedAppsFinal = FXCollections.observableArrayList(sortedApps);
...
somelistview.setItems(sortedAppsFinal);</code>
ArrayList processListUnsorted=new ArrayList();
...
刺痛输入。。。
processListUnsorted.add(input.trim());
...
List SORTEDAPS=processListUnsorted.stream().sorted(String.CASE\u不区分大小写\u顺序)
.collect(Collectors.toList());
...
ObservableList sortedAppsFinal=FXCollections.observableArrayList(sortedApps);
...
setItems(sortedAppsFinal)

这里有一种方法可以实现这一点,但我会采用James\D方法。

你应该考虑使用细胞工厂。分开绳子。然后使用HBox。在HBox中使用两个标签。将第一个标签设置为
HBox.setHgrow(标签“始终”)
设置最大宽度(双倍最大值)

import java.util.ArrayList;
导入java.util.List;
导入javafx.application.application;
导入javafx.collections.FXCollections;
导入javafx.scene.scene;
导入javafx.scene.control.Label;
导入javafx.scene.control.ListCell;
导入javafx.scene.control.ListView;
导入javafx.scene.layout.HBox;
导入javafx.scene.layout.Priority;
导入javafx.stage.stage;
公共类ListViewExperiments扩展了应用程序
{
@凌驾
public void start(Stage primaryStage)引发异常
{
setTitle(“ListView实验1”);
列表数据=新的ArrayList();
添加数据(“CTAudSvc 2760”);
添加数据(“ctfmon 6176”);
添加数据(“dllhost 6464”);
ListView ListView=新建ListView();
setItems(FXCollections.observearraylist(数据));
setCellFactory(lv->new ListCell())
{
标签=新标签();
Label label2=新标签();
HBox HBox=新的HBox(标签,label2);
@凌驾
public void updateItem(字符串项,布尔值为空)
{
super.updateItem(项,空);
if(空){
设置图形(空);
}
否则{
label.setMaxWidth(双倍最大值);
HBox.setHgrow(标签、优先级、始终);
String[]splitString=item.split(\\s+);
label.setText(splitString[0]);
label2.setText(splitString[1]);
设置图形(hBox);
}
}
});
HBox HBox=新的HBox(列表视图);
场景=新场景(hbox,300,120);
初级阶段。场景(场景);
primaryStage.show();
}
公共静态void main(字符串[]args)
{
应用程序启动(args);
}
}

您确定UI中缺少空格还是只是一种非单空格字体?您应该研究使用
Cell Factory
。拆分
字符串
。然后使用
HBox
。在
HBox
中使用两个
标签。将第一个
标签设置为
HBox.setHGrow(“始终”)
它从powershell“Get Process | Format Table-Property Name,ID”中输出的命令。使用适当的类(使用
String
int
属性)表示数据,并将其显示在具有两列的表视图中,这不是更好的方法吗?这也是一个好主意。谢谢你的好主意。谢谢,我会尝试一下。最好是通过在初始值设定项中创建这些节点并根据内容简单地填充它们,从而避免重新创建
HBox
+
标签。此外,即使不需要节点,也可以创建它们。这也应该避免,不管代码是否在
updateItem
方法中。我希望更新涵盖了你提到的所有事情。我已经尝试了你的代码,它工作得非常好。非常感谢您的解决方案。
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;

public class ListViewExperiments extends Application
{

    @Override
    public void start(Stage primaryStage) throws Exception
    {
        primaryStage.setTitle("ListView Experiment 1");

        List<String> data = new ArrayList();
        data.add("CTAudSvc                2760");
        data.add("ctfmon                  6176");
        data.add("dllhost                 6464");

        ListView listView = new ListView();
        listView.setItems(FXCollections.observableArrayList(data));
        listView.setCellFactory(lv -> new ListCell<String>()
        {
            Label label = new Label();              
            Label label2 = new Label();
            HBox hBox = new HBox(label, label2);

            @Override
            public void updateItem(String item, boolean empty)
            {
                super.updateItem(item, empty);
                if (empty) {
                    setGraphic(null);
                }
                else {
                    label.setMaxWidth(Double.MAX_VALUE);
                    HBox.setHgrow(label, Priority.ALWAYS);
                    String[] splitString = item.split("\\s+");
                    label.setText(splitString[0]);
                    label2.setText(splitString[1]);
                    setGraphic(hBox);
                }
            }
        });

        HBox hbox = new HBox(listView);

        Scene scene = new Scene(hbox, 300, 120);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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