Solved

How to fill my jtable

Posted on 2011-09-13
3
249 Views
Last Modified: 2012-05-12
i need to fill out my jtable. how do I do that with my arraylist?

When the code below is finished I come over to a class that has a jtable named jtable1.

How do I fill this one out? I have tried diffrent solutions from the net but cant seem to get it to work.
if(comboBoxArtikel.getSelectedIndex()  == 0)
        {
            int ID = Integer.parseInt(textBox_ID.getText());
            double pris = Double.parseDouble(textBox_Pris.getText());
            Bok b1 = new Bok(ID, textBox_Namn.getText(), pris, textBox_ISBN.getText(), textField_Forfattare.getText(), textField_Genre.getText());

            label_Forfattare.setText("Författare");
            label_Genre.setText("Genre");
            label_Forfattare.setVisible(true);
            textField_Forfattare.setVisible(true);
            label_Genre.setVisible(true);
            textField_Genre.setVisible(true);

            artikelArray.add(b1);

            Meny.setArtikelArray(artikelArray);



            this.dispose();
            System.out.print(b1);
       }

Open in new window

0
Comment
Question by:Mickeys
  • 2
3 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 333 total points
ID: 36530643
You can use this example:
http://www.java2s.com/Code/JavaAPI/javax.swing.table/DefaultTableModeladdRowObjectrowData.htm

Declare the cvariable of the type DefaultTableModel

DefaultTableModel model;


instantinate it with dedfeault constructor

model = new DefaultTableModel();
then add columns:

model.addColumn("gfirst column name");

model.addColumn("second column name");

....

Then
populate String[] array for each row
and
model.addRow(array);

do it for all your rows

Then pass your model opbject to the class where you are creating jtable
and

use jtable.setModel(model)
you'll populate your jtable





0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 333 total points
ID: 36530687
 DefaultTableModel model = new DefaultTableModel();
model.addColumn("Title");
model.addColumn("ISBN");
model.addColumn("Genre");
...

//and the for each book 
//utyoui maye create a Vector:

//start loop over all books

Vector v = new Vector();

v.add(textBox_Namn.getText());
v.add( textBox_ISBN.getText());
...

model.addRow(v);

//end loop over all books

// you model instance  should be passed to the class
// which has jtable

jtable.setModel(model); //will populate JTable


 

Open in new window

0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 167 total points
ID: 36535434
Since you're already storing List<Bok> you can use that as the basis for a custom TableModel. If you attach your Bok class, it will help
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

708 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

13 Experts available now in Live!

Get 1:1 Help Now