0

I did create the event to get the users' selected multiple values from the array, however, the "\n" - NewLine code on the shown windows popup dialogue does not function. How can I make those in the new line? (I did try the "\r\n" and "System.getProperty("line.separator");" already...

`        //get the selected info
        listSoftw.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                String software = "";
                List<String> SelectedSoftware = listSoftw.getSelectedValuesList();
                for (Object Softwares : SelectedSoftware) {
                    System.getProperty("line.separator");
                    software = software + " - " + Softwares + " \r\n";
                }
                software = software.substring(0, software.length() - 1);
                selectedSoftw.setText(" \n\n" + software + " \n\n");
            }
        });`
`        //ActionListener for receive & display the user input's data
        jbtConfirm.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name = jtfName.getText();
                String ID = jtfID.getText();
                String prog = (String) groupProg.getSelection().getActionCommand();
                String softwares = selectedSoftw.getText();

                int result = JOptionPane.showConfirmDialog(null, "Name: " + name + "\n" + "ID: " + ID + "\n"
                        + "Programme: " + prog + "\n" + "Software selected: \n "
                        + softwares
                        + "\n" + "\n" + "\n Is the information correct? \n", "Check Information",
                        JOptionPane.YES_NO_OPTION,
                        JOptionPane.QUESTION_MESSAGE
                );

                switch (result) {
                    case JOptionPane.YES_OPTION ->
                        //show "Thank you" dialog
                        JOptionPane.showMessageDialog(null, "Thank you.");
                    case JOptionPane.NO_OPTION -> {
                        //exit
                        System.exit(0);
                    }
                    default -> {
                        //show dialog
                    }
                }
            }
        });`

Expected:Problem:

1st image: expected, 2nd image: issue...

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 3
    What type of component is `result`, where you call `result.setText()`? Please provide a [mre]. – maloomeister Mar 23 '22 at 08:53
  • 2
    Unless you're willing to make use of something like a `JTextArea` as message `Object` property, you really should wrap your text in HTML, for [example](https://stackoverflow.com/questions/12702689/joptionpane-displaying-html-problems-in-java/12702858#12702858), [example](https://stackoverflow.com/questions/13926545/display-array-in-joptionpane/13926750#13926750), [example](https://stackoverflow.com/questions/15824033/joptionpane-multidimensional-array-output/15824067#15824067), – MadProgrammer Mar 23 '22 at 09:05
  • A `JTextArea` would allow you to copy the text, if that's important, for [example](https://stackoverflow.com/questions/16409387/joptionpane-output-text-copy/16409519#16409519), [example](https://stackoverflow.com/questions/66404271/joptionpane-creating-resizable-text-and-gui/66404453#66404453); [example](https://stackoverflow.com/questions/36759123/joptionpane-with-text-wrapping-using-custom-swing-component/36759399#36759399) – MadProgrammer Mar 23 '22 at 09:08
  • Hi, @maloomeister I update my code inside already and add some comments as well, sorry if anything makes confused. – Vun Kian Hiung Mar 23 '22 at 09:08
  • @VunKianHiung even though you now renamed the component to `selectedSoftw`, it is still unclear what type this component actually is. Your issue seems to be that this component removes the newlines when you `setText()` on it (afaik a `JTextField` does this...). – maloomeister Mar 23 '22 at 09:12
  • @maloomeister Yeah yeah, that's the `JTextfield` actually. I have defined it at first. – Vun Kian Hiung Mar 23 '22 at 09:16
  • 1
    @VunKianHiung well, then there is your issue. This information should however be available in your question, we should not have to guess this. Don't use a `JTextField` to store your formatted text, since it will filter out the newlines of your text. Store the formatted text somewhere else for your `ActionListener` to use, and/or have a look at the suggestions from MadProgrammer above. – maloomeister Mar 23 '22 at 09:21
  • @maloomeister sure, thanks for the advice. Will think of another method as well as possible. Thanks a lot. – Vun Kian Hiung Mar 23 '22 at 09:35

0 Answers0