Solved

How to pass array to a jTable

Posted on 2011-02-11
15
413 Views
Last Modified: 2012-05-11
Dear All,

this is my code.

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:

        double i[]=new double[100000];

        double a=Double.parseDouble(jTextField1.getText());
        double b=Double.parseDouble(jTextField2.getText());
        double m=Double.parseDouble(jTextField3.getText());

        jButton1.setEnabled(false);

         i[0]=10;    //Starting Value

        //Generating Random Numbers

        for(int j=1; j<i.length; j++) {
            i[j]=(((a*i[j-1]+b)%m)/m);
        }
         //Arranging the Randomly Generated Numbers into Order.
        jTable1....................................................
        jButton1.setEnabled(true);
    }

and i want to show the values of array i[] to jTable1, how can I do this.
0
Comment
Question by:sfaisalawan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 2
  • 2
15 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 125 total points
ID: 34876361
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34876385
0
 
LVL 47

Accepted Solution

by:
for_yan earned 125 total points
ID: 34876497

It is not enought to know one-dimensional array
to build the table, you probably want to know how
many columns (and rows) you'll have and you may want to know
the column headers therefore it is good to start with the Vector
of column headers:

Vector vCol = new Vector();

vCol.addElement("Col1");
vCol.addElement("Col2");
...

This vector will communicate to table model how many columns
we want to have and what are their names.

Then you'll first create tableModel feeding this vector to the constructor,
following the naming of the link above and taking advantage of the
EntryTableModel class which they suggested which extends DefaultTableModel
you'll then say

tableModel = new EntryTableModel(vCol);

then you can start populating vectors for each row
and adding them to table model like say with elements of your array :

int counter = 0;
let's say you want numRows = 10;

int numRows = 10;

for(int j=0; j<numRows; j++){
Vector rowData = new Vector();

for(int jj=0; jj<vCol.size(); jj++) {
rowData.addElement(new Double(i[counter]));
counter++;
}
tableModel.addRow(rowData);
}

Elements of vectors should be objects
therefore you need to use newDouble(double)
when addiing to vectors, rather than numbers

After you run through these loops
you can create JTable and set its model
JTable table = new JTable();
 table.setModel(tableModel);

This is really rathe long process but should be understandable.

Let us know if you see any problems.
















0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:sfaisalawan
ID: 34876512
This is basically random number generation from a statistical equation. I have to use only 1 array for this. all my work is fine I am just structed in this situation, I have to show all random numbers which are about 100000 into a table or textArea, any solution for that
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34876524

If you want just to show it in the form of the table and don't need to manipulate in any way
I'd go with JTextAre
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34876542
You just decide how many columns you want and then create a string like that

int numColumns = 10;

JTextArea txt = new JTextArea(60,60);
int count = 0;
String s = "";

for(int jj=0;jj<numColumns; jj++){
if(count >= i.length)break;
s +=  i[count] + "\t";
count++;
}

txt.setText(s);

I hope tabs (\t) should work in text area in swing - this would ensure
that you have aligned columns







0
 
LVL 47

Expert Comment

by:for_yan
ID: 34876544
No sorry,  a little correction is neded of course
0
 
LVL 3

Author Comment

by:sfaisalawan
ID: 34876550
then what will be the process I am doing

jTextArea1.append(i[]);

but its not working
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34876555
I think this should be fine:

int numColumns = 10;

JTextArea txt = new JTextArea(60,60);
int count = 0;
String s = "";
for(int num=0; num<i.length; num++){
for(int jj=0;jj<numColumns; jj++){

s +=  i[num] + "\t";
count++;
}
s +=  System.getProperty("line.separator");
}

txt.setText(s);

0
 
LVL 47

Expert Comment

by:for_yan
ID: 34876565
No you cannot append Double to text area and you need
to format a String and then use this String to setText to this text area
That should work the way I showed
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34876572

this is cleaner - without remnants of the prvious version in the code:

int numColumns = 10;

JTextArea txt = new JTextArea(60,60);
int count = 0;
String s = "";
for(int num=0; num<i.length; num++){
for(int jj=0;jj<numColumns; jj++){

s +=  i[num] + "\t";

}
s +=  System.getProperty("line.separator");
}

txt.setText(s);

0
 
LVL 47

Expert Comment

by:for_yan
ID: 34876645
Sorry, IO'm making so many stupid mistakes today,
I'll make it right now
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34876650
I hope this should populate correctly:

int numColumns = 10;

JTextArea txt = new JTextArea(60,60);
int count = 0;
String s = "";

wihile (count < i.length){
for(int jj=0;jj<numColumns; jj++){

s +=  i[count] + "\t";
count++;
}
s +=  System.getProperty("line.separator");
}

txt.setText(s);

0
 
LVL 92

Expert Comment

by:objects
ID: 34876770
if you need to append the row to an existing table data then see here

http://www.exampledepot.com/egs/javax.swing.table/AppendRow.html

If you're replacing existing data then just create a new model

0
 
LVL 47

Expert Comment

by:for_yan
ID: 34876887

This one should also work correctly if
array size is not divisible by the nuber of columns:

int numColumns = 10;

JTextArea txt = new JTextArea(60,60);
int count = 0;
String s = "";

wihile (count < i.length){
for(int jj=0;jj<numColumns; jj++){

if(count  == i.length)break;
s +=  i[count] + "\t";
count++;
}
s +=  System.getProperty("line.separator");
}

txt.setText(s);

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

761 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question