ERLEDIGT
NEIN
NEIN
ANTWORTEN
2
2
ZUGRIFFE
402
402
EMPFEHLEN
-
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 :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
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 :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
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,
Ähm was? *kopfkratz* ... dieser Satzbau ist irgendwie nicht korrekt und ich verstehe nicht ganzIch habe eine Klasse wo ich in eine Form ausfüllen muss
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 :1
KlassenName klasse = new KlassenNamen()
oder
Code :1
KlassenName klasse = new KlassenNamen(ÜbergabeVonParameternAnKonstruktor)
wenn die Klasse nun eine Methode enthält z.B. gibMirDieInfos(), dann musst du eben diese aufrufen
Code :1
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?
-
08.01.11 09:46 #3
- Registriert seit
- Jun 2009
- Beiträge
- 870
@Code46
Wenn du weitere Hilfe dazu brauchst, lies mal hier: http://openbook.galileocomputing.de/...sel_03_001.htm
Ähnliche Themen
-
JTabel wird nicht richtig angezeigt aber ich kann etwas hinzufügen?
Von Code46 im Forum Swing, Java2D/3D, SWT, JFaceAntworten: 1Letzter Beitrag: 19.01.11, 10:44 -
Innerhalb einer Klasse eine Instanz einer anderen Klasse
Von HackerNeo im Forum VisualStudio & MFCAntworten: 1Letzter Beitrag: 17.11.09, 13:08 -
Klasse in einer anderen Klasse verwerden?
Von MadCrusher im Forum PHPAntworten: 29Letzter Beitrag: 16.02.08, 04:26 -
Klasse nutzt Methode einer anderen Klasse
Von sasfed im Forum PHPAntworten: 2Letzter Beitrag: 06.02.06, 19:57 -
2 Tabellen, etwas finden, was in einer, aber nicht in der anderen ist
Von ZidaneIX im Forum PHPAntworten: 1Letzter Beitrag: 08.05.04, 18:09





Zitieren
Login





