Von einer anderen klasse etwas in das JTabel hinzufügen?

Code46

Erfahrenes Mitglied
Ich habe eine Klasse wo ich in eine Form ausfüllen muss. Nachdem ich das ausgefüllt habe, habe ich ein Button der das in ein JTabel hinzufügen soll jedoch ist dieser JTabel in einer anderen Klasse. Code habe ich schon geschrieben aber bin jetzt bischen verwirrt um erlich zu sein.
Ich weis jetzt nicht genau wie ich das machen muss.

Mein Code für das JTABEL:

Code:
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;


class Product implements Comparable {
    private int cost;
    private String name;
public Product(int c, String n) {
    cost = c;
    name = n;
}
public Product() {
    this (0, "default");
}
public int getCost() {
    return cost;
}
public String getName() {
    return name;
}


public int compareTo(Object o) {
    Product temp = (Product) o;


    return (name.compareTo(temp.name));
}
}
public class GUI1 extends JFrame {


  ArrayList products = new ArrayList();

// GUI bits and bobs
  JPanel panInput = new JPanel(new GridLayout(2,1)),
         panAdd = new JPanel(),
         panDelSort = new JPanel();

  JTextField txtName = new JTextField(10),
             txtCost = new JTextField(5);
  JButton btnAdd = new JButton("Add"),
          btnDelete = new JButton("Delete"),
          btnSort = new JButton("Sort Products");

  JTable tab = new JTable();

// The DefaultTableModel will allow the contents of the
// JTable to be manipulated.
  DefaultTableModel tabMod = new DefaultTableModel();

  Product [] initialProducts = {new Product(57, "Widget"),
                                new Product(700, "Cuddly toy"),
                                new Product(12000, "Digital Camera")};

  public GUI1() {
    super("Product list");
    tab.setModel(tabMod);
    tabMod.addColumn("Product Name");
    tabMod.addColumn("Product Cost");

    btnAdd.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
          addProduct();
      }
    });

    btnDelete.setToolTipText("Select the records to delete first");
    btnDelete.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
          deleteProduct();
      }
    });
    btnSort.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
          sortProducts();
      }
    });

    panAdd.add(btnAdd);
    panAdd.add(new JLabel("Product name:"));
    panAdd.add(txtName);
    panAdd.add(new JLabel("Product cost:"));
    panAdd.add(txtCost);
    panDelSort.add(btnDelete);
    panDelSort.add(btnSort);
    panInput.add(panAdd);
    panInput.add(panDelSort);
    getContentPane().add(panInput, BorderLayout.SOUTH);
    getContentPane().add(new JScrollPane(tab));
    loadInitialProducts();
}
// Delete a product from the ArrayList and from the JTable
public void deleteProduct() {
    int [] rows = tab.getSelectedRows();
    for (int i = 0; i < rows.length; i++) {
        tabMod.removeRow(rows[i] - i); // delete from JTable
        products.remove(rows[i] - i); // delete from ArrayList
    }
}

// Sort the products in the ArrayList and then
// reload them into the JTable in the sorted order

public void sortProducts() {
    Collections.sort(products); // sorts into name order
    loadProductsIntoTable();
}

// Add a new products to the ArrayList and the JTable
public void addProduct() {
    String [] row = new String [2];
    Product newProd = new Product(Integer.parseInt(txtCost.getText()), txtName.getText());

    products.add(newProd); // add to the ArrayList
    row[0] = newProd.getName();
    row[1] = Integer.toString(newProd.getCost());
    tabMod.addRow(row); // add to the JTable
}
public void loadInitialProducts() {
    for (int i = 0; i < initialProducts.length; i++) {
        products.add(initialProducts[i]);
    }
    loadProductsIntoTable();
}
public void loadProductsIntoTable() {
    tabMod.setRowCount(0);

    String [] row = new String [2];
    for (Iterator i = products.iterator(); i.hasNext();) {
        Product temp = (Product) i.next();
        row[0] = temp.getName();
        row[1] = Integer.toString(temp.getCost());
        tabMod.addRow(row);
    }
}
public static void main(String [] a){
    GUI1 me = new GUI1();
    me.addWindowListener(new WindowAdapter () {
       public void windowClosing(WindowEvent e) {
          System.exit(0) ;
        }
      });
    me.pack();
    me.setVisible(true);
  }
}


Meine Klasse wo ich die Form habe:

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import static javax.swing.JOptionPane.*;


public class AddDVD extends
JFrame implements ActionListener {
JTextField addtitle, type, addprice;
JButton addT;
JComboBox box_type = new JComboBox();
TextArea list = new TextArea(4, 30);
LibraryData db = new LibraryData();
ImageIcon le = new ImageIcon("MY CONTENT/Images/Left.jpg");
ImageIcon ri = new ImageIcon("MY CONTENT/Images/Right.jpg");



public AddDVD() {
    
addT = new JButton("Add new DVD/Music");
addtitle = new JTextField(2);
type = new JTextField(18);
addprice = new JTextField(18);
setLayout(new BorderLayout());
setBounds(400, 400, 450, 240);
setTitle("Add DVD/Music");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

setResizable(false);
setVisible(true);


JPanel top = new JPanel();
top.setLayout(new GridLayout(3, 2, 5, 5));
top.add(new JLabel("Title: ", JLabel.RIGHT));
top.add(addtitle);
top.add(new JLabel("Type: ", JLabel.RIGHT));
top.add(box_type);String[] typeStr = {
"DVD","Music"};
for (int i = 0; i < typeStr.length; ++i) {

box_type.addItem(typeStr[i]);
}
box_type.setBackground(Color.lightGray);
box_type.setFocusable(true);
top.add(new JLabel("Price: ", JLabel.RIGHT));
top.add(addprice);
add("North", top);

JPanel middle = new JPanel();
middle.add(new JLabel("Notes: ", JLabel.RIGHT));
middle.add(list);
list.setEditable(true);
add("Center", middle);

JPanel left = new JPanel();
left.add(new JLabel(ri));
add("West", left);

JPanel right = new JPanel();
right.add(new JSeparator(SwingConstants.VERTICAL));
add("East", right);


JPanel bottom = new JPanel();
bottom.add(addT);
addT.addActionListener(this);
addT.setBackground(Color.lightGray);
addT.setToolTipText("Add as new track");
add("South", bottom);}

public void actionPerformed(ActionEvent e) {
                if (e.getSource() == addT) 
                {


                }
}
}
 
Hi,

Ich habe eine Klasse wo ich in eine Form ausfüllen muss

Ähm was? *kopfkratz* ... dieser Satzbau ist irgendwie nicht korrekt und ich verstehe nicht ganz ;-)

Aber:
Ich würde sagen, du musst aus der Klasse ein Objekt machen und dann die Werte über eine Methode an dieses Objekt übergeben.

Du kannst nicht einfach so auf die Klasse zugreifen.

Bsp.
Code:
KlassenName klasse = new KlassenNamen()

oder

Code:
KlassenName klasse = new KlassenNamen(ÜbergabeVonParameternAnKonstruktor)

wenn die Klasse nun eine Methode enthält z.B. gibMirDieInfos(), dann musst du eben diese aufrufen

Code:
klasse.gibMirDieInfos(evtl. Parameter);

Wenn du aus Klassen Objekte machst, kannst du Daten zwischen diesen austauschen.

Interessant wäre auch zu wissen, um welche Art von Daten es sich handelt. Daten aus einer Datei, aus einer DB oder sind die Daten nur im flüchtigen Speicher zur Laufzeit vorhanden?
 
Zurück