Solved

How to fill my jtable

Posted on 2011-09-13
3
254 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 70
Java / Linux and Regular Expressions 11 71
netstat -ano | find "8000" and taskkill /f /pid 2984 3 38
replace a word with other 1 40
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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

895 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

11 Experts available now in Live!

Get 1:1 Help Now