Results 1 to 3 of 3
  1. #1
    rick2993 is offline Member
    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    Default Issue with printing a 2d array in a new window

    I hope i dont insult you guys with this question as it'll probably turn out to be very easy. But nonetheless it has still consumed most of my day today...

    How can i add a 2d array into a new window using a JButton?

    Here is a fragmented snippet and let me know if you need more information.

    Java Code:
    public class DaisyCollector extends JFrame
    {
        
           final static int ROWS = 10;
           final static int COLS = 10;
           static int[][] daisies = new int[ROWS][COLS];
           int row, col;
           static Random ran = new Random();
           static int number;
           JTextField coorin = new JTextField("Cooridinates" + COLS + "," + ROWS);
    }
    
    private static void showArray(int[][] array) 
            {
                for (int n = 0; n <= 20; n++)
                {
                    if(n == 0)
                        System.out.print("     ");
                    if (n < 9)
                        System.out.print(n + "     ");
                    if (n == 9)
                        System.out.print(n + "\n");
                    if (n == 10)
                        System.out.print("     ===");
                    if (n > 10 && n < 20)
                        System.out.print("   ===");
                    if (n == 20)
                        System.out.print("\n");
                }
                
                for (int row = 0; row < array.length; row++)                          
                {
                System.out.printf("%s" +":", row);
                for (int col = 0; col < array[row].length; col++)
               
                System.out.printf("%6s", array[row][col]);
                System.out.println();
                                
                }
               
        }   
    
        private class MapButtonListener extends JFrame implements ActionListener
       {
          public void actionPerformed(ActionEvent e)
          {
                                
               showArray(daisies);        
         
          }
       }  
    
    
    /**That statement would print the array when the user clicks the "Map" button but i really need it in a new window... I tried something like
    
    JLabel message;
    message = new JLabel(showArray(daisies));
    add(message);
    
    but Netbeans said void type wasn't allowed there. Does anyone have any idea how to fix this?
    */
    Last edited by rick2993; 12-21-2012 at 12:00 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Issue with printing a 2d array in a new window

    Please check out the forum FAQ or my link below on how to use [code] [/code] tags so that your code retains its formatting and may be easier to read.

    All the showArray(...) method does is print out data using println(...) statements, so it doesn't make sense that calling this inside of a JLabel will do anything useful. Instead consider creating a method that will either display the data in a JTextArea or else in a JTable. Google the Swing tutorials on these and give it a go.

  3. #3
    rick2993 is offline Member
    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    Default Re: Issue with printing a 2d array in a new window

    Thanks so much! i'll work on that. who knows how long that might have taken me to figure out lol.

Similar Threads

  1. Printing on the same window
    By Fits in forum New To Java
    Replies: 1
    Last Post: 07-02-2012, 12:36 PM
  2. Printing the current window
    By Paul_White in forum New To Java
    Replies: 1
    Last Post: 08-17-2011, 01:18 PM
  3. Window Resizing issue
    By er91ic512 in forum AWT / Swing
    Replies: 2
    Last Post: 07-28-2009, 01:48 PM
  4. Regarding issue in frame window
    By santhosh_el in forum AWT / Swing
    Replies: 1
    Last Post: 06-01-2009, 09:10 AM
  5. Issue with printing line
    By Azndaddy in forum Advanced Java
    Replies: 1
    Last Post: 04-04-2008, 08:37 PM

Posting Permissions

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