ERLEDIGT
NEIN
NEIN
ANTWORTEN
1
1
ZUGRIFFE
385
385
EMPFEHLEN
-
Ich habe eine Frame wo ich in eine Form(DVD Name,DVD Preis etc.) ausfüllen kann. 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 Frame. 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 mit dem JTABLE:
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
package cw; import java.util.*; import javax.swing.*; import javax.swing.table.*; import java.awt.*; import java.awt.event.*; class Product implements Comparable { private String name; private int cost; public Product(int c, String n) { cost = c; name = n; } public Product() { this (0,"defaul"); } 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 DVD extends JFrame implements ActionListener { ArrayList products = new ArrayList(); JPanel panInput = new JPanel( new GridLayout(3,1)), panAdd = new JPanel(), panDelSort = new JPanel(); JButton btnAdd = new JButton("Add"), btnDelete = new JButton("Delete"), btnSort = new JButton("Sort Products"); JTable tab = new JTable(); JMenuBar menubar = new JMenuBar(); JMenu file = new JMenu("File"); JMenu Edit = new JMenu("Edit"); JMenuItem save = new JMenuItem("Save"); JMenuItem exit = new JMenuItem("Exit"); JMenuItem AddDVD = new JMenuItem("Add DVD"); JMenuItem UpdateDVD = new JMenuItem("Update DVD"); JMenuItem SearchDVD = new JMenuItem("Search DVD"); DefaultTableModel tabMod = new DefaultTableModel(); Product [] initialProducts = {}; public DVD() { super("Product list"); tab.setModel(tabMod); tabMod.addColumn("Title"); tabMod.addColumn("Type"); tabMod.addColumn("Notes"); tabMod.addColumn("Price"); tabMod.addColumn("Image"); btnAdd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { } }); btnDelete.setToolTipText("Select the records to delete first"); btnDelete.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { } }); btnSort.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { } }); menubar.add(file); menubar.add(Edit); file.add(save); file.add(exit); Edit.add(AddDVD); Edit.add(UpdateDVD); Edit.add(SearchDVD); //panDelSort.add(btnAdd); // panDelSort.add(btnDelete); // panDelSort.add(btnSort); panInput.add(panAdd); panInput.add(panDelSort); exit.addActionListener(this); AddDVD.addActionListener(this); UpdateDVD.addActionListener(this); SearchDVD.addActionListener(this); this.setJMenuBar(menubar); getContentPane().add(panInput, BorderLayout.SOUTH); getContentPane().add(new JScrollPane(tab)); loadInitialProducts(); } 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){ DVD me = new DVD(); me.addWindowListener(new WindowAdapter () { @Override public void windowClosing(WindowEvent e) { System.exit(0) ; } }); me.pack(); me.setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == AddDVD) { new AddDVD(); } else { if (e.getSource() == SearchDVD) { new SearchDVD(); } else { if (e.getSource() == UpdateDVD) { new UpdateDVD(); } else if (e.getSource() == exit) { System.exit(0); } } } } }
Der andere Frame mit der Form:
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
package cw; import java.awt.*; import java.awt.event.*; import java.io.File; import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; import static javax.swing.JOptionPane.*; public class AddDVD extends JFrame implements ActionListener { JTextField jtfTitle,jtfPrice,jtfImage,jtfNotes; JButton btnadd,btnclear,btnimage; JComboBox jcbType = new JComboBox(); LibraryData db = new LibraryData(); int value; String name,notes,image; boolean type,ok; public AddDVD() { btnadd = new JButton("Add new DVD/Music"); btnclear = new JButton("Clear Fields"); btnimage = new JButton("Upload Image"); jtfTitle = new JTextField(30); jtfPrice = new JTextField(5); jtfNotes = new JTextField(40); jtfImage = new JTextField(50); setLayout(new BorderLayout()); setBounds(400, 400, 450, 200); setTitle("Add DVD/Music"); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setResizable(false); setVisible(true); JPanel top = new JPanel(); top.setLayout(new GridLayout(6, 3)); top.add(new JLabel("Title: ", JLabel.RIGHT)); top.add(jtfTitle); top.add(new JLabel("Type:", JLabel.RIGHT)); top.add(jcbType); String[] ratingStr = { "Film","Music"}; for (int i = 0; i < ratingStr.length; ++i) { jcbType.addItem(ratingStr[i]); } jcbType.setBackground(Color.lightGray); jcbType.setFocusable(true); top.add(new JLabel("Notes:", JLabel.RIGHT)); top.add(jtfNotes); top.add(new JLabel("Price:", JLabel.RIGHT)); top.add(jtfPrice); top.add(new JLabel("Image:", JLabel.RIGHT)); top.add(jtfImage); add("North", top); JPanel bottom = new JPanel(); bottom.add(btnadd); btnadd.addActionListener(this); bottom.add(btnclear); btnclear.addActionListener(this); bottom.add(btnimage); btnimage.addActionListener(this); add("South", bottom);} public void actionPerformed(ActionEvent e) { if (e.getSource() == btnadd){ LibraryData.dataConection(); try { name = jtfTitle.getText(); if (name.equals("")){ showMessageDialog(this,"Enter DVD name");return; } if (jcbType.getSelectedItem() =="DVD"){type = true;} else{type = false;} notes = jtfNotes.getText(); value = Integer.parseInt(jtfPrice.getText()); image= jtfImage.getText(); } catch (Exception nfe) { System.out.println(nfe.toString()); JOptionPane.showMessageDialog(this, " Please Input Value of Item in Numbers only "); return; } LibraryData.add(name, type, notes, value, image); if (!ok) { JOptionPane.showMessageDialog(this, " Item Successfully added to Database"); } if (ok) { JOptionPane.showMessageDialog(this, "Item Could Not be added into the Database "); } } else if (e.getSource() ==btnimage) { try { JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new File("C:\\Users\\Serdar\\Desktop\\CW\\")); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); FileNameExtensionFilter filter = new FileNameExtensionFilter( "Image files", "jpg", "png"); chooser.setFileFilter(filter); try { int code = chooser.showOpenDialog(this); if (code == JFileChooser.APPROVE_OPTION) { File selectedFile = chooser.getSelectedFile(); jtfImage.setText(selectedFile.getName());} } catch (Exception f) {} } catch(Exception ev){} } else if (e.getSource() == btnclear) { jtfTitle.setText(""); jtfNotes.setText(""); jtfPrice.setText(""); jtfImage.setText(""); } } }
-
Übergib die Table dem anderen frame über eine set-Funktion - Das wäre mein Ansatz.
in dem Frame, in dem sich die Table nicht befindet erstellst du eine JTable-Variable
Über eine set-Methode im anderen Frame kann das Table vom ersten Frame übergeben werdenCode :1
JTable table = null;
- Hier ist wichtig zu wissen, dass kein neues Table erstellt wird, sondern die Speicheradresse weitergegeben wird. Daraus folgt, dass du direkt auf das Table im anderen Frame zugreifst.
Du brauchst im Frame mit dem Table also nur
aufrufen. Danach kannst du es auch im anderen Frame verwenden.Code :1
anderesFrame.setTable(tab);
Hoffe das war verständlich
Bei Fragen - schieß los.
MfG - Pasukaru
Edit: Hab hier was gefunden, das erklärt wie's funktioniert
Zitat von Snape
Geändert von Pasukaru (11.01.11 um 17:57 Uhr)
Ähnliche Themen
-
JTable aus Vector füllen
Von tameck im Forum Swing, Java2D/3D, SWT, JFaceAntworten: 3Letzter Beitrag: 14.04.08, 11:03 -
JTable dynamisch füllen
Von DonAqua im Forum Swing, Java2D/3D, SWT, JFaceAntworten: 2Letzter Beitrag: 18.02.08, 16:09 -
JTable mit Inhalt füllen
Von Musicman75 im Forum Swing, Java2D/3D, SWT, JFaceAntworten: 7Letzter Beitrag: 09.08.07, 00:38 -
JTable mit Daten füllen
Von ratze79 im Forum JavaAntworten: 2Letzter Beitrag: 04.06.07, 20:33 -
Problem mit dem Füllen einer JTable
Von Schnacki im Forum Swing, Java2D/3D, SWT, JFaceAntworten: 6Letzter Beitrag: 21.03.07, 14:23





Zitieren
Login





