1

I have an ArrayList containing 100 values.

al = "AA","BB","CC","DD","EE","FF"... upto 100 values

I need to insert these values in a jTable having 2 columns and 50 rows i.e.

col1   col2  
AA     BB   
CC     DD  
EE     FF  
...    ...   

upto 50 rows

How do I get that (preferably using loops)? Need a generic solution because number of elements or rows can vary.

3

3 Answers 3

1

Here's what you need . I know this may not be the optimized way, Still try this:

package App;

import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;

public class JtableModel extends AbstractTableModel {

List list, oddList, evenList;
String col_names[] = {"First", "Second"};

public JtableModel(List list) {
    this.list = list;

}

public void myFormatting(){

    this.oddList = new ArrayList();
    this.evenList = new ArrayList();
    for (int i = 0; i < list.size(); i++) {
        if (i % 2 == 0) {
            this.evenList.add(list.get(i));
        } else {
            this.oddList.add(list.get(i));
        }
    }
}



@Override
public int getRowCount() {
    return evenList.size();
}

@Override
public int getColumnCount() {
    return col_names.length;
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {

    switch (columnIndex) {
        case 0:
            try {
                return evenList.get(rowIndex);
            } catch (Exception e) {

            }
        case 1:
            try {
                return oddList.get(rowIndex);
            } catch (Exception  e) {
               //find out how you can validate this 

            }
    }

    return new String();
}
}

And the Snippet of java File generate with NetBeans :

 public class JtableDemo extends javax.swing.JFrame {

/**
 * Creates new form JtableDemo
 */
public JtableDemo() {
    initComponents();
    fillTbl();
}

public void fillTbl(){
    ArrayList arr = new ArrayList();
    arr.add("AA");
    arr.add("BB");
    arr.add("CC");
    arr.add("DD");
    arr.add("EE");

    JtableModel tableModel = new JtableModel(arr);
    tableModel.myFormatting();
    jTable1.setModel(tableModel);

}

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Dividing the original arr in two sublists is completely unnecessary and a waste of memory and time.
0

i'd simply convert the arraylist in an 2 dimensional array ...

somethin like that

public void convertToArray(){
    int count = 0;
    for (int j = 0; j < entrys.length; j++) {
        for (int i = 0; i < 2; i++) {
            entrys[j][i] = list.get(count++);
        }
    }
}

then create the table

    String[] header = {"col1", "col2"};
    table = new JTable(entrys, header);

and add to frame etc.

Comments

0

More or less :

import java.util.ArrayList;
 import javax.swing.*;
 import javax.swing.table.DefaultTableModel;


 public class NewClass extends JFrame {

private JTable table;
private DefaultTableModel model;
private JScrollPane scroll;
private String headers[] = {"col1","col2"};
String[] data = {"AA", "BB", "CC", "DD", "EE", "FF","GG","HH","II","JJ","KK","LL","MM","NN","OO","PP","QQ","RR"};

public NewClass() {

    model = new DefaultTableModel();
    model.setColumnIdentifiers(headers);

    table = new JTable();
    table.setModel(model);

    scroll = new JScrollPane(table);

    insert();

    add(scroll, java.awt.BorderLayout.CENTER);
    setSize(300, 300);
    setVisible(true);
}

public void insert(){  
 ArrayList<String> ar = new ArrayList<String>();
    for (int i = 0; i < data.length; i++) {
        ar.add(data[i]);
    }

   for (int i=0;i<(ar.size()/2);i++) {                 
    model.addRow(new Object[] { String.valueOf(ar.get(2*i)) ,  String.valueOf(  ar.get((2*i)+1))                                });

  }
    }

public static void main(String[] args) {
new NewClass();
    }
   }

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.