Results 1 to 3 of 3
  1. #1
    jhetfield18 is offline Member
    Join Date
    Oct 2007
    Posts
    9
    Rep Power
    0

    Default About JOptionPane.showMessageDialog

    Hi I was wondering if I could use a for(){} in a JOptionPane.showMessageDialog.To explain,let's say I have an array A,full of integers,and have done two different equations using the numbers from A and stored the the results in two arrays B,C one for each equation.Can I use one message box to print all the results in a form like this:
    Number: ......
    Result for equation no.1: ......
    Result for equation no.2: ......

    So it becomes like this in the message box:


    Number: ......
    Result for equation no.1: ......
    Result for equation no.2: ......


    Number: ......
    Result for equation no.1: ......
    Result for equation no.2: ......


    Number: ......
    Result for equation no.1: ......
    Result for equation no.2: ......


    Number: ......
    Result for equation no.1: ......
    Result for equation no.2: ......

    ..................................


    Can I do this????Is there any way?Perhaps another message dialog command from another library that supports many results?

    Regards,Nick

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    JLabel supports html 3.2 (roughly).
    Another option is to make up your own JDialog for this.
    Java Code:
    // intiialize your_components
    // add listeners as needed
    JDialog dialog = new JDialog(new Frame());
    dialog.getContentPane().add(your_components)
    dialog.pack();
    dialog.setLocation(...)
    dialog.setVisible(true);
    // carry on as desired..
    Java Code:
    import javax.swing.*;
    import java.util.Arrays;  // j2se 1.5+
    
    public class OptionsTest {
        public static void main(String[] args) {
            int[] data = { 2, 3, 4, 5 };
            int[] squares = getSquares(data);
            int[] cubes = getCubes(data);
            System.out.printf("squares = %s%n", Arrays.toString(squares));
            System.out.printf("cubes   = %s%n", Arrays.toString(cubes));
            String s = formatResults(data, squares, cubes);
            JOptionPane.showMessageDialog(null, new JLabel(s), "Results",
                                          JOptionPane.PLAIN_MESSAGE);
        }
    
        static private int[] getSquares(int[] input) {
            int[] out = new int[input.length];
            for(int j = 0; j < input.length; j++) {
                out[j] = input[j]*input[j];
            }
            return out;
        }
    
        static private int[] getCubes(int[] input) {
            int[] out = new int[input.length];
            for(int j = 0; j < input.length; j++) {
                out[j] = input[j]*input[j]*input[j];
            }
            return out;
        }
    
        static String formatResults(int[] input, int[] one, int[] two) {
            // StringBuffer is the legacy alternative for j2se 1.4-.
            StringBuilder sb = new StringBuilder("<html>");
            for(int j = 0; j < input.length; j++) {
                sb.append("Number: " + input[j] + "<br>");
                sb.append("Result for equation no.1: " + one[j] + "<br>");
                sb.append("Result for equation no.2: " + two[j] + "<br><br>");
            }
            return sb.toString();
        }
    }

  3. #3
    jhetfield18 is offline Member
    Join Date
    Oct 2007
    Posts
    9
    Rep Power
    0

    Default

    I guess that the new dialog idea would be better since i don't know html apart from a little here and there.So I'll search around and try that
    Thx man!!!!!

Similar Threads

  1. JOptionPane (customizing)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-14-2008, 12:39 PM
  2. JOptionPane dialog (Localizing)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-14-2008, 12:36 PM
  3. About JOptionPane.showMessageDialog
    By jhetfield18 in forum Advanced Java
    Replies: 0
    Last Post: 11-02-2007, 11:56 AM
  4. problems with JOptionPane
    By oregon in forum AWT / Swing
    Replies: 2
    Last Post: 08-05-2007, 06:58 PM
  5. Replies: 1
    Last Post: 07-31-2007, 05:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •