2 Replies - 264 Views - Last Post: 02 February 2013 - 07:00 PM Rate Topic: -----

#1 kosmro  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 07-March 12

Printing to new window dilemma

Posted 02 February 2013 - 01:11 AM

Here's the problem.

I have a few different print lines (like a 'for' loop), of which I would like to make display in a new pop-up window. However, I'm not sure how to make it print to the new window.


Here is an example of a section which prints:
/**Handle ShowAll button*/
    private class ShowAll implements ActionListener{
        public void actionPerformed(ActionEvent e){
            for(int c=0; c<counter; c++){
                System.out.println("\n==============================\n" + libarr.get(c));
            }
            System.out.println("\n==============================\n");
        }//action performed
    }//class


And this is what I have for a pop-up window:
JOptionPane.showMessageDialog(null, "Hello " + testingSt);


How do I make the final output from the for loop then display in a new window (like the code just above).


I have searched around, and I can't find anything that would help.

Some help would be great!
Thanks,
~kosmro~

Is This A Good Question/Topic? 0
  • +

Replies To: Printing to new window dilemma

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Printing to new window dilemma

Posted 02 February 2013 - 04:50 AM

Build the entire String object in the loop, I recommend using StringBuilder, and pass the resulting String object (or StringBuilder.toString() object) to the JOptionPane dialog.
Was This Post Helpful? 1
  • +
  • -

#3 kosmro  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 07-March 12

Re: Printing to new window dilemma

Posted 02 February 2013 - 07:00 PM

Ah, cool. Thanks!

Well, the final code I used for this in the end was this:
/**Handle ShowAll button*/
    private class ShowAll implements ActionListener{
        public void actionPerformed(ActionEvent e){
            StringBuilder listAl = new StringBuilder();
            for(int c=0; c<counter; c++){
                listAl.append("\n==============================\n" + libarr.get(c));
            }
            JTextArea textArea = new JTextArea(listAl + "\n==============================\n");
            JScrollPane scrollPane = new JScrollPane(textArea);  
            textArea.setLineWrap(true);  
            textArea.setWrapStyleWord(true); 
            scrollPane.setPreferredSize( new Dimension( 500, 500 ) );
            JOptionPane.showMessageDialog(null, scrollPane, "List of All Items", JOptionPane.PLAIN_MESSAGE);
        }//action performed
    }//class


I made it a scrollable window as the printout would be too big to fit in a fixed window (and allows for growth in the full list).

Thanks GregBrannon for your help! It solved several headaches (similar case for about 5/6 different sections of code :/ ).

Cheers,
~kosmro~
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1