Search code examples
javaswingprintingawtjoptionpane

Java: How to print to a scrollable dialog using a void Print() function?


I am trying to print in scrollable dialog box using a void print() function but I can't seem to figure this out. Is it possible to use the function as is?

       rslt = JOptionPane.showConfirmDialog(this,
                                            tempPanel,
                                            univ.printList,
                                            JOptionPane.OK_CANCEL_OPTION,
                                            JOptionPane.PLAIN_MESSAGE);

Solution

  • There are probably a few dozen ways you could do something like this...

    Start by taking a look at How to Make Dialogs for more details

    The following just creates a html based list of the items...

    public void showProfessorList(){
        StringBuilder sb = new StringBuilder(25);
        sb.append("<html>");
        for (int i=0; i < professorList.size(); i++){
            sb.append(professorList.get(i).getName())).append("<br>");
        }
        JOptionPane.showConfirmDialog(this,
                                      new JScrollPane(new JLabel(sb.toString),
                                      "Behold",
                                      JOptionPane.OK_CANCEL_OPTION,
                                      JOptionPane.PLAIN_MESSAGE);    
    }
    

    You could get fancy and use a JList instead, but that comes down to your needs...