tutorials.de Buch-Aktion 05/2012
Like Tree1Danke
  • 1 Beitrag von sebastianb
ERLEDIGT
JA
ANTWORTEN
3
ZUGRIFFE
536
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    Avatar von Masterpurzel
    Masterpurzel Masterpurzel ist offline Mitglied Silber
    Registriert seit
    Apr 2007
    Ort
    Vienna, Austria, Austria
    Beiträge
    52
    Hallo Community,

    ich sitze jetzt schon seit einigen Stunden an meinem Problem und konnte bisher keine Lösung finden!

    Mein Quellcode sieht wie folgt aus:

    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
    
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
     
    public class SwingApplication {
        public JTextField textfield_input;
        JLabel label;
        JButton button_de_jp, button_clear, button_jp_de;
        public JTextArea textarea_output;
        
        public SwingApplication() {
            //Create and set up the window.
            JFrame jframe = new JFrame("Japanisches Wörterbuch");
            jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            //Set the frame icon to an image loaded from a file.
            //frame.setIconImage(new ImageIcon(imgURL).getImage());
            
            //Set Sizeable to false
            jframe.setResizable(false);
            
            //Create GridBagLayout
            jframe.setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
            
            //Ad new objects
            textfield_input = new JTextField();
            c.fill = GridBagConstraints.HORIZONTAL;
            c.weightx = 0;
            c.weighty = 0;
            c.gridx = 1; //Spalte
            c.gridy = 0; //Reihe
            c.gridheight = 1;
            c.gridwidth = 5; //Anzahl Spaltenlänge
            jframe.add(textfield_input, c);
            
            label = new JLabel(" ");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.weightx = 0;
            c.weighty = 0;
            c.gridx = 1; //Spalte
            c.gridy = 1; //Reihe
            c.gridheight = 1;
            c.gridwidth = 5; //Anzahl Spaltenlänge
            jframe.add(label, c);
            
            button_de_jp = new JButton("Deutsch -> Japanisch");
            button_de_jp.addActionListener(new ButtonAction());
            c.fill = GridBagConstraints.HORIZONTAL;
            c.weightx = 0;
            c.weighty = 0;
            c.gridx = 1; //Spalte
            c.gridy = 2; //Reihe
            c.gridheight = 1;
            c.gridwidth = 1; //Anzahl Spaltenlänge
            jframe.add(button_de_jp, c);
            
            label = new JLabel(" ");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.weightx = 0;
            c.weighty = 0;
            c.gridx = 2; //Spalte
            c.gridy = 2; //Reihe
            c.gridheight = 1;
            c.gridwidth = 1; //Anzahl Spaltenlänge
            jframe.add(label, c);
            
            button_clear = new JButton("Inhalt löschen");
            button_clear.addActionListener(new ButtonAction());
            c.fill = GridBagConstraints.HORIZONTAL;
            c.weightx = 0;
            c.weighty = 0;
            c.gridx = 3; //Spalte
            c.gridy = 2; //Reihe
            c.gridheight = 1;
            c.gridwidth = 1; //Anzahl Spaltenlänge
            jframe.add(button_clear, c);
            
            label = new JLabel(" ");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.weightx = 0;
            c.weighty = 0;
            c.gridx = 4; //Spalte
            c.gridy = 2; //Reihe
            c.gridheight = 1;
            c.gridwidth = 1; //Anzahl Spaltenlänge
            jframe.add(label, c);
            
            button_jp_de = new JButton("Japanisch -> Deutsch");
            button_jp_de.addActionListener(new ButtonAction());
            button_jp_de.setName("jp_de");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.weightx = 0;
            c.weighty = 0;
            c.gridx = 5; //Spalte
            c.gridy = 2; //Reihe
            c.gridheight = 1;
            c.gridwidth = 1; //Anzahl Spaltenlänge
            jframe.add(button_jp_de, c);
            
            label = new JLabel(" ");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.weightx = 0;
            c.weighty = 0;
            c.gridx = 1; //Spalte
            c.gridy = 3; //Reihe
            c.gridheight = 1;
            c.gridwidth = 3; //Anzahl Spaltenlänge
            jframe.add(label, c);
            
            textarea_output = new JTextArea(5, 20);
            textarea_output.setEditable(false); //Set editable to false
            c.fill = GridBagConstraints.HORIZONTAL;
            c.weightx = 0;
            c.weighty = 0;
            c.gridx = 1; //Spalte
            c.gridy = 4; //Reihe
            c.gridheight = 1;
            c.gridwidth = 5; //Anzahl Spaltenlänge
            jframe.add(textarea_output, c);
            
            //Size the frame.
            jframe.pack();
     
            //Show it.
            jframe.setVisible(true);
        }
    }

    die 2. Klasse wie folgt:

    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
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
     
    public class ButtonAction implements ActionListener {
        public String deutsch = null;
        
        /*
         * Datenbank
         **/
        String url = "jdbc:mysql://***:3306/woerterbuch";
        String username = "***";
        String password = "***";
        
        @Override
        public void actionPerformed(ActionEvent event) {
            if(event.getActionCommand().equals("Deutsch -> Japanisch")) {
                SwingApplication swingapp = new SwingApplication();
                deutsch = swingapp.textfield_input.getText(); [B]// immer null[/B]
                System.out.println("Input: "+deutsch);
                Connection conn = null;
                PreparedStatement s;
                try {
                    Class.forName ("com.mysql.jdbc.Driver").newInstance();
                    conn = DriverManager.getConnection (url, username, password);
                    System.out.println ("Database connection established");
                    s = conn.prepareStatement("SELECT id, deutsch, japanisch FROM woerterbuch WHERE deutsch = ?");
                    s.setString(1, deutsch);
                    s.executeQuery();
                    ResultSet rs = s.getResultSet();
                    rs.next();
                    swingapp.textarea_output.setText(rs.getString("japanisch"));
                } catch (Exception e) {
                    e.printStackTrace();
                    System.err.println ("Cannot connect to database server");
                } finally {
                    if (conn != null) {
                        try {
                            conn.close ();
                            System.out.println ("Database connection terminated");
                        } catch (Exception e) {
                            /* ignore close errors */
                        }
                    }
                }
            } else if(event.getActionCommand().equals("Inhalt löschen")) {
                SwingApplication swingapp = new SwingApplication();
                swingapp.textarea_output.setText("");
            } else if(event.getActionCommand().equals("Japanisch -> Deutsch")) {
                SwingApplication swingapp = new SwingApplication();
                swingapp.textarea_output.setText("Japanisch -> Deutsch");
            }
        }
     
    }

    Ich bekomme bei

    Code :
    1
    2
    
    SwingApplication swingapp = new SwingApplication();
    deutsch = swingapp.textfield_input.getText();

    immer einen Wert von NULL!

    Ich hoffe es kann mir jemand helfen.
     

  2. #2
    MiMi MiMi ist offline Mitglied Smaragd
    Registriert seit
    Sep 2007
    Beiträge
    1.177
    Deine SwingApplication wird nehm ich an in einer anderne Klasse erstellt?
    Dann ist es kein Wunder, indem Moment wo du auf den Button klickst, erstellt du ein neues Objekt der SwingApplication, in diesem Objekt ist natuerlich nichts in deinem Textfeld.

    Du erstellst in jedem (else)if ein neues Objekt von der SwingApplication. Das ist glaub ich nicht das was du willst.
    Geändert von MiMi (09.11.10 um 09:35 Uhr)
     

  3. #3
    sebastianb sebastianb ist offline Mitglied Gold
    Registriert seit
    Dec 2009
    Beiträge
    125
    Das liegt wohl daran dass du bei
    Code :
    1
    
    SwingApplication swingapp = new SwingApplication();
    ein neues Objekt deiner ursprünglichen Anwendung erstellst. Falls du auf die Werte des Textfeldes zugreifen willst musst du entweder eine Referenz von SwinApplication an ButtonAction mitgeben (Meiner Meinung nach nicht sonderlich schön) oder du implementierst das Interface ActionListener in SwingApplication und überschreibst dort die Methode actionPerformed.
    Das Verwalten von Datenbankverbindungen haben in ActionPerformed eigentlich auch nichts zu suchen

    Gruß

    Sebastian
    Masterpurzel bedankt sich. 

  4. #4
    Avatar von Masterpurzel
    Masterpurzel Masterpurzel ist offline Mitglied Silber
    Registriert seit
    Apr 2007
    Ort
    Vienna, Austria, Austria
    Beiträge
    52
    Danke für die Tipps!

    Ich werde den Tipp mit dem ActionListener in SwingApplication versuchen. Hoffe das klappt.

    €dit: ich habe es nun so gelöst, das ich den AL in der Class SwingApplication definiert habe.

    Danke für die Hilfe!

    Lg

    Masterpurzel
    Geändert von Masterpurzel (09.11.10 um 10:04 Uhr)
     

Ähnliche Themen

  1. JTextField - Inhalt abholen
    Von mr_waiter im Forum Java Grundlagen
    Antworten: 3
    Letzter Beitrag: 11.10.08, 13:07
  2. 2 classen (JTextField --> variablen übergeben)
    Von Fastchiller im Forum Java
    Antworten: 2
    Letzter Beitrag: 04.04.08, 13:22
  3. JTextField - Inhalt markieren
    Von Java_Neuling im Forum Swing, Java2D/3D, SWT, JFace
    Antworten: 6
    Letzter Beitrag: 08.10.07, 08:08
  4. DB Inhalt in JTextField
    Von The_Answer1985 im Forum Swing, Java2D/3D, SWT, JFace
    Antworten: 9
    Letzter Beitrag: 17.08.07, 08:45
  5. Antworten: 3
    Letzter Beitrag: 30.03.07, 07:42

Stichworte