Inhalt von JTextField an 2. Klasse übergeben

Masterpurzel

Mitglied
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:
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:
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(); // immer null
			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:
SwingApplication swingapp = new SwingApplication();
deutsch = swingapp.textfield_input.getText();

immer einen Wert von NULL!

Ich hoffe es kann mir jemand helfen.
 
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.
 
Zuletzt bearbeitet:
Das liegt wohl daran dass du bei
Code:
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
 
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
 
Zuletzt bearbeitet:

Neue Beiträge

Zurück