ERLEDIGT
JA
JA
ANTWORTEN
2
2
ZUGRIFFE
1715
1715
EMPFEHLEN
-
Halli Hallo!
Ich bin ziemlich neu in der Welt von Java.
Habe bis jetzt zwei Lehrgänge besucht und wollte nun ein wenig rumprobieren.
Da wir regelmäßig zu Hause die Zählerstände ablesen, dachte ich mir, dass ich ein Programm baue, bei dem man die Daten in einer GUI eingibt und diese dann in eine Excel-Tabelle geschrieben werden!
Leider hakt es irgendwo an einer STelle und ich weiß nicht wo
Vielleicht kann mir jemand helfen!?
Irgendwie nimmt er die Daten im TextField nicht an. Programmier in Eclipse und das zeigt mir keinerlei Fehler an!
Habe zwei Klassen gebastelt, die ich dann über eine Klasse mit main-Methode aufrufe:
main-Klasse (Application):
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
import java.io.IOException; public class Application { public static void main(String[] args) { Tabelle2 tab= new Tabelle2(); try { tab.tabelleErzeugen(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } GUI g=new GUI("Zaehlerstand"); g.setVisible(true); } }
Klasse, um Tabelle zu erzeugen und Einträge zu steuern:
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
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.hssf.usermodel.*; import org.apache.poi.hssf.util.HSSFColor; public class Tabelle2 { HSSFWorkbook wb; HSSFSheet sh ; HSSFRow row; HSSFCell cell; HSSFCellStyle csty; public void tabelleErzeugen()throws IOException { // Neue Mappe erzeugen wb = new HSSFWorkbook(); // Neue Tabelle erzeugen sh = wb.createSheet(); // Objektreferenz auf Reihe erzeugen row = null; // Objektreferenz auf Zelle erzeugen cell = null; // Zellformat Objekt erzeugen csty = wb.createCellStyle(); // Datenformat Objekt erzeugen // HSSFDataFormat df = wb.createDataFormat(); // Font-Objekt erzeugen HSSFFont font = wb.createFont(); // Schriftgröße auf 12pt einstellen font.setFontHeightInPoints((short) 12); // Schriftfarbe blau font.setColor((short)0xC); // Schriftart font.setFontName("Times New Roman"); // Fett font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // Schriftart auf Zellformat-Objekt anwenden csty.setFont(font); // Rahmen einstellen csty.setBorderBottom(HSSFCellStyle.BORDER_THIN); csty.setBottomBorderColor(HSSFColor.PINK.index); csty.setBorderLeft(HSSFCellStyle.BORDER_THICK); // Zeile mit Überschriften erzeugen short rownum=0; row=sh.createRow(rownum); cell=row.createCell(0); cell.setCellStyle(csty); cell.setCellValue("Datum"); cell=row.createCell(2); cell.setCellStyle(csty); cell.setCellValue("Stromzaehler"); cell=row.createCell(4); cell.setCellStyle(csty); cell.setCellValue("Wasserzaehler"); cell=row.createCell(6); cell.setCellStyle(csty); cell.setCellValue("Gaszaehler"); // Ausgabedatei ereugen FileOutputStream out; try { out = new FileOutputStream("Tabelle.xls"); // Mappe schreiben und Datei schließen wb.write(out); out.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // Ende Tabelle erzeugen public void eintraegeErzeugen(String pDatum, String pZaehler, String pElement) throws FileNotFoundException, IOException { // Excel Mappe öffnen wb = new HSSFWorkbook(new FileInputStream("Tabelle.xls")); // Tabelle1 auswählen sh = wb.getSheet("Tabelle1"); // Objektreferenz auf Zeile erzeugen HSSFRow row = null; // Objektreferenz auf Zelle erzeugen HSSFCell cell = null; int anzahl_zeilen=sh.getLastRowNum(); row=sh.createRow(anzahl_zeilen+1); cell=row.createCell(0); cell.setCellValue(pDatum); if (pElement.equals("Stromzaehler")) { cell=row.createCell(2); cell.setCellValue(pZaehler); } if (pElement.equals("Wasserzaehler")) { cell=row.createCell(4); cell.setCellValue(pZaehler); } if (pElement.equals("Gaszaehler")) { cell=row.createCell(6); cell.setCellValue(pZaehler); } // Ausgabedatei ereugen FileOutputStream out; try { out = new FileOutputStream("Tabelle.xls"); // Mappe schreiben und Datei schließen wb.write(out); out.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // Ende eintraegeErzeugen } // Ende class
Klasse, die GUI erstellt:
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
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; import java.awt.GridLayout; import java.awt.Label; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Timer; import java.util.TimerTask; import java.util.Vector; import javax.swing.DefaultComboBoxModel; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.event.ListDataEvent; import javax.swing.event.ListDataListener; public class GUI extends JFrame implements ListDataListener { private static final long serialVersionUID = 1L; Tabelle2 tab=new Tabelle2(); private DefaultComboBoxModel comboBoxModel1; JLabel l1=new JLabel("Datum",0); JLabel l2=new JLabel("Datum",0); JTextField tf1=new JTextField(20); JComboBox cb1; JLabel l3=new JLabel("Zaehlerstand",0); JTextField tf2=new JFormattedTextField(20); JButton b1=new JButton("eintragen"); JPanel p=new JPanel(); /** * Fenstererzeugung * @param pTitel */ public GUI(String pTitel) { super(pTitel); this.createGUI(); this.eventhandling(); } private void createGUI() { this.fillComboBox(); this.add(this.l1, BorderLayout.NORTH); l1.setForeground(Color.RED); l1.setFont(new Font("Datum", 0, 24)); // Layout stapeln this.p.setLayout(new GridLayout(4,2)); this.p.add(this.cb1); this.p.add(this.b1); this.p.add(new Label()); this.p.add(new Label()); this.p.add(this.l2); l2.setFont(new Font("Datum", 0, 18)); this.p.add(this.tf1); this.p.add(this.l3); l3.setFont(new Font("Zaehlerstand", 0, 18)); this.p.add(this.tf2); this.add(this.p, BorderLayout.CENTER); this.pack(); } private void eventhandling() { this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setResizable(false); b1.addActionListener(new ActionListener () { @Override public void actionPerformed(ActionEvent e) { try{ if ( ! tf1.getText().isEmpty() || ! tf2.getText().isEmpty() ){ tf1.setText(""); tf2.setText(""); cb1.getSelectedItem().toString(); tab.eintraegeErzeugen(tf1.getText(), tf2.getText(), cb1.getSelectedItem().toString()); } else { JOptionPane.showMessageDialog( GUI.this, "Bitte geben Sie das Datum UND den Zaehlerstand der Ablesung an"); } } catch (Exception ex) { // ex.getStackTrace(); JOptionPane.showMessageDialog(GUI.this, "FEHLER"); } } }); // Uhrzeit einfuegen Timer t=new Timer(); // (Klasse, wann beginnen, warten bis naechster Aufruf) t.schedule(new Datum(), 0, 1000); } private void fillComboBox() { cb1=new JComboBox(); // ComboBoxModel erzeugen Vector<String> zaehler=new Vector<String>(); zaehler.add("Stromzaehler"); zaehler.add("Wasserzaehler"); zaehler.add("Gaszaehler"); comboBoxModel1=new DefaultComboBoxModel(zaehler); comboBoxModel1.addListDataListener(this); //ComboBoxModel setzen cb1.setModel(comboBoxModel1); } @Override public void contentsChanged(ListDataEvent arg0) { System.out.println(cb1.getModel().getSelectedItem()); } @Override public void intervalAdded(ListDataEvent arg0) { // TODO Auto-generated method stub } @Override public void intervalRemoved(ListDataEvent arg0) { // TODO Auto-generated method stub } private class Datum extends TimerTask { SimpleDateFormat date=new SimpleDateFormat( "EEE, d MMM yyyy HH:mm:ss"); @Override public void run() { String date1=date.format(new Date()); l1.setText(date1); } } }
-
Moin!
Du fragst hier ab, ob eins der beiden Textfelder nicht leer ist, leerst sie dann selbst und willst dann mit dem leeren Text die Tabelle füllen?Code :1 2 3 4 5 6 7
if ( ! tf1.getText().isEmpty() || ! tf2.getText().isEmpty() ){ tf1.setText(""); tf2.setText(""); cb1.getSelectedItem().toString(); tab.eintraegeErzeugen(tf1.getText(), tf2.getText(), cb1.getSelectedItem().toString());
So,
würde es mehr Sinn machen!Code :1 2 3 4
if ( !tf1.getText().isEmpty() && !tf2.getText().isEmpty() ){ tab.eintraegeErzeugen(tf1.getText(), tf2.getText(), cb1.getSelectedItem().toString()); }
Viele Grüße
-
Danke für den Tipp, Den Fehler hatte ich auch nun schon selbst gefunden!
Der richtige Fehler:
Das Sheet im geöffneten Workbook heißt "Sheet0" und nicht "Tabelle 1".
Hier mal der fertig programmierte und funktionierende Code:
Tabelle:
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
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.hssf.usermodel.*; import org.apache.poi.hssf.util.HSSFColor; public class Tabelle2 { HSSFWorkbook wb; HSSFSheet sh ; HSSFRow row; HSSFCell cell; HSSFCellStyle csty; public void tabelleErzeugen()throws IOException { // Neue Mappe erzeugen wb = new HSSFWorkbook(); // Neue Tabelle erzeugen sh = wb.createSheet(); // Objektreferenz auf Reihe erzeugen row = null; // Objektreferenz auf Zelle erzeugen cell = null; // Zellformat Objekt erzeugen csty = wb.createCellStyle(); // Datenformat Objekt erzeugen // HSSFDataFormat df = wb.createDataFormat(); // Font-Objekt erzeugen HSSFFont font = wb.createFont(); // Schriftgröße auf 12pt einstellen font.setFontHeightInPoints((short) 12); // Schriftfarbe blau font.setColor((short)0xC); // Schriftart font.setFontName("Times New Roman"); // Fett font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // Schriftart auf Zellformat-Objekt anwenden csty.setFont(font); // Rahmen einstellen csty.setBorderBottom(HSSFCellStyle.BORDER_THIN); csty.setBottomBorderColor(HSSFColor.PINK.index); csty.setBorderLeft(HSSFCellStyle.BORDER_THICK); // Zeile mit Überschriften erzeugen short rownum=0; row=sh.createRow(rownum); cell=row.createCell(0); cell.setCellStyle(csty); cell.setCellValue("Datum"); cell=row.createCell(2); cell.setCellStyle(csty); cell.setCellValue("Stromzaehler"); cell=row.createCell(4); cell.setCellStyle(csty); cell.setCellValue("Wasserzaehler"); cell=row.createCell(6); cell.setCellStyle(csty); cell.setCellValue("Gaszaehler"); // Ausgabedatei ereugen FileOutputStream out; try { out = new FileOutputStream("Tabelle.xls"); // Mappe schreiben und Datei schließen wb.write(out); out.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // Ende Tabelle erzeugen public void eintraegeErzeugen(String pDatum, String pZaehler, String pElement) throws FileNotFoundException, IOException { System.out.println(pDatum+" "+pZaehler+" "+pElement); // Excel Mappe öffnen wb = new HSSFWorkbook(new FileInputStream("Tabelle.xls")); // Tabelle1 auswählen sh = wb.getSheet("Sheet0"); // Objektreferenz auf Zeile erzeugen HSSFRow row = null; // Objektreferenz auf Zelle erzeugen HSSFCell cell = null; int anzahl_zeilen=sh.getLastRowNum(); row=sh.createRow(anzahl_zeilen+1); cell=row.createCell(0); cell.setCellValue(pDatum); if (pElement.equals("Stromzaehler")) { cell=row.createCell(2); cell.setCellValue(pZaehler); } if (pElement.equals("Wasserzaehler")) { cell=row.createCell(4); cell.setCellValue(pZaehler); } if (pElement.equals("Gaszaehler")) { cell=row.createCell(6); cell.setCellValue(pZaehler); } // Ausgabedatei ereugen FileOutputStream out; try { out = new FileOutputStream("Tabelle.xls"); // Mappe schreiben und Datei schließen wb.write(out); out.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // Ende eintraegeErzeugen public void tabelleFuellen(String pDatum, String pZaehler, String pElement) throws FileNotFoundException, IOException { File tab=new File("Tabelle.xls"); if( !tab.exists()) { this.tabelleErzeugen(); // System.out.println("Tabelle wird erzeugt"); this.eintraegeErzeugen(pDatum, pZaehler, pElement); // System.out.println("Eintraege werden erzeugt"); } else if ( tab.exists()) this.eintraegeErzeugen(pDatum, pZaehler, pElement); }// Ende tabelleFuellen() } // Ende class
GUI:
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
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; import java.awt.GridLayout; import java.awt.Label; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Timer; import java.util.TimerTask; import java.util.Vector; import javax.swing.DefaultComboBoxModel; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.event.ListDataEvent; import javax.swing.event.ListDataListener; public class GUI extends JFrame implements ListDataListener { private static final long serialVersionUID = 1L; Tabelle2 tab=new Tabelle2(); private DefaultComboBoxModel comboBoxModel1; JLabel l1=new JLabel("Datum",0); JLabel l2=new JLabel("Datum",0); JTextField tf1=new JTextField(20); JComboBox cb1; JLabel l3=new JLabel("Zaehlerstand",0); JTextField tf2=new JFormattedTextField(); JButton b1=new JButton("eintragen"); JPanel p=new JPanel(); /** * Fenstererzeugung * @param pTitel */ public GUI(String pTitel) { super(pTitel); this.createGUI(); this.eventhandling(); } private void createGUI() { this.fillComboBox(); this.add(this.l1, BorderLayout.NORTH); l1.setForeground(Color.RED); l1.setFont(new Font("Datum", 0, 24)); // Layout stapeln this.p.setLayout(new GridLayout(4,2)); this.p.add(this.cb1); this.p.add(this.b1); this.p.add(new Label()); this.p.add(new Label()); this.p.add(this.l2); l2.setFont(new Font("Datum", 0, 18)); this.p.add(this.tf1); this.p.add(this.l3); l3.setFont(new Font("Zaehlerstand", 0, 18)); this.p.add(this.tf2); this.add(this.p, BorderLayout.CENTER); this.pack(); } private void eventhandling() { this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setResizable(false); b1.addActionListener(new ActionListener () { @Override public void actionPerformed(ActionEvent e) { try{ if ( ! tf1.getText().isEmpty() && ! tf2.getText().isEmpty() ){ tab.tabelleFuellen(tf1.getText(), tf2.getText(), cb1.getSelectedItem().toString()); tf1.setText(""); tf2.setText(""); } else { JOptionPane.showMessageDialog( GUI.this, "Bitte geben Sie das Datum UND den Zaehlerstand der Ablesung an"); } } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(GUI.this, "FEHLER "+ex); } } }); // Uhrzeit einfuegen Timer t=new Timer(); // (Klasse, wann beginnen, warten bis naechster Aufruf) t.schedule(new Datum(), 0, 1000); } private void fillComboBox() { cb1=new JComboBox(); // ComboBoxModel erzeugen Vector<String> zaehler=new Vector<String>(); zaehler.add("Stromzaehler"); zaehler.add("Wasserzaehler"); zaehler.add("Gaszaehler"); comboBoxModel1=new DefaultComboBoxModel(zaehler); comboBoxModel1.addListDataListener(this); //ComboBoxModel setzen cb1.setModel(comboBoxModel1); } @Override public void contentsChanged(ListDataEvent arg0) { System.out.println(cb1.getModel().getSelectedItem()); } @Override public void intervalAdded(ListDataEvent arg0) { // TODO Auto-generated method stub } @Override public void intervalRemoved(ListDataEvent arg0) { // TODO Auto-generated method stub } private class Datum extends TimerTask { SimpleDateFormat date=new SimpleDateFormat( "EEE, d MMM yyyy HH:mm:ss"); @Override public void run() { String date1=date.format(new Date()); l1.setText(date1); } } }
Application:
Code :1 2 3 4 5 6 7 8 9
public class Application { public static void main(String[] args) { GUI g=new GUI("Zaehlerstand"); g.setVisible(true); } }Geändert von Tinipieps (08.03.10 um 14:06 Uhr) Grund: Damit vorhandene Tabelle nicht überschrieben wird
Ähnliche Themen
-
Excel-Tab via UserForm füllen
Von manthom im Forum Visual Basic 6.0Antworten: 0Letzter Beitrag: 19.02.08, 20:10 -
Mithilfe Visual Basic in Excel Zählen
Von Audrey im Forum Visual Basic 6.0Antworten: 1Letzter Beitrag: 15.02.08, 15:44 -
Excel: sehr große Tabelle automatisch füllen
Von fiacyberz im Forum Office-AnwendungenAntworten: 4Letzter Beitrag: 13.09.06, 13:19 -
Tabelle mithilfe der GD-Bibliothek in eine Grafik umwandeln
Von Catscrash im Forum PHPAntworten: 1Letzter Beitrag: 03.05.06, 16:21 -
Tabelle mit Daten aus anderer Tabelle füllen
Von kippi01 im Forum PHPAntworten: 2Letzter Beitrag: 02.06.05, 22:56





Zitieren
Login




