0

I have a QuizApplication which will show up the results upon user finished the test. I have following piece of code which returns quiz result as string:

public String getResult() {
 int score = 0;
    String[] choices;
    String scoreB = "";
    for (int i = 0; i < test.length; i++) {
        if (test[i].getCorrect() == test[i].getAns()) {
            score++;
        } else {
          choices = test[i].getChoice();
            scoreB += "Question " + (i + 1) + " : " + test[i].getQn() + "\n<html></br>choices[test[i].getCorrect() - 1] + "\n\n";
        }
    } // end of for loop
      return result;
}

And then I want to show the result in the form of MessageDialog, so I used:

JOptionPane.showMessageDialog(null, newQuiz.getResult()

But, I want to add a vertical scrollbar onto the JOptionPane.showMessageDialog. How can I do it?

mKorbel
  • 108,320
  • 17
  • 126
  • 296
Karen Goh
  • 124
  • 1
  • 5
  • 19

2 Answers2

3

One of the parameters to showMessageDialog is an Object. If this Object is a Component, it will be added to the dialog's view.

I'm not sure why you want to add JScrollBar to the dialog, but you will need to construct a Component containing all the elements you want to display and pass it to the dialog via the message parameter.

Check out How to use Dialogs for details

MadProgrammer
  • 323,026
  • 21
  • 204
  • 329
1

As a concrete example of @MadProgrammer's suggestion, this example wraps a JTextArea in a JScrollPane and passes the latter to JOptionPane.showMessageDialog(). Also consider this alternative using HTML/CSS.

image

Community
  • 1
  • 1
trashgod
  • 196,350
  • 25
  • 213
  • 918
  • Hi trashgod, I have seen many JTextArea example and it is no good to what I want to achieve. For the JTextArea, you will specify an area like JtextArea output = new JTextArea(200, 200) like this, but I want a dynamic "dialog box" or frame to display my answers and it can exceed a screen size. So, a vertical scrollbar is necessary for a user to see all the answers. – Karen Goh Feb 28 '13 at 02:25
  • Why not a resizable `JDialog` instead of a `JOptionPane`? – trashgod Feb 28 '13 at 02:43