converting to JOptionPane


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: converting to JOptionPane

  1. #1
    Join Date
    Jul 2005
    Location
    malaysia
    Posts
    6

    Unhappy converting to JOptionPane

    hi, all, ok, i admit, that this is my assignment, but i really can't crack the answer. it requires me to convert the scripts below to a JOptionPane... how can it be possible, thank you in advance for those who are willing to help:

    Code:
    //import javax.swing.JOptionPane;
    import java.io.*;
    
    public class Q7
    {
      public static void main (String[]args) throws IOException
      {
      	String que,temp;
        int triangle;
        BufferedReader DataMasuk;
        DataMasuk = new BufferedReader (new InputStreamReader (System.in));
        System.out.println ("Key in an integer triangle:");
        //System.out.print("Key in an integer triangle: ");
        triangle = Integer.parseInt(DataMasuk.readLine());
    
      {
    
    	  for(int x=0; x<triangle; x++)
    	    {
    		  for(int y=0; y<=x; y++)
    		    {
    			  System.out.print("*");
    			  //temp = "*";
    		    }
    		   System.out.println(" ");
    	    }
    
      }
    }
    }

  2. #2
    Join Date
    Jul 2005
    Location
    malaysia
    Posts
    6
    anybody???

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Read code carefully and understand it before you finish, mkay ?

    Code:
    import javax.swing.*;
    
    public class Q7 {
    
      public Q7() {} // empty default constructor
    
      public void doTriangle() {
        try {
          int triangle = getUsersIntegerEntry();
          printTriangle(triangle);
        }
        catch (Exception ex) {  
          // user cancelled, do nothing
        }
      }
      /**
       * Show input dialog, loops with error dialog until the user
       * gets it right.  If user cancels the parse will throw a
       * nullPointerException that is caught by the exception handler
       * in the doTriangle method
       */
      private int getUsersIntegerEntry() throws Exception {
        while (true) {
          String s = JOptionPane.showInputDialog(null,
                                                  "Key in an integer triangle",
                                                  new Integer(0)); // could also be null
          try {
            return Integer.parseInt(s);
          }
          catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(null,
                                          s+" is not an integer",
                                          "Input error",
                                          JOptionPane.ERROR_MESSAGE);
          }
        }
      }
      /**
       * Do triangle output
       */
      private void printTriangle(int triangle) {
        for (int x = 0; x < triangle; x++) {
          for (int y = 0; y <= x; y++) {
            System.out.print("*");
            //temp = "*";
          }
          System.out.println(" ");
        }
      }
      /**
       * Main, just creates a Q7 instance and invokes Q7's
       * only public method.
       */
      public static void main(String[] args) {
        Q7 trallala=new Q7();
        trallala.doTriangle();
      }
    }
    Last edited by sjalle; 07-26-2005 at 05:04 AM.
    eschew obfuscation

  4. #4
    Join Date
    Jul 2005
    Location
    malaysia
    Posts
    6
    thank you very much for the help, but i need the output in the JOptionPane format too... that's what bothers the most, i don't know how to do it...

  5. #5
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    I don't think you can get that output in the 'JOptionPane format', infact I'm not sure
    what you mean by that. Do you mean draw the triangle in a panel in a frame ?
    eschew obfuscation

  6. #6
    Join Date
    Jul 2005
    Location
    malaysia
    Posts
    6
    Quote Originally Posted by sjalle
    I don't think you can get that output in the 'JOptionPane format', infact I'm not sure
    what you mean by that. Do you mean draw the triangle in a panel in a frame ?
    sorry for the cofusion, yes, i need it in a panel

  7. #7
    Join Date
    Jul 2005
    Posts
    22
    Yes it is possible to display the output using a JOptionPane, see this code (converted from old):
    Code:
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    
    public class Triangle {
    
    	public static void main(String[] args) {
    		int triangle;
    		triangle = Integer.parseInt(JOptionPane.showInputDialog(null,
    				"Ke in an integer triangle",null));
    		{
    			String display="";
    			for(int x = 0; x < triangle; x++) {
    				for(int y = 0; y <= x; y++) {
    					display+="*";
    				}
    				display+=" \n";
    			}
    			JOptionPane.showMessageDialog(new JFrame(),display);
    		}
    	}
    }
    I think this is what you want to do.

  8. #8
    Join Date
    Jul 2005
    Location
    malaysia
    Posts
    6
    thank zodoz n sjalle, both of you have been much help for me. the program works fine. thank you again

  9. #9
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Ok Zodoz, I wasn't aware of that. However, giving an assignment that requires the
    students to do text "graphics" in a GUI isn't what I would call relevant education, but
    then I have seen many examples of strange teachings presented by students in this
    forum.
    eschew obfuscation

Similar Threads

  1. Converting projects to VB.NET
    By Frank Oquendo in forum .NET
    Replies: 116
    Last Post: 06-14-2002, 06:04 AM
  2. Replies: 0
    Last Post: 11-20-2000, 05:37 AM
  3. Replies: 0
    Last Post: 11-20-2000, 05:32 AM
  4. Replies: 1
    Last Post: 10-11-2000, 12:23 PM
  5. Replies: 1
    Last Post: 10-06-2000, 11:25 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center