Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to pass array to a jTable

Posted on 2011-02-11
15
Medium Priority
?
419 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 500 total points
ID: 34876361
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

604 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