0

I have a list that contains Strings and I am trying to set its items as JLabel text, the only problem is that the result is a single line

JButton btnSearch = new JButton("Search");
btnSearch.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent arg0) {
        String fileName = textField.getText();
        StringBuilder result = new StringBuilder();
        List <String> searchResult = new Search().cautaFisiere("C:\\AdwCleaner", fileName);

        for (int x = 0; x < searchResult.size(); x++) {
            result.append(searchResult.get(x)).append("\n");

        }
        lblNewLabel.setText(result.toString());
    }

});

As you can see I tried appending items as new line but no result. I also tried append(System.getProperty("line.separator")) but still no result, the text is still displayed in one line

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
alin dradici
  • 201
  • 2
  • 4
  • 11

1 Answers1

1

As mentioned in here Multiline text in JLabel, just can just use HTML tag <br> to make a new line, but it must be in <html></html> tags.

JButton btnSearch = new JButton("Search");
btnSearch.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent arg0) {
        String fileName = textField.getText();
        StringBuilder result = new StringBuilder();
        List <String> searchResult = new Search().cautaFisiere("C:\\AdwCleaner", fileName);

        for (int x = 0; x < searchResult.size(); x++) {
            result.append(searchResult.get(x)).append("<br>");
        }
        lblNewLabel.setText("<html>" + result.toString() + "</html>");
    }
});
Community
  • 1
  • 1
Vladimír Bielený
  • 2,795
  • 2
  • 11
  • 16