Results 1 to 3 of 3
  1. #1
    geekchick is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default Having trouble with 2d Array

    hi,

    I would like to add column names and fields entered in text boxes to a JTable, but my code is not compiling. Also, is there a way to set a 2d array to an undetermined size? I'm not sure how many entries there will be. Here is my code:

    Java Code:
    public class CustomerDisplay
    {
    	JFrame frame = new JFrame();
    	JPanel panel = new JPanel();
    	
    	private String [] columnNames = null;
    	private String[][] rowData = null;
    	
    	public CustomerDisplay(String rowData[][], String [] columnNames )
    	{
    		this.columnNames = columnNames;
    		this.rowData = rowData;
    		
    		JTable table = new JTable(rowData, columnNames);
    		JScrollPane sp = new JScrollPane(table);
    		panel.add(sp);
    		add(panel);
    		
    		
    	}
    
    	
    	
    	
    }
    Java Code:
    public class Customer extends JPanel
    {
    	JPanel panel = new JPanel();
    	private String [] columnNames = {"First Name", "Last Name", "Customer Status", "Dependent Age", "Customer ID"};
    	private String [][] rowData = new String [10][10];
    	public Customer()
    	{
    				
    		panel.setLayout(new BorderLayout());
    		panel.add(new Buttons(), BorderLayout.NORTH);
    		panel.add(new CustomerInformation(), BorderLayout.CENTER);
    		panel.add(new CustomerDisplay(rowData, columnNames), BorderLayout.SOUTH);
    		
    		add(panel);
    		
    	}
    }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    If your code is not compiling, provide the error message(s) and line number(s) that the compiler gives you.

    As far as I know, there is no way to create an array of an undetermined size. However, an array can be resized using the Array.newInstance() method, then System.arraycopy().
    Last edited by Zack; 08-02-2010 at 04:53 AM.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Post your complete error message here to see. I don't think that anyone wants to compile your code and fix errors for you.

    Just looking at your code I've seen that you haven't import any required packages in to the class file.

Similar Threads

  1. Having trouble reading external txt file to an array
    By Metastar in forum New To Java
    Replies: 18
    Last Post: 07-21-2010, 11:29 PM
  2. trouble with array method
    By kpro862 in forum New To Java
    Replies: 6
    Last Post: 05-12-2010, 12:08 AM
  3. Array trouble
    By hobo in forum New To Java
    Replies: 8
    Last Post: 11-17-2009, 12:04 PM
  4. [SOLVED] Array trouble....
    By AngrYkIdzrUlE in forum New To Java
    Replies: 9
    Last Post: 04-18-2009, 10:18 PM
  5. Having trouble with array
    By ice22 in forum New To Java
    Replies: 3
    Last Post: 11-13-2007, 03:06 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
  •