Solved

How to pass array to a jTable

Posted on 2011-02-11
15
407 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
Comment Utility
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
0
 
LVL 47

Accepted Solution

by:
for_yan earned 125 total points
Comment Utility

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
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility
No sorry,  a little correction is neded of course
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 3

Author Comment

by:sfaisalawan
Comment Utility
then what will be the process I am doing

jTextArea1.append(i[]);

but its not working
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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
Comment Utility
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
Comment Utility

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
Comment Utility
Sorry, IO'm making so many stupid mistakes today,
I'll make it right now
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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
Comment Utility
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
Comment Utility

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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:

772 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

12 Experts available now in Live!

Get 1:1 Help Now