Solved

How to pass array to a jTable

Posted on 2011-02-11
15
410 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now