0

I have data stored in array list object here :

    ArrayList<Long> itr= new ArrayList<Long>();
    ArrayList<Double> dub = new ArrayList<Double>();
    ArrayList<String> str = new ArrayList<String>();

I am able to print sample data with the below program how do I pass all the array list object data here? so that I can get array list data in jtable tabular column. I am only able to fetch individual values. How do I do it with for loop?

class ColoumnHeader extends JFrame 
{
public ColoumnHeader()
{
    super("Jtable Layout");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
        String[] ColoumnHeaders={"Integers","RealNumbers","OtherTokens"};
    JTable table=new JTable(new Object[][]{{itr.get(1),dub.get(1),str.get(1)}},ColoumnHeaders);


            table.setPreferredScrollableViewportSize(new Dimension(500,80));

    JScrollPane pane = new JScrollPane(table);
    getContentPane().add(pane,BorderLayout.CENTER);
}
}
 public class asm
{
public static void main(String[] args)
 {
  ColoumnHeader c=new ColoumnHeader();
  c.setVisible(true);
 }
}
Box
  • 113
  • 12
  • Possible duplicate of [How to populate a jTable from an ArrayList?](http://stackoverflow.com/questions/20012772/how-to-populate-a-jtable-from-an-arraylist). – Hovercraft Full Of Eels Apr 13 '16 at 22:04
  • [Other similar questions](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=how+to+populate+a+jtable+from+arraylist+site:http:%2F%2Fstackoverflow.com%2F) – Hovercraft Full Of Eels Apr 13 '16 at 22:05
  • Recommendation 1: Get rid of the parallel ArrayLists as that's an invitation to disaster and a debugging nightmare. Instead create a class to hold the three (or more attributes) and then a single ArrayList, or better TableModel to hold a collection of objects of this class. – Hovercraft Full Of Eels Apr 13 '16 at 22:06
  • So, each `ArrayList` is a column of data then? – MadProgrammer Apr 13 '16 at 22:25

1 Answers1

2

Take the time to read the JavaDocs and tutorials as more often then not, the answer is easily available...

DefaultTableModel mod = (DefaultTableModel) table.getModel();
mod.addColumn("Long", itr.toArray(new Long[itr.size()]));
mod.addColumn("Double", dub.toArray(new Double[dub.size()]));
mod.addColumn("Strings", str.toArray(new String[str.size()]));
wonderb0lt
  • 2,035
  • 1
  • 23
  • 37
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366