-1

I created a JTable and inserted the column header and data into the table.

  • data is vector of vectors
  • column_header is vector of string

This is the code:

Vector<Diagnosis_data> Arraylist_object = new Vector<Diagnosis_data>();
Vector<Vector<Object>> data = new Vector<Vector<Object>>();

Diagnosis_data d1 = new Diagnosis_data("92992","1",1422-09-09,1422-09-18,E11.9,"Type 2 diabetes mellitus without complications");
Diagnosis_data d2 = new Diagnosis_data("8199111","2",1415-09-09,1415-09-18,E11.622,"Type 2 diabetes mellitus with other skin ulcer");

Arraylist_object.add(d1);
Arraylist_object.add(d2);

int f = 0;
while(f < Arraylist_object.size())
{
    Vector<Object> vector = new Vector<Object>();

    for(; f < Arraylist_object.size() ; f++)
    {
        vector.add(Arraylist_object.get(f).patient_ID);
        vector.add(Arraylist_object.get(f).Ad_ID);
        vector.add(Arraylist_object.get(f).Ad_start);
        vector.add(Arraylist_object.get(f).Ad_End);
        vector.add(Arraylist_object.get(f).Diagnosis_code);
        vector.add(Arraylist_object.get(f).Diagnosis_Des);

        data.add(vector);
    }
}


DefaultTableModel tableModel = new DefaultTableModel(data,column_header);

JTable table_2 = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(table_2);

scrollPane.setBounds(1, 1, 1050, 500);
panel_1.add(scrollPane);

The result is:

enter image description here

user85421
  • 28,957
  • 10
  • 64
  • 87
monaalhumud
  • 57
  • 1
  • 7
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). Obviously (I hope) remove all the unrelated cruft at the top of the GUI. 2) You've described a problem, but asked no question. What is your question? 3) `scrollPane.setBounds(1, 1, 1050, 500);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, .. – Andrew Thompson May 08 '17 at 05:23
  • .. or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson May 08 '17 at 05:25
  • *"my answer is edited to reflect the added code"* I suggested to post an MCVE / SSCCE. What didn't you understand? – Andrew Thompson May 08 '17 at 11:21
  • @AndrewThompson what should **_I_** understand? Why should **_I_** post an MCVE/SSCCE as ANSWER? I edited the question, but I am not the author... – user85421 May 08 '17 at 11:32
  • anyway, seems like the problem is already solve since the author is already asking about layout in another question (with correct data) – user85421 May 08 '17 at 11:35
  • No, post the problem code (in the form of an MCVE / SSCCE) as part of the *question*, like [this question](http://stackoverflow.com/questions/6315699/why-are-the-level-fine-logging-messages-not-showing) .. – Andrew Thompson May 08 '17 at 11:38
  • @AndrewThompson **I AM NOT the author of the question**, I just edited it!!! I have no "problem code" to post... The author is "monaalhumud" – user85421 May 08 '17 at 16:28
  • @CarlosHeuberger Oh, my bad. No, I don't expect someone providing an answer to post an MCVE/SSCCE, but confused you with the OP. :P – Andrew Thompson May 08 '17 at 16:44

1 Answers1

2

The code you posted is working given the correct data - the problem must be the data. Use a debugger to check it or, just for testing, print out the content of data.

Probably the same Vector is being added to data multiple times or the content of each Vector is same (maybe the Patient object is using static fields?)

Note: data I used to test your code - a new Vector must be created for each row:

Vector<Vector<?>> data = new Vector<>();
Vector<String> column_header = new Vector<>();

column_header.addAll(Arrays.asList("A", "B", "C"));

Vector<String> row;
row = new Vector<>();
row.addAll(Arrays.asList("11", "12", "13"));
data.add(row);
row = new Vector<>();
row.addAll(Arrays.asList("21", "22", "23"));
data.add(row);
row = new Vector<>();
row.addAll(Arrays.asList("31", "32", "33"));
data.add(row);

EDIT: After the generating data code has been added, it is clear that the problem is, as I suspected above: "the same Vector is being added".

Look at this part:

Vector<Object> vector = new Vector<Object>();

for(; f < Arraylist_object.size() ; f++)
{
    vector.add(...);

    data.add(vector);
}

The vector is created only once OUTSIDE the loop, but being added multiple time inside the loop.

Solution: create the vector INSIDE the loop

And there is no need for the additional while, actually the convention is to declare the variable in the for loop (and use i as counter):

for(int i = 0; i < Arraylist_object.size() ; i++)
{
    Vector<Object> vector = new Vector<Object>();

    vector.add(Arraylist_object.get(i).patient_ID);
    ...

    data.add(vector);
}

or use foreach:

for (Diagnosis_data diagnosis : Arraylist_object)
{
    Vector<Object> vector = ...
    vector.add(diagnosis.patient_ID);
    vector.add(diagnosis.Ad_ID);
    ...
}

 

Advice: spend some time learning to use a debugger, it will not be wasted if you want to do serious programming.

Advice2: as commented by Andrew Thompson, use LayoutManager's instead of using constant coordinates to layout your components. Tutorial: Laying Out Components Within a Container

user85421
  • 28,957
  • 10
  • 64
  • 87
  • Dear Carlos - thenk you so much for your replay .. i test data by printing it it is contains a good vectors with the same sort ... this is the code for filling vector – monaalhumud May 08 '17 at 06:06
  • int f = 0; while(f < Arraylist_object.size()) { Vector vector = new Vector(); for(; f < Arraylist_object.size() ; f++) { vector.add(Arraylist_object.get(f).patient_ID); vector.add(Arraylist_object.get(f).Ad_ID); vector.add(Arraylist_object.get(f).Ad_start); vector.add(Arraylist_object.get(f).Ad_End); vector.add(Arraylist_object.get(f).Diagnosis_code); vector.add(Arraylist_object.get(f).Diagnosis_Des); data.add(vector); } } – monaalhumud May 08 '17 at 06:06
  • **1)** above should be included in the original post (at least to be readable) **2)** it seems still incomplete - what is Arraylist_object and, more important, its content - should also be part of the post **3)** print out `Arraylist_object.get(f).patient_ID` in the inner loop to verify it (using a debugger would be much more efficient) – user85421 May 08 '17 at 06:47
  • Dear i added what required in the post please check – monaalhumud May 08 '17 at 07:24