How to resolve a JTable, JScrollPane error?

I am trying to retrieve a list of all itmes in a database and put this information in a JTable with a JScrollPane on a JFrame.  I am selecting several columns to be used for selection and display.  However, for the browse table object I only need to display 3 columns --> record no, customer, and order date.  The error I am getting is on this line --->>
JTable table1 = new JTable(browseList, headers);
ERROR ---->>
The constructor JTable(ArrayList<String>, Object[]) is undefined
How can I resolve this problem and get the JTable to work?
// Required columns for browse - SONUM, buyer, sodate  
// Required columns for selection to display - 	sonum, buyer, sodate, buyaddr1, buyaddr2, buycity, buystate, buyzip, 
//				buycoun, transport, delivdate, cashCharge, comments    
			    ArrayList<String> browseList = new ArrayList<String>();
			    String sqlString = "SELECT SONUM, BUYER, SODATE, " +
    			"BUYADDR1, BUYADDR2, BUYCITY, BUYSTATE, BUYZIP, BUYCOUN," +
    			"TRANSPORT, DELIVDATE, CASHCHARGE, COMMENTS FROM CELL_SOHEAD";
			    PreparedStatement pst = connection.prepareStatement(sqlString);
		    	ResultSet rsbrowse = pst.executeQuery();
		    	rsbrowse.next();
			    while (rsbrowse.next()) {
			    		browseList.add(rsbrowse.getString("SONUM"));
			    		browseList.add(rsbrowse.getString("BUYER"));
			    		browseList.add(rsbrowse.getString("SODATE"));
			    	}
			    browseList.trimToSize();
			    noOfContacts = new String [browseList.size()];
	            browseList.toArray(noOfContacts);
	    		System.out.println("contacts "+browseList.size());
		        connection.close();
		        Object headers[] = {"Record No", "Customer", "Order Date"}; 
		        JTable table1 = new JTable(browseList, headers);
		        JScrollPane scroll1 =  new JScrollPane(table1);
		        JFrame tableFrame = new JFrame("Please make a selection");
		        JPanel tablePanel = new JPanel();
		        tablePanel.add(scroll1);
		        tableFrame.add(tablePanel);
		        tableFrame.setVisible(true);
		        tableFrame.pack();

Open in new window

Vanavah EdwardsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
There is no such constaructor - it is TableModel has such cibstructor, not JTable
0
for_yanCommented:


 TableModel model = new TableModel(browseList, headers);
JTable tabble = new JTable();
   table.setModel(model);
0
for_yanCommented:
or arther

DefaultTableModel model = new DefaultTableModel(browseList, headers);
 JTable table1 = new JTable();
table1.setModel(model);

0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

for_yanCommented:
There is no such constructor

JTable(ArrayList, String[])
0
for_yanCommented:
You actually need to make Vector out of ArrayList and Vector of ColumnNames
then there is a constructor:

http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/JTable.html#JTable%28java.util.Vector,%20java.util.Vector%29

public JTable(Vector rowData,
              Vector columnNames)

0
for_yanCommented:
No, the way you are doing it - it is not correct
This is one of the ways to do it:

(still you'll need to remove a bunch of calls like trim to szie etc.
// Required columns for browse - SONUM, buyer, sodate  
// Required columns for selection to display - 	sonum, buyer, sodate, buyaddr1, buyaddr2, buycity, buystate, buyzip, 
//				buycoun, transport, delivdate, cashCharge, comments    
			//    ArrayList<String> browseList = new ArrayList<String>();

                                                 Vector<Vector<String>> browseList = new ArrayListVector<Vector<String>> ();
			    String sqlString = "SELECT SONUM, BUYER, SODATE, " +
    			"BUYADDR1, BUYADDR2, BUYCITY, BUYSTATE, BUYZIP, BUYCOUN," +
    			"TRANSPORT, DELIVDATE, CASHCHARGE, COMMENTS FROM CELL_SOHEAD";
			    PreparedStatement pst = connection.prepareStatement(sqlString);
		    	ResultSet rsbrowse = pst.executeQuery();
		    	rsbrowse.next();
			    while (rsbrowse.next()) {
Vector<String> v = new Vector<String>();
			    		v.add(rsbrowse.getString("SONUM"));
			    		v.browseList.add(rsbrowse.getString("BUYER"));
			    		v.browseList.add(rsbrowse.getString("SODATE"));
                                                        browseLis.add(v);
			    	}
			  //  browseList.trimToSize();
			    noOfContacts = new String [browseList.size()];
	            browseList.toArray(noOfContacts);
	    		System.out.println("contacts "+browseList.size());
		        connection.close();
		        Object headers[] = {"Record No", "Customer", "Order Date"};
                                        Vector<String> headersV = new Vector<String>(); 
                                       for(int j=0; j<headers.lenght; j++)headersV.add(headers[j]);
		        JTable table1 = new JTable(browseList, headersV);
		        JScrollPane scroll1 =  new JScrollPane(table1);
		        JFrame tableFrame = new JFrame("Please make a selection");
		        JPanel tablePanel = new JPanel();
		        tablePanel.add(scroll1);
		        tableFrame.add(tablePanel);
		        tableFrame.setVisible(true);
		        tableFrame.pack();

Open in new window



0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
for_yanCommented:
Constructor arguments should be of excat types as they are defined in the class
Look at JTable - where did you find constructor which takes (ArrayList, Object[]) - ?
There is nothing lke that:
------
JTable()
          Constructs a default JTable that is initialized with a default data model, a default column model, and a default selection model.
JTable(int numRows, int numColumns)
          Constructs a JTable with numRows and numColumns of empty cells using DefaultTableModel.
JTable(Object[][] rowData, Object[] columnNames)
          Constructs a JTable to display the values in the two dimensional array, rowData, with column names, columnNames.
JTable(TableModel dm)
          Constructs a JTable that is initialized with dm as the data model, a default column model, and a default selection model.
JTable(TableModel dm, TableColumnModel cm)
          Constructs a JTable that is initialized with dm as the data model, cm as the column model, and a default selection model.
JTable(TableModel dm, TableColumnModel cm, ListSelectionModel sm)
          Constructs a JTable that is initialized with dm as the data model, cm as the column model, and sm as the selection model.
JTable(Vector rowData, Vector columnNames)
          Constructs a JTable to display the values in the Vector of Vectors, rowData, with column names, columnNames.
----
0
Vanavah EdwardsAuthor Commented:
I am not using an arraylist as you posted --->>  JTable(ArrayList, String[])
I am first converting the arrayList to an array in line 2 --->>
browseList.toArray(noOfContacts);

The error I am now getting on TabelModel part of --->>TableModel(browseList, headers); is
Cannot instantiate the type TableModel.
noOfContacts = new String [browseList.size()];
	            browseList.toArray(noOfContacts);
	    		System.out.println("contacts "+browseList.size());
		        connection.close();
		        Object headers[] = {"Record No", "Customer", "Order Date"}; 
		        TableModel model = new TableModel(browseList, headers);
		        JTable table= new JTable();
		        table.setModel(model);
		        JFrame tableFrame = new JFrame("Please make a selection");
		        JPanel tablePanel = new JPanel();
		        tablePanel.add(table);
		        tableFrame.add(tablePanel);
		        tableFrame.setVisible(true);
		        tableFrame.pack();

Open in new window

0
for_yanCommented:

browseList.toArray(noOfContacts);  - browseList cannot be converted to array

youn need ti say:

String [] myArray = browseList.toArray(noOfContacts);

then myArray will be array, but not browseList
 
0
for_yanCommented:


public Object[] toArray()
http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#toArray%28%29

You can nevere convert - there aare no suych methds where objects morph to another type
Thsi is the only way how you can use tis method:
String [] myArray = browseList.toArray(noOfContacts);

0
for_yanCommented:
or this methiod
http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#toArray%28T[]%29
but they never morph to another type - browseList will still remain ArrayList as it was created -
the array will be another aentity under another name like noOfContacts - very strange idea to call array "noOfcontacts" - don't do it
it is very confusing
0
for_yanCommented:
and still there is no constructor JTable(Object[], Object[])
there is only constructor JTable(Object[][], object[])
one array should be two-dimensional another one-dimensional
0
for_yanCommented:
And you definitely do not need anyof these arrays, trimToSize(), etc. etc.

Create Vector of Vectors when you read the data as I showed yiou and then Vector of Strings for headers
and use that last constructor JTable(Vector, Vector) - the most convenient one
0
for_yanCommented:


>The error I am now getting on TabelModel part of --->>TableModel(browseList, headers); is
>Cannot instantiate the type TableModel.

Don't do that - i meant DefaultTaleModel, but you can do it without mode  - just craete table
as JTable(Vector, Vecrtor) as  I showed oin  ID:37318451
0
Vanavah EdwardsAuthor Commented:
I got the method of converting arraylist to array from --->>
http://www.java2s.com/Book/Java/0080__Collections/Convert_ArrayList_to_array.htm
0
for_yanCommented:

 String[] strings = al.toArray(new String[al.size()]);

it is "strings" which becomae array - new entity,  "al" is not morphing to array,
and you used the saem browseList as if it became suddenly  an array, when it was born as ArrayLists - that is impossible
0
for_yanCommented:

This method :
  browseList.toArray(noOfContacts);
  is useless unless you captuyre the result of it into variable which is arrays

String [] newArray = browseList.toArray(noOfContacts);

and  browseList remains ArrayList, it just generates new entity which is array - big, big difference

Anyway, use Vectors as I showed you

0
for_yanCommented:
And anyway for that constructor firts array needs to be two-dimensional, so it will still not work if you say

String [] newArray = browseList.toArray();

 JTable table1 = new JTable(newArray, headers); // thsi has both one-dimesnsional arrays - so it will fail also



0
Vanavah EdwardsAuthor Commented:
I change and studied you code and this error comes up -->
The constructor JTable(String[], Vector<String>) is undefined

Vector<String> v = new Vector<String>();
			    while (rsbrowse.next()) {
			    		v.add(rsbrowse.getString("SONUM"));
			    		v.add(rsbrowse.getString("BUYER"));
			    		v.add(rsbrowse.getString("SODATE"));
			    	}
			    noOfContacts = new String [v.size()];
	            String[] browseArray = v.toArray(noOfContacts);
	    		System.out.println("contacts "+v.size());
		        connection.close();
		        Object headers[] = {"Record No", "Customer", "Order Date"};
                Vector<String> headersV = new Vector<String>(); 
               for(int j=0; j<headers.length; j++)headersV.add(headers[j]);
 
		        JTable table = new JTable(browseArray, headersV);
		        JFrame tableFrame = new JFrame("Please make a selection");
		        JPanel tablePanel = new JPanel();
		        tablePanel.add(table);
		        tableFrame.add(tablePanel);
		        tableFrame.setVisible(true);
		        tableFrame.pack();

Open in new window

0
for_yanCommented:
No you modified my code incorrectkly - firts arguments shoudl be vector of vectors:


Vector<String> v = new Vector<String>();
                      while (rsbrowse.next()) {
Vector<String> v = new Vector<String>(); // this should be inside teh loop

                                  v.add(rsbrowse.getString("SONUM"));
                                  v.add(rsbrowse.getString("BUYER"));
                                  v.add(rsbrowse.getString("SODATE"));
   browseList.add(v) // here you add vector as element of the vector

                            }

          JTable table = new JTable(browseList, headersV);  - first argument should be Vector of Vectors , second argument should be Vector


0
for_yanCommented:
So within each cylec of the lop you need to create new vexctor (correspnding to the row of the table)
and populate it and then add as element of the Vector of Vectors browseList
0
for_yanCommented:
Then your first argunemnet reoresets the body of the table
- each vector - element corresponding to the row of the tab;e

and second argument is a simple Vector - corresponding to the Vector of headers
In this way you are constrcting the table
0
Vanavah EdwardsAuthor Commented:
I am getting several errors now
Line 1 --->>ArrayListVector cannot be resolved to a type
Line  7 ---> v cannot be resolved (I think this because v is in the loop)
Line 15 --->> The method add(String) in the type Vector<String> is not applicable for the arguments (Object)
Line 17 --->> The constructor JTable(String[], Vector<String>) is undefined
Vector<Vector<String>> browseList = new ArrayListVector<Vector<String>> ();
			    while (rsbrowse.next()) {
				    Vector<String> v = new Vector<String>();
			    		v.add(rsbrowse.getString("SONUM"));
			    		v.add(rsbrowse.getString("BUYER"));
			    		v.add(rsbrowse.getString("SODATE"));
			    		browseList.add(v);	// here you add vector as element of the vector
			    	}
			    noOfContacts = new String [v.size()];
	            String[] browseArray = v.toArray(noOfContacts);
	    		System.out.println("contacts "+v.size());
		        connection.close();
		        Object headers[] = {"Record No", "Customer", "Order Date"};
                Vector<String> headersV = new Vector<String>(); 
               for(int j=0; j<headers.length; j++)headersV.add(headers[j]);
 
		        JTable table = new JTable(browseArray, headersV);

Open in new window

0
for_yanCommented:
Vector<Vector<String>> browseList = new ArrayListVector<Vector<String>> ();  // ArrayListVector - what is this?   ArrayList<Vector<String>> 

Open in new window


the res of it may be  dependendt on this mistake - correct - think a little bit - it write everything to you "ArrayListVector cannot be resolved to a type"
0
for_yanCommented:
Well, I wrote wrongly also  - it shoudl be

1:
 Vector<Vector<String>> browseList = new Vector<Vector<String>> ();  
0
for_yanCommented:
Still think a little bit - in that I was right!
0
Vanavah EdwardsAuthor Commented:
Al the other errors are resolved.  I declare v as Vector v; because it was in a loop and therefore invisible.  2 errors remain --->>
Line 15 --->> The method add(String) in the type Vector<String> is not applicable for the arguments (Object)
Line 17 --->> The constructor JTable(String[], Vector<String>) is undefined
0
for_yanCommented:
Post your code.
You should onot have (String[], Vector<String>) , you shoudl have (Vector, Vector)
anyawsy post the code whi ch younhave now - how owuld I know wjat is liine 15 andf line 17

Even beetter - look and think first - then post if you cvacnnot unsderstan - it should be strightforward
0
for_yanCommented:
Change this
Object headers[] = {"Record No", "Customer", "Order Date"};
to:
String  headers[] = {"Record No", "Customer", "Order Date"};

0
for_yanCommented:

Chaneg this:
  JTable table = new JTable(browseArray, headersV);
  to
 JTable table = new JTable(browseList, headersV);


0
Vanavah EdwardsAuthor Commented:
Al the other errors are resolved.  I declare v as Vector v; because it was in a loop and therefore invisible.  2 errors remain  I a mistake with the line nos --->>
Line 16 --->> The method add(String) in the type Vector<String> is not applicable for the arguments (Object)
Line 18 --->> The constructor JTable(String[], Vector<String>) is undefined
Vector v;
		    	 Vector<Vector<String>> browseList = new Vector<Vector<String>> ();
			    while (rsbrowse.next()) {
				    v = new Vector<String>();
			    		v.add(rsbrowse.getString("SONUM"));
			    		v.add(rsbrowse.getString("BUYER"));
			    		v.add(rsbrowse.getString("SODATE"));
			    		browseList.add(v);	// here you add vector as element of the vector
			    	}
				noOfContacts = new String [v.size()];
	            Object[] browseArray = v.toArray(noOfContacts);
	    		System.out.println("Order size "+v.size());
		        connection.close();
		        Object headers[] = {"Record No", "Customer", "Order Date"};
                Vector<String> headersV = new Vector<String>(); 
               for(int j=0; j<headers.length; j++)headersV.add(headers[j]);
 
		        JTable table = new JTable(browseArray, headersV);

Open in new window

0
for_yanCommented:
Look at my two postings above.
0
Vanavah EdwardsAuthor Commented:
All errors has disappeared.  I am a bit slow because I am trying to understand the code and the Vectors rather than just tryng to copy code to get it to work.  Pursuit of understanding is what I am seeking.  I
Vector v = null;
		    	 Vector<Vector<String>> browseList = new Vector<Vector<String>> ();
			    while (rsbrowse.next()) {
				    v = new Vector<String>();
			    		v.add(rsbrowse.getString("SONUM"));
			    		v.add(rsbrowse.getString("BUYER"));
			    		v.add(rsbrowse.getString("SODATE"));
			    		browseList.add(v);	// add vector as element of the vector
			    	}
	    		System.out.println("Order size "+v.size());
		        connection.close();
		        String headers[] = {"Record No", "Customer", "Order Date"};
                Vector<String> headersV = new Vector<String>(); //  
               for(int j=0; j<headers.length; j++)headersV.add(headers[j]);
               JTable table = new JTable(browseList, headersV);

Open in new window

0
for_yanCommented:
That is correct.

The best way you'll get understanding also  - read the error measseges - think what they mean - adn act accordingly.
In most cases they are straightforward - if it ssays -  no such constructor - go to API look what
constructor exist and make sure you provide required types of the arguments  - that is what I'm doing - and you can do the same.
If it says - no method - check the types, compare with API and make sure you put strings if it needs strings and not objctects.

To get to API quicky I juts type in google - "JTable Java API" nad first line will have all methods, constructors, etc

0
Vanavah EdwardsAuthor Commented:
It works.  How can I define column size and scrolls.  Ive add JScrollPane(table)  but the scrolls does appear.
0
for_yanCommented:
"does appear" or "does not appaer" ?

If you have more rows - I think they would appear automatically - if your whole table fits witohout scrools - you would not need them
and they don't appear
0
Vanavah EdwardsAuthor Commented:
What about column sizing?
0
for_yanCommented:

>What about column sizing?

What this question means?
0
for_yanCommented:
I think it will normally adjust cell witdh to the text or numbers in the cell
0
Vanavah EdwardsAuthor Commented:
I looked back at some code and got the scroll bars.  I realized it can be automatic, neverr or always --->
JScrollPane scrolls = new JScrollPane(table,
                           JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                              JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
Columns, what I meants is to be able to adjust individual column size?  This is a different issue may be I would post it as a separate question as I got this problem resolve.
0
for_yanCommented:
Yes, yiou can request scrollbars alwys, but deafault is to have them when it needs.

You should look at the table realizes what in particluar you don;t like - and ten we can think how to change it.
From the very nbeginning it is betteer to leave defauylt situtaion and see waht it does.
0
Vanavah EdwardsAuthor Commented:
I have to adjust my columns because i am not equal sizes for all.  But I want the customer name to have a wider column and the order date to have a much smaller column and so on.  I will close off this session and post a new question for this problem.  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.