# Leere Textfelder abfangen



## Popon (31. Mrz 2010)

Hallo zusammen... 

Ich habe da ein Problem... Und zwar habe ich ein Formular mit dem ich in die Datenbank schreibe...
Nun soll beim Drücken des Buttons, auf leere Textfelder hingewiesen werden...

Erstmal ein bild wie mein Formular ausschaut:







Zum einem habe ich zwei Textfelder. In dem einen muss man den Namen eingeben. In dem anderen ein Integerwert... Mit dem Integerwert funktioniert das einwandfrei. 

Zum anderen habe ich drei Drop Down Menüs, die alle drei selektiert werden müssen... Ansonsten soll wie oben schon erwähnt, mit einem JDialog darauf hingewiesen werden...

Also noch einmal zusammengefasst... Probleme mit leeren String TextFeldern und unselektierten Comboboxen abfangen...

Hier erstmal der Code...
Die Betreffenden Zeilen sind zwischen 
210 - 244;
261 - 295;
315 - 350;

Vielen Dank schonmal im Voraus...

Gruß Popon



```
package Projekt;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;

public class Add_DeleteCocktails extends JDialog implements ActionListener

{
	/**
	 * author Dinler Deniz
	 */
	
	JButton addButtonRecipe = new JButton("Hinzufügen");
	JButton exitButton = new JButton("Beenden");
	
	JTextField tfCocktail = new JTextField("");
	JTextField tfGlassNr = new JTextField("");
	JButton addButtonCocktail = new JButton("Hinzufügen");
	JButton deleteButtonCocktail = new JButton("Löschen");
	
	JTextField tfIngredient = new JTextField("");
	JButton addButtonIngredient = new JButton("Hinzufügen");
	JButton deleteButtonIngredient = new JButton("Löschen");
	
	CBXCocktails cbxCocktails = new CBXCocktails();
	CBXIngredients cbxIngredients = new CBXIngredients();
	CBXAmounts cbxAmount = new CBXAmounts();
	JButton addButtonAllocation = new JButton("Hinzufügen");
	JButton deleteButtonAllocation = new JButton("Löschen");
	
	private static final long serialVersionUID = 1L;

	Add_DeleteCocktails() throws SQLException
	{
		super();
		this.setTitle("Cocktails hinzufügen");
		
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation(d.width/3, d.height/8);
		this.setSize(d.width/3, d.height/2);
		
		this.setLayout(new GridBagLayout());					
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.NONE;
		
		/**
		 * Create the Layout (GridBagLayout) and add the Components into the Layout
		 */
		
		// JLabel Cocktail
		c = makeC(0, 0, 2, 1);
		c.insets = new Insets(20, 20, 0, 10);
		this.add(new JLabel("Cocktail"), c);
		
		// LTextArea (with a titled Border) to write the recipe how to mix a cocktail
		c.gridwidth = GridBagConstraints.REMAINDER;
		JTextArea ta = new JTextArea();
		ta.setPreferredSize(new Dimension(300, 100));
		ta.setRows(21);
		TitledBorder title;
		title = BorderFactory.createTitledBorder("Cocktail-Rezept");
		title.setTitleJustification(TitledBorder.CENTER);
		ta.setBorder(title);
		c = makeC(2, 0, 4, 12);
		c.insets = new Insets(20, 10, 5, 20);
		this.add(ta, c);
		
		//Button to save the recipe as a textfile
		c = makeC(4, 12, 1, 1);
		c.insets = new Insets(5, 10, 20, 5);
		addButtonRecipe.addActionListener(this);
		this.add(addButtonRecipe, c);
		
		// Exit Button
		c.gridwidth = GridBagConstraints.REMAINDER;
		c = makeC(5, 12, 1, 1);
		c.insets = new Insets(5, 5, 20, 20);
		c.anchor = GridBagConstraints.SOUTHEAST;
		this.add(exitButton, c);
		this.exitButton.addActionListener(this);
		
		
		// JTextField to write the cocktail name
		c = makeC(0, 1, 2, 1);
		c.fill=GridBagConstraints.HORIZONTAL;
		c.insets = new Insets(5, 20, 5, 10);
		this.add(tfCocktail, c);
		
		// JLabel glass nr
		c = makeC(0, 2, 2, 1);
		c.insets = new Insets(0, 20, 0, 10);
		this.add(new JLabel("Glas"), c);
		
		// JTextField to write the glass nr
		c = makeC(0, 3, 2, 1);
		c.fill=GridBagConstraints.HORIZONTAL;
		c.insets = new Insets(5, 20, 5, 10);
		this.add(tfGlassNr, c);
		
		// JButton Add Cocktail into table cocktails
		c = makeC(0, 4, 1, 1);
		c.insets = new Insets(5, 20, 5, 5);
		addButtonCocktail.addActionListener(this);
		this.add(addButtonCocktail, c);
		
		// JButton Delete Cocktail from table cocktails
		c = makeC(1, 4, 1, 1);
		c.insets = new Insets(5, 5, 5, 10);
		deleteButtonCocktail.addActionListener(this);
		this.add(deleteButtonCocktail, c);
		
		// JLabel Ingredient
		c = makeC(0, 5, 2, 1);
		c.insets = new Insets(15, 20, 0, 10);
		this.add(new JLabel("Zutat"), c);
		
		// JTextField to write the ingredient name
		c = makeC(0, 6, 2, 1);
		c.fill=GridBagConstraints.HORIZONTAL;
		c.insets = new Insets(5, 20, 5, 10);		
		this.add(tfIngredient, c);
		
		// JButton Add Ingredient into the tabel ingredients
		c = makeC(0, 7, 1, 1);
		c.insets = new Insets(5, 20, 5, 5);
		addButtonIngredient.addActionListener(this);
		this.add(addButtonIngredient, c);
		
		// JButton Delete Ingredient from the table ingredients
		c = makeC(1, 7, 1, 1);
		c.insets = new Insets(5, 5, 5, 10);
		deleteButtonIngredient.addActionListener(this);
		this.add(deleteButtonIngredient, c);
		
		// JLabel Allocation
		c = makeC(0, 8, 2, 1);
		c.insets = new Insets(15, 20, 0, 10);
		this.add(new JLabel("Zuordnen"), c);
		
		//JComboBox list of cocktails 
		c = makeC(0, 9, 2, 1);
		c.fill=GridBagConstraints.HORIZONTAL;
		c.insets = new Insets(5, 20, 5, 10);		
		this.add(cbxCocktails, c);
		
		//JComboBox list of ingredients 
		c = makeC(0, 10, 2, 1);
		c.fill=GridBagConstraints.HORIZONTAL;
		c.insets = new Insets(5, 20, 5, 10);
		this.add(cbxIngredients, c);
		
		//JComboBox list of amounts 
		c = makeC(0, 11, 2, 1);
		c.fill=GridBagConstraints.HORIZONTAL;
		c.insets = new Insets(5, 20, 5, 10);
		this.add(cbxAmount, c);
		
		// JButton Add the contain of the cbxs into the table ingredient_allocation
		c = makeC(0, 12, 1, 1);
		c.insets = new Insets(5, 20, 20, 5);
		addButtonAllocation.addActionListener(this);
		this.add(addButtonAllocation, c);
		
		// JButton Delete the contain of the cbxs from the table ingredient_allocation
		c = makeC(1, 12, 1, 1);
		c.insets = new Insets(5, 5, 20, 10);
		deleteButtonAllocation.addActionListener(this);
		this.add(deleteButtonAllocation, c);
		
		this.setVisible(true);
		this.pack();
		this.toFront();
		this.setAlwaysOnTop(true);
		
	}	
	
	private GridBagConstraints makeC(int x, int y, int width, int height)
	{
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = x;
		gbc.gridy = y;
		gbc.gridwidth = width;
		gbc.gridheight = height;
		return gbc;
	}
	
	public void actionPerformed(ActionEvent ae)
	{
		Connection con = DatabaseConnection.getConnection();
		PreparedStatement ps = null;
		
		if (ae.getSource() == addButtonCocktail)
		{
			String INSERT_COCKTAIL = "INSERT INTO cocktails(cocktailID, cocktailName, glassNR, imageNR) VALUES (?, ?, ?, ?)";
			try
			{
				con.setAutoCommit(false);
				ps = con.prepareStatement(INSERT_COCKTAIL);
			    ps.setString(1, "0");
			    ps.setString(2, tfCocktail.getText());
			    ps.setString(3, tfGlassNr.getText());
			    ps.setString(4, null);
			    ps.executeUpdate();
			    con.commit();
			    ps.close();
			} catch (SQLException e)
			{
				e.printStackTrace();	
		    	
		    	JDialog incomplete = new JDialog();
				incomplete.setTitle("About Cocktail Pro");
				
				Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
				incomplete.setLocation(d.width/2, d.height/4);
				
				incomplete.setSize(300, 150);
				JLabel describeException = new JLabel();
				describeException.setText("<html>Fehler:  " + e.getMessage() + 
		    								"<br/><br/>Cocktailname und GlassNr müssen ausgefüllt werden</html>");
				
				incomplete.add(describeException);
				describeException.setHorizontalAlignment(SwingConstants.CENTER);
				incomplete.setVisible(true);
				incomplete.setAlwaysOnTop(true);
			}
		}
		if (ae.getSource() == deleteButtonCocktail)
		{
			String DELETE_COCKTAIL = "DELETE FROM cocktails WHERE cocktailname = ? ";
			try
			{
				con.setAutoCommit(false);
				ps = con.prepareStatement(DELETE_COCKTAIL);
			    ps.setString(1, tfCocktail.getText());
			    ps.executeUpdate();
			    con.commit();
			    ps.close();
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		if (ae.getSource() == addButtonIngredient)
		{
			String INSERT_INGREDIENT = "INSERT INTO ingredients(ingredientID, ingredientName) VALUES (?, ?)";
			try
			{
				con.setAutoCommit(false);
				ps = con.prepareStatement(INSERT_INGREDIENT);
			    ps.setString(1, "0");
			    ps.setString(2, tfIngredient.getText());
			    ps.executeUpdate();
			    con.commit();
			    ps.close();
			    
			} catch (SQLException e)
			{
					e.printStackTrace();

					JDialog incomplete = new JDialog();
					incomplete.setTitle("About Cocktail Pro");
					
					Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
					incomplete.setLocation(d.width/2, d.height/4);
					
					incomplete.setSize(300, 150);
					JLabel describeException = new JLabel();
					describeException.setText("<html>Fehler:  " + e.getMessage() + 
			    								"<br/><br/>Cocktailname und GlassNr müssen ausgefüllt werden</html>");
					
					incomplete.add(describeException);
					describeException.setHorizontalAlignment(SwingConstants.CENTER);
					incomplete.setVisible(true);
					incomplete.setAlwaysOnTop(true);
			}			
		}
		
		if (ae.getSource() == deleteButtonIngredient)
		{
			String DELETE_INGREDIENT = "DELETE FROM ingredients WHERE ingredientName = ? ";
			try
			{
				con.setAutoCommit(false);
				ps = con.prepareStatement(DELETE_INGREDIENT);
			    ps.setString(1, tfIngredient.getText());
			    ps.executeUpdate();
			    con.commit();
			    ps.close();
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		if (ae.getSource() == addButtonAllocation)
		{
			String INSERT_ALLOCATION = 	"INSERT INTO ingredient_allocation(allocationID, cocktailID, " +
										"ingredientID, amountID) VALUES (?, ?, ?, ?)";
			
			try
			{
				con.setAutoCommit(false);
				ps = con.prepareStatement(INSERT_ALLOCATION);
			    ps.setString(1, "0");
			    ps.setString(2, cbxCocktails.getSelectedItem().toString());
			    ps.setString(3, cbxIngredients.getSelectedItem().toString());
			    ps.setString(4, cbxAmount.getSelectedItem().toString());
			    
			    if 	((!cbxCocktails.getSelectedItem().toString().equals("Cocktails")) && 
			    	 (!cbxIngredients.getSelectedItem().toString().equals("Zutaten")) &&
			    	 (!cbxAmount.getSelectedItem().toString().equals("Menge")))
			    {
				    ps.executeUpdate();
				    con.commit();
				    ps.close();
			    }
			    else
			    {
			    	JDialog incomplete = new JDialog();
			    	JLabel describe_incomplete = new JLabel();
			    	describe_incomplete.setText("<html><br/><br/>" +
							"Die Auswahl ist nicht vollständig!" +
							"<br/><br/>" +
							"Alle Drop Down Menüs müssen selektiert werden");
			    	incomplete.setTitle("Unvollständig");
			    	incomplete.add(describe_incomplete);
			    }
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
			
		}
		
		if (ae.getSource() == deleteButtonAllocation)
		{
			String DELETE_INGREDIENT = "DELETE FROM ingredients WHERE ingredientName = ? ";
			try
			{
				con.setAutoCommit(false);
				ps = con.prepareStatement(DELETE_INGREDIENT);
			    ps.setString(1, tfIngredient.getText());
			    ps.executeUpdate();
			    con.commit();
			    ps.close();
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		if (ae.getSource() == exitButton)
		{
			this.dispose();
			this.setVisible(false);
		}	
	}
}
```


----------



## Michael... (31. Mrz 2010)

Du musst selbst prüfen, ob alle notwendigen Eingaben gemacht wurden.
z.B. jTextField.getText().length()>0


----------



## Popon (31. Mrz 2010)

Super...

Das man auf solche banelen Sachen net kommt ist schon komisch...

Danke Dir...


----------



## Popon (31. Mrz 2010)

Die leeren Textfelder habe ich jetzt schon implementiert..

Wie ist denn der Ansatz bei den Comboboxen?


----------



## Popon (31. Mrz 2010)

Problem gelöst...


----------

