0

At first, im not a native speaker, so please excuse my mistakes ^^

I am writing a program that gets all primes up to a specific integer and want to print all primes in the end at a JOptionPane.

Actually, i do calculate all primes in a seperate function, return them all in one integer array, safe this array into a string and then print the string in the JOptionPane.

But unfortunately, I just get only one!! extremely long line where all the integers are printed, crossing both monitors. So when I print for example all primes up to 10000, I get all the primes in one line.

I want, that for example only 100 primes are in one line, and then the rest gets printed in the next line. But how can I achieve this?

    check_button1.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

                int x = Integer.parseInt(InputField1.getText());
                int prime_numbers[];


                String all_prime_String = Arrays.toString(number_theory.primes(x));
                System.out.println(Arrays.toString(number_theory.primes(x)));


                JOptionPane.showMessageDialog(Math_Collection_Gui.this, all_prime_String, "Ergebnis",  JOptionPane.INFORMATION_MESSAGE);

        }
    });

The number_theory.primes(x) function returns the array of all primes up to x, and then i safe this array in a string.

Like i said, my problem is, that all values of the array get printed in one line. So is it possible, to fix the lenght of the box or something, that the program is forced to jump to the next line? Or do you got any other tricks for me?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
HyperPro
  • 103
  • 2
  • btw, I just forgot to delete the line "int prime_numbers[];", it has no use – HyperPro Sep 10 '19 at 20:28
  • Possible [related](https://stackoverflow.com/questions/41904362/multiple-joptionpane-input-dialogs/41904856#41904856), btw, you can [edit] your question to remove that code that has no use – Frakcool Sep 10 '19 at 20:35

1 Answers1

0

The easiest choice is probably a read-only text area:

JTextArea textArea = new JTextArea(all_prime_String, 8, 20);
textArea.setLineWrap(true);
textArea.setEditable(false);

JOptionPane.showMessageDialog(Math_Collection_Gui.this, new JScrollPane(textArea), "Ergebnis",  JOptionPane.INFORMATION_MESSAGE);

There are certainly more readable choices, such as placing each value in a JList row, if you want to bother.

If you want to place some text underneath the text area, pass an Object array as the JOptionPane’s message:

Object[] message = {
    new JScrollPane(textArea),
    "Additional text here",
};
JOptionPane.showMessageDialog(Math_Collection_Gui.this, message, "Ergebnis",  JOptionPane.INFORMATION_MESSAGE);
VGR
  • 40,506
  • 4
  • 48
  • 63
  • Thank you, it works like this! But do you know if there is any way, to combine this scrollpane with even more text? So that i have the Scrollpane and then 1-2 lines "normal text" under it? – HyperPro Sep 10 '19 at 20:38
  • @HyperPro Updated answer. You can pass multiple message parts by passing an object array containing those parts. – VGR Sep 11 '19 at 00:35