Action Listener reagiert nicht auf Button druck mehr???

kleinfritzchen

Aktives Mitglied
Hallo,
ich hab hier was geschrieben bei dem eine ganze Zeit der Button addInventurPfad auf druck über den ActionEvent funktioniert hat aber etzt geht es nicht mehr. ich find den Fehler nicht!
kann mir emand mal einen Hinweis geben wo der Fehler liegt oder liegen könnte?

Java:
package Pack;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileNameExtensionFilter;



public class GUI_Inv extends JFrame {

	// Elemente
	JTabbedPane reiterPane = new JTabbedPane();
	JPanel eingabe, auswertung, optionen, fussZeile, leftPane, auswertLeftPane, addPane,
			lftPaneOptionen, pnlMitteArt2;

	//addPane
	JComboBox jBox;
	JTextField textFeld1, textFeld2,artikelPfad, inventurPfad;
	JButton btnSpeichern, btnLoeschen,btnDrucken, btnAddArtikelPfad, btnAddInventurPfad;
	JRadioButton rb1, rb2;
	ButtonGroup btnGruppe;

	JLabel label1, label2, labl3, label4;
	JLabel labArtikel, labTextFeld1, labTextFeld2, labArtInfo;

	//leftPane
	JTextArea infoText;
	 
	ScrollPane liste,auswertListe;
	
	 private static int MAXINV, MAXART;
	
	// Konstruktor
	public GUI_Inv(String titel) {
		super(titel);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		erstelleKomponente();
		addKomponente();
		pack();
	}

	private void erstelleKomponente() {
		reiterPane = new JTabbedPane(); //GrundFrame mit Reitern
		eingabe = new JPanel();
		eingabe.setLayout(new BorderLayout());
		
		addPane=new JPanel();
		addPane.setLayout(new GridLayout(0,2,5,5));
		addPane.setAlignmentX(LEFT_ALIGNMENT);
		labArtikel =new JLabel("Artikel Nr");
			addPane.add(labArtikel);
		jBox=new JComboBox();
		jBox.setPrototypeDisplayValue(textFeld1);
			addPane.add(jBox);
		labTextFeld1=new JLabel("\u00d8 1 [mm] ", JLabel.RIGHT);
			addPane.add(labTextFeld1);
		textFeld1=new JTextField(14);
			addPane.add(textFeld1);
		labTextFeld2 =new JLabel("\u00d8 2 [mm] ", JLabel.RIGHT);
			addPane.add(labTextFeld2);
		textFeld2=new JTextField(11);
			addPane.add(textFeld2);
			addPane.add(Box.createHorizontalGlue());
		btnSpeichern=new JButton("Speichern");
			addPane.add(btnSpeichern);
			rb1=new JRadioButton("Inventur-D");
			rb2=new JRadioButton("Artikel-D");
			btnGruppe=new ButtonGroup();
			rb1.setSelected(true);
			btnGruppe.add(rb1);
			btnGruppe.add(rb2);
		addPane.add(rb1);
		addPane.add(rb2);
		addPane.setMaximumSize(addPane.getPreferredSize());
		
		infoText=new JTextArea();
		infoText.setEditable(false);
		infoText.setLineWrap(true);
		infoText.setWrapStyleWord(true);
		infoText.setAlignmentX(LEFT_ALIGNMENT);
		infoText.setFocusTraversalKeysEnabled(false);
		
		leftPane=new JPanel();
		leftPane.setLayout(new BoxLayout(leftPane, BoxLayout.Y_AXIS));
		leftPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 4, 4));
		leftPane.add(addPane);
		leftPane.add(infoText);
		
		fussZeile =new JPanel();
		fussZeile.setLayout(new FlowLayout(FlowLayout.LEFT));
		
		auswertung =new JPanel();
		auswertung.setLayout(new BorderLayout());
		auswertListe=new ScrollPane();
		auswertLeftPane=new JPanel();
		btnLoeschen=new JButton("Löschen");
		btnDrucken=new JButton("Drucken...");
		auswertLeftPane.add(btnDrucken);
		
		
		liste=new ScrollPane();
		
		optionen=new JPanel();
		optionen.setLayout(new BorderLayout());
		optionen.setBorder(BorderFactory.createEmptyBorder(10, 5, 5, 10));
		Box boxLinks=Box.createVerticalBox();
		//boxLinks.add(BorderFactory.createEmptyBorder(0, 0, 0, 5));
		label1=new JLabel("Artikel Pfad");
		label1.setAlignmentX(RIGHT_ALIGNMENT);
		label1.setAlignmentY(CENTER_ALIGNMENT);

		label2=new JLabel("Inventur Pfad");
		label2.setAlignmentX(RIGHT_ALIGNMENT);
		label2.setAlignmentY(CENTER_ALIGNMENT);
		boxLinks.add(label1);
		
		boxLinks.add(Box.createVerticalStrut(15));
		boxLinks.add(label2);
		optionen.add(boxLinks, BorderLayout.WEST);
			Box boxMitte=Box.createVerticalBox();
//			pnlMitteArt2=new JPanel();
//			pnlMitteArt2.setLayout(new BoxLayout(pnlMitteArt2,BoxLayout.LINE_AXIS));
			artikelPfad=new JTextField(30);
			artikelPfad.setAlignmentX(LEFT_ALIGNMENT);
			artikelPfad.setMaximumSize(artikelPfad.getPreferredSize());
			inventurPfad=new JTextField(30);
			inventurPfad.setAlignmentX(LEFT_ALIGNMENT);
			inventurPfad.setMaximumSize(inventurPfad.getPreferredSize());
			boxMitte.add(artikelPfad);
			boxMitte.add(Box.createVerticalStrut(5));
			boxMitte.add(inventurPfad);
			optionen.add(boxMitte, BorderLayout.CENTER);
				Box boxRechts=Box.createVerticalBox();
				btnAddArtikelPfad=new JButton("...");
				btnAddArtikelPfad.addActionListener(new buttonHorcher());
				btnAddInventurPfad=new JButton("...");
				btnAddInventurPfad.addActionListener(new buttonHorcher());
				boxRechts.add(btnAddArtikelPfad);
				boxRechts.add(btnAddInventurPfad);
				btnAddArtikelPfad.setAlignmentX(LEFT_ALIGNMENT);
				btnAddInventurPfad.setAlignmentX(LEFT_ALIGNMENT);
				optionen.add(boxRechts, BorderLayout.LINE_END);
			
			
	}

	private void addKomponente() {
		getContentPane().add(reiterPane);
		reiterPane.addTab("Eingabe", eingabe);
		reiterPane.addTab("Auswertung", auswertung);
		reiterPane.addTab("Optionen", optionen);
		
		eingabe.add(leftPane, BorderLayout.WEST);
		eingabe.add( liste, BorderLayout.CENTER);
		eingabe.add(fussZeile, BorderLayout.LINE_END);
		
		auswertung.add(auswertListe, BorderLayout.CENTER);
		auswertung.add(auswertLeftPane, BorderLayout.WEST);
		
		leseDateien();
	}

	private void leseDateien() {
//		setOption("MAXART",(""+200));
//		setOption("MAXINV",""+1000);
		File fl=new File(getOption("ArtikelPfad"));
		if(!fl.canRead()){
			JOptionPane.showMessageDialog(optionen,
				    "Die Artikeldatei kann nicht gelesen werden, bitte eine gültige \"Artikel.txt\" einstellen",
				    "ungültige Datei",
				    JOptionPane.WARNING_MESSAGE);
		}
		if (! fl.canWrite()){
			JOptionPane.showMessageDialog(optionen,
				    "Die Artikeldatei kann nicht verändert werden wegen fehlender Schreibrechte!",
				    "Warnung keine ausreichenden Zugriffsrechte",
				    JOptionPane.WARNING_MESSAGE);
		}
		artikelPfad.setText(getOption("ArtikelPfad"));
		inventurPfad.setText(getOption("InventurPfad"));
		
		try {
			FileReader fr=new FileReader(fl.getAbsoluteFile());
			BufferedReader br=new BufferedReader(fr);
			String z=br.readLine();
			while ((z=br.readLine())!= null){
				// System.out.println(z);
				String[] zz=z.split(";");
				jBox.addItem(zz[0]);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e){
			System.out.println("Ein Ausgabefehller beim Initialisieren der Artikel Datei");
		}
		//JOptionPane invDialog=new JOptionPane();
		int ant=JOptionPane.showConfirmDialog(null,
									"Neue Inventur-Datei erstellen?\nBei Nein wird die zuletzt geöffnete Datei geladen.",
									"Inventur Datei", JOptionPane.YES_NO_OPTION	);
		if (ant==0){
			System.out.println("ja wurde gedrückt");
			File pfad=new File(getOption("InventurPfad"));
			JFileChooser flc=new JFileChooser(pfad);
			FileNameExtensionFilter filter=new FileNameExtensionFilter("Invenur Datei ","csv");
			flc.setFileFilter(filter);
			int returnVal =flc.showSaveDialog(eingabe);

			if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = flc.getSelectedFile();
    			System.out.println(flc.getSelectedFile().toString());
                //This is where a real application would save the file.
                setOption("InventurPfad",(file.toString()+".csv"));
                inventurPfad.setText(getOption("InventurPfad"));
                try {
        			FileWriter fr=new FileWriter(file+".csv");
        			fr.write("Art Nr.;Einheit;D 1;d 2;Meter;Datum");
        			fr.close();
        		} catch (IOException e) {
        			// TODO Auto-generated catch block
        			e.printStackTrace();
        		}
            } else {
                System.out.println("Abbruch durch benutzer");
            }
			
		}
		if (ant==1){
			System.out.println("nein wurde gedrückt");
		}
	}
	
	class buttonHorcher implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			if (arg0.getSource() == btnAddArtikelPfad){
				System.out.println("Artikelpfad gedrückt");
				 artikelPfadDialog();
				 jBox.removeAllItems();
				 leseDateien();
				
			if (arg0.getSource() == btnAddInventurPfad){
				System.out.println("Inventurpfad + gedrückt");
				inventurPfadDialog();
				leseDateien();
			}
		}
	}
	}
	
	public void artikelPfadDialog(){
		FileNameExtensionFilter filter = new FileNameExtensionFilter(
		        "Artikrl Datei", "txt", "csv");
		JFileChooser fc=new JFileChooser(getOption("ArtikelPfad"));
		fc.setFileFilter(filter);
		int returnVal =fc.showOpenDialog(optionen);
		File file=null;
	        if (returnVal == JFileChooser.APPROVE_OPTION) {
	             file = ( fc.getSelectedFile());
	            //This is where a real application would open the file.
	            
	            setOption("ArtikelPfad",file.getAbsoluteFile().toString());
	        } else {
	            artikelPfad.setText(getOption("ArtikelPfad"));
	        }
	        //return file;
	}
	
	public void inventurPfadDialog(){
		FileNameExtensionFilter filter = new FileNameExtensionFilter(
		        "Inventur Datei",  "csv");
		System.out.println("bin da");
		JFileChooser fc=new JFileChooser();
		fc.setFileFilter(filter); 
		int returnVal =fc.showOpenDialog(optionen);
		
	        if (returnVal == JFileChooser.APPROVE_OPTION) {
	            File file = fc.getSelectedFile();
	            //This is where a real application would open the file.
	            inventurPfad.setText( file.getAbsolutePath());
	            setOption("InventurPfad",inventurPfad.getText());
	        } else {
	            inventurPfad.setText(getOption("InventurPfad"));
	        }
	}
	
	/*
	 * Properties einstellungen sichern und lesen
	 */
	public static String getOption(String variable) {
		try {
			Properties props = new Properties();
			File ini = new File("Inventur.ini");
			props.load(new FileInputStream(ini));
			return (props.getProperty(variable));

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void setOption(String variable, String wert) {
		try {
			Properties props = new Properties();
			File ini = new File("Inventur.ini");
			props.load(new FileInputStream(ini));
			// System.out.println(props.getProperty("ARBEITER"));
			// Und jetzt die Werte?
			props.setProperty(variable, wert);

			// Und sichern
			props.store(new FileOutputStream(ini), variable);

			// System.out.println(props.getProperty("ARBEITER"));
		} catch (Exception e) {
			System.out.println("Wert-Schlüssel schreiben nicht möglich");
		}

	}

	public static void main(String[] args)
			throws UnsupportedLookAndFeelException {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		GUI_Inv fenster = new GUI_Inv("TITEL");
		// fenster.setSize(640, 400);
		fenster.setLocationRelativeTo(null);
		System.out.println("test");
		// setOption("ITHEM_1","1574027070356");
		
		fenster.setVisible(true);

	}
}

MfG Fritz
 
Java:
public void actionPerformed(ActionEvent arg0) 
{
            if (arg0.getSource() == btnAddArtikelPfad)
            {
                System.out.println("Artikelpfad gedrückt");
                 artikelPfadDialog();
                 jBox.removeAllItems();
                 leseDateien();
                
            if (arg0.getSource() == btnAddInventurPfad)
            {
                System.out.println("Inventurpfad + gedrückt");
                inventurPfadDialog();
                leseDateien();
            }
            }
}
Klammern falsch gesetzt 😉
Das zweite if wird im Ersten einegschlossen
 
Setz in den ActionListener mal ein sysout.
Schaut auf den ersten Blick ganz gut aus. (EDIT: Nö doch nicht, siehe oben 😛 )

EDIT 2:
Falls du Eclipse nutzt, dann einmal Strg + Shift + F drücken.
 
Zuletzt bearbeitet von einem Moderator:
Str+Shift*F war nicht so gut, hatte einige sachen extra der Übersichthalber anders Formatiert.

So als Tipp - wenn du etwas der Überischt halber einrücken willst, setz es in {}

Java:
        textFeld2=new JTextField(11);
        {
            addPane.add(textFeld2);
            addPane.add(Box.createHorizontalGlue());
        }
        btnSpeichern=new JButton("Speichern");
        {
            addPane.add(btnSpeichern);
            rb1=new JRadioButton("Inventur-D");
            rb2=new JRadioButton("Artikel-D");
            btnGruppe=new ButtonGroup();
            rb1.setSelected(true);
            btnGruppe.add(rb1);
            btnGruppe.add(rb2);
        }
        addPane.add(rb1);

Diese Klammern bewirken Javatechnisch nichts (ausser du hast Deklarationen drin) aber der Formatter rückt es dir ein.

Eine andere Idee (wenn ich deinen doch recht langen Code naschaue) ist, diesen Codeblöcke in Funktionen zusammenzufassen. (Ich sag das mal einfach so, ohne die konkreten Hintergründe zu kennen)
 

Zurück
Oben