[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

How to fill my jtable

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
Mickeys
Asked:
Mickeys
  • 2
3 Solutions
 
for_yanCommented:
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
 
for_yanCommented:
 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
 
CEHJCommented:
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now