Solved

How to pass array to a jTable

Posted on 2011-02-11
15
414 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Restore security prompts not working 10 234
add projects t working set in maven 2 62
Fast way to search item into Java Array (Rhino compatible) 2 45
Setup GlassFish 4 52
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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 …
Suggested Courses

740 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