Probleme mit JCheckBox

Status
Nicht offen für weitere Antworten.

landmesser

Mitglied
Hallo!

Ich habe ein Problem mit JCheckBox:
aus einer java-Application wird ein zweites Fenster mit JCheckboxen geöffnet, die aus einer Liste
abgeleitet werden.
Jetzt stecke ich irgendwie fest.
Ich möchte feststellen, spätestens wenn das zweiter Fenster geschlossen wird,
welche Boxen gedrückt worden sind und diese Liste zurückgeben.

Vielen Dank für irgendwelche Hilfe!!


Anbei ein Auszug der Quellen.

Code:
   ArrayList liste = new ArrayList();

    //Create a new internal frame.
    protected void createLSTFrame() {
        //Create and set up the window.
        if ( frame == null ) 
            frame = new JFrame ("Auswahl aus Liste");
        
        //Make sure we have nice window decorations.
        JFrame.setDefaultLookAndFeelDecorated(true);
        
        if (lstliste.size()<2) {
            JOptionPane.showMessageDialog(frame, (liste.size()<1? "Kein":"Nur ein") + " Wert in Liste vorhanden");
        } else {
            //Create and set up the content pane.
            JComponent newContentPane = new LISTENCheckBox (liste);
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(newContentPane);
            //Display the window.
            frame.setResizable(false);
            frame.setLocation(300,200);
            frame.setSize(200,lstliste.size()*30);
            //frame.pack();
            frame.setVisible(true);
        } 
    }


LISTENCheckBox.java :
---------------------

import java.util.ArrayList;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LISTENCheckBox extends JPanel
                           implements ItemListener {
    JCheckBox cb[];
    int cnt;
    
    public LSTCheckBox(ArrayList liste) {
        super(new BorderLayout());

        //Create the check boxes.
        cnt = liste.size();
        JCheckBox cb[] = new JCheckBox[cnt];

        setLayout(new GridLayout(cnt, 1));
        for (int i = 0; i < cnt; ++i) {
            cb[i] = new JCheckBox ( " " + liste.get(i) );
            add(cb[i]);
            cb[i].addItemListener(this);
        }
    }

    public void itemStateChanged(ItemEvent e) {
        for (int i = 0; i < cnt; ++i) {
            if (cb[i] != null) {
                if (e.getSource().equals(cb[i])) {
// hier geht es irgendwie weiter               
// wenn da nicht dieser NullpointerException waere...
                }    
            }    
        }
    }
}
 
cnt gibt die Anzahl der Elemente in dem Array an, aber der Arrayindex beginnt bei 0!
So geht das::
Code:
for (int i = 0; i < cb.length; ++i) {
            if (cb[i] != null) {
                if (e.getSource().equals(cb[i])) {
// hier geht es irgendwie weiter               
// wenn da nicht dieser NullpointerException waere...
                }   
            }   
        }

//Edit: Wir geben auch Antworten, wenn man nicht in Blau schreibt, wenn nicht sogar eher, wenn nicht
 
Vielen Dank erst mal an Alle!

Ich bitte mein Stil zu entschuldigen; ich bin java-newbie!

Ich habe durch Herumprobieren herausgefunden, von wo der NullpointerException herkommt:

es tritt auf jedes mal wenn ich auf cb bzw cb zugreife, z.B.
cb.length cb oder cb.getName()

Was mache ich hier falsch?

Übrigens: die for-scheife muss ja (int i = 0; i < cb.length; i++) heissen!

Danke!
 
Werden die Objekte korrekt initialisiert?

Code:
        for (int i = 0; i < cnt; i++) {
            cb[i] = new JCheckBox ( " " + liste.get(i) );
            add(cb[i]);

Normalerweise müsste er die for..ja ausführen. Geb sicherheitshalber mal in der for Schleife was aus (z.B. den Index), um zu schauen ob die Objekte überhaupt erzeugt werden.

Btw. wenn deine Klasse LISTENCheckBox heisst muss dein Konstruktor auch so heissen.
 
Ich habe noch ein println eingebaut:

Bei 3 Elementen in liste erhalte ich
i = 0 null
i = 1 null
i = 2 null

<LSTCheckBox.java>

Code:
import java.util.ArrayList;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LSTCheckBox extends JPanel
                         implements ItemListener {
    JCheckBox cb[];
    int cnt;
    
    public LSTCheckBox(ArrayList liste) {
        super(new BorderLayout());

        //Create the check boxes.
        cnt = liste.size();
        JCheckBox cb[] = new JCheckBox[cnt];

        setLayout(new GridLayout(cnt, 1));
        for (int i = 0; i < cnt; i++) {
            cb[i] = new JCheckBox ( " " + liste.get(i));
            add(cb[i]);
            cb[i].addItemListener(this);
System.out.println("i=" + i + " " + cb[i].getName());
            
        }
    }

    public void itemStateChanged(ItemEvent e) {
        for (int i = 0; i < cb.length; i++) {   // !!! hier tritt schon ein NullPointerExcetion auf!!
            if (e.getSource().equals(cb[i])) {
                // bla bla bla....
            }    
        }
    }
}

Sobald ich auf ein Checkbox klicke, erhalte ich ein java.lang.NullPointerException at LSTCheckBox.java line:29

In dieser Zeile steht:
for (int i = 0; i < cb.length; i++) { // !!! hier ...

Edit by Dotlens: Code Tags eingefügt
 
landmesser hat gesagt.:
Code:
public class LISTENCheckBox extends JPanel
                           implements ItemListener {
    JCheckBox cb[];
    int cnt;
    
    public LSTCheckBox(ArrayList liste) {
        super(new BorderLayout());

        //Create the check boxes.
        cnt = liste.size();
        cb = new JCheckBox[cnt]; //Hier ist eine Änderung!

Probiers mal so, kann sonst sein das du Probleme mit dem Sichtbarkeitsbereich der Variablen hast.
 
Folgendes Problem habe ich noch:

In der Klasse LSTCheckBox wird ArrayList liste als Parameter übergeben.

In itemStateChanged ist liste jedoch nicht mehr sichtbar.

Sobald ein JCheckBox geändert wird, soll über den entsprechenden Index das Element von liste bearbeitet werden.

Code:
import java.awt.event.*; 
import javax.swing.*; 

public class LSTCheckBox extends JPanel 
                         implements ItemListener { 
    JCheckBox cb[]; 
    int cnt; 
    
    public LSTCheckBox(ArrayList liste) { 
        super(new BorderLayout()); 

        //Create the check boxes. 
        cnt = liste.size(); 
        cb[] = new JCheckBox[cnt]; 

        setLayout(new GridLayout(cnt, 1)); 
        for (int i = 0; i < cnt; i++) { 
            cb[i] = new JCheckBox ( " " + liste.get(i)); 
            add(cb[i]); 
            cb[i].addItemListener(this); 
        } 
    } 

    public void itemStateChanged(ItemEvent e) { 
        for (int i = 0; i < cb.length; i++) {
            if (e.getSource().equals(cb[i])) { 

[color=red]// So, hier möchte ich über den geänderten JCheckBox die
// oben übergebene ArrayListe liste bearbeiten,
// doch ich kann liste von hier nicht ansprechen[/color]

            }    
        } 
    } 
}
 
Anonymous hat gesagt.:
Code:
    JCheckBox cb[]; 
....
        cb[] = new JCheckBox[cnt];

Es heisst:

Code:
JCheckBox cb[];
....
cb = new JCheckBox[cnt];

Siehe meinen Code oben.

Zum andern Problem:

Warum machst du es nicht so:

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class LSTCheckBox extends JPanel
                         implements ItemListener {
	JCheckBox cb[];
   int cnt;
   ArrayList al;
   
   public LSTCheckBox(ArrayList liste) {
   	super(new BorderLayout());
	   al = liste;
      cnt = liste.size();
      cb = new JCheckBox[cnt];

      setLayout(new GridLayout(cnt, 1));
      for (int i = 0; i < cnt; i++) {
      	cb[i] = new JCheckBox ( " " + liste.get(i));
         add(cb[i]);
         cb[i].addItemListener(this);
      }
	}

   public void itemStateChanged(ItemEvent e) {
   	for (int i = 0; i < cb.length; i++) {
      	if (e.getSource().equals(cb[i])) {
				/* Mach was du willst */
         }   
   	}
	}
}

Ist nur ne Idee, keine Ahnung obs läuft, bzw. ob es das ist was du willst , aber eigentlich müsstest du so Zugriff auf die Liste haben.
 
Ja, so klappt es. Ich kann innerhalb von itemStateChanged auf liste zugreifen.

Doch wie gebe ich liste wieder an die aufrufende Application mit den veränderten Elementen zurück?
 
An welche Applikation ? Innerhalb von LSTCheckBox kannst du auf die Liste ja zugreifen.
Wenn du von außerhalb drauf zugreifen willst dann schreib dir eine entsprechende get-Methode, die einfach die Liste zurückgibt.
 
Ich hole mal etwas weiter aus, um zu zeigen was ich eigenlich vor habe:

1. lst und marked sind arrays

Code:
    String[] lst = new String[LSTMAX];
    boolean[] marked = new boolean[LSTMAX];
    int anzlst = 0;

2. createLSTFrame() wird in der main-Klasse über ein Button aufgerufen und soll
ein zweites Fenster öffnen (aber nur wenn noch nicht vorhanden)

Code:
    //Create a new internal frame.
    protected void createLSTFrame() {
        //Create and set up the window.
        if ( frame == null ) 
            frame = new JFrame ("LST-Auswahl");
        
        //Make sure we have nice window decorations.
        JFrame.setDefaultLookAndFeelDecorated(true);

        if (anzlst < 2) {
            JOptionPane.showMessageDialog(frame, (anzlst<1? "Kein":"Nur ein") + " Lagestatus in EDBS-Auftrag vorhanden");
        } else {
            //Create and set up the content pane.
            JComponent newContentPane = new LSTCheckBox ( lst, marked, anzlst );
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(newContentPane);
            //Display the window.
            frame.setResizable(false);
            frame.setLocation(300,200);
            frame.setSize(200,anzlst*30);
            //frame.pack();
            frame.setVisible(true);
        } 
    }

3. Das zweite Fenster (Klasse LSTCheckBox) besteht aus JCheckBox, die Beschriftung,
ActiveState Anzahl wird dynamisch aus lst und marked abgeleitet, z.B.:

|----------------|
| [ ] Auswahl1 |
| [ ] Auswahl2 |
| [ ] Auswahl3 |
| [ ] Auswahl4 |
|----------------|

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LSTCheckBox extends JPanel
                         implements ItemListener {
    JCheckBox cb[];
    boolean[] mark = new boolean[10];
    
    public LSTCheckBox (String[] lst, boolean[] marked, int cnt ) {
        super(new BorderLayout());

        System.arraycopy(marked, 0, mark, 0, cnt);
        
        //Create the check boxes.
        cb = new JCheckBox[cnt]; 

        setLayout(new GridLayout(cnt, 1));
        for (int i = 0; i < cnt; i++) {
            cb[i] = new JCheckBox ( " " + lst[i]);
            add(cb[i]);
            cb[i].addItemListener(this);
            cb[i].setSelected(true);
        }
        System.arraycopy(mark, 0, marked, 0, cnt);

    }

    public void itemStateChanged(ItemEvent e) {
        for (int i = 0; i < cb.length; i++) {
            if (e.getSource().equals(cb[i])) {
                if (e.getStateChange() == ItemEvent.SELECTED)
                    mark[i] = true;
                else
                    mark[i] = false;

            }
        }
    }
}

3. So, jetzt kommt das Problem wo mein Verstand aussetzt:
Ich wähle ein oder mehrere Eintrage aus

|----------------|
| [X] Auswahl1 |
| [ ] Auswahl2 |
| [X] Auswahl3 |
| [ ] Auswahl4 |
|----------------|

4. Ich möchte irgendwie, das über den JCheckBox das Array marked aktualisiert wird und an
das main zurückgegeben wird, damit ich mit der Auswahl marked die weitere Bearbeitung
durchführen kann.

5. Danke!
 
Anbei der gesamte Code:

Für sonstige Verbesserungsvorschläge wäre ich natürlich dankbar!!

Ich muß noch dazu sagen, dies ist mein erstes Java-Programm (ich habe bisher nur in FORTRAN bzw. C programmiert).

Mir fällt es sehr schwer, mich in java einzuarbeiten. Bei mir entsteht das Programm Schritt für Schritt durch testen, nachlesen, googlen, testen, nachlesen, googlen, ...

Inhalt von user.ini muß noch angepaßt werden:
DATENVERZEICHNIS=c:\daten\......

Beispiel einer *.edb-Datei:

EDBS02860000AKND000000 0000ULQA00000001anschlp B 040514040514
EDBS02300000BSPE000001000000ULPUNN 000100013452576710713050059021407 000 FB BROCKHAGEN B00000002101345263468057679641801 177345263460057679645301 003AP00025 0000000000019AP KARTE 0000
EDBS02750000BSPE000008000000ULPUNN 000100013452576810002440059021409 000 ALTBST HALLE B00000003001345279653057680071703 962PP06001 101345279657057680071201 982AP00009 177345279648057680074501 003AP00025 0000000000019AP (1) KARTE 0000
EDBS00240000AEND000000000000




EdbsHusky.java:

Code:
// EDBS2HUSKY
// EdbsHusky.java
// 
// 10.02.2005 kn
//
//**********************************************************************************

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;


public class EdbsHusky extends JFrame
                       implements ActionListener {
    static private final String newline = "\n";
    JButton openButton, outpButton, testButton, lstButton, saveButton;
    static JFrame frame;
    JTextArea log;
    JFileChooser fc;
    File iFile, oFile;
    final int LADEN     = 1;
    final int TESTEN    = 2;
    final int SCHREIBEN = 3;
    final int LSTMAX    = 10;
    final int NBZ = 0, PAT = 1, PNR = 2, PRZ = 3, PST = 4, ZST = 5, AKU = 6, SAK = 7, VAT = 8, BEV = 9, ENT = 10, UNT = 11, KEB = 12;
    final int LST = 0, LKR = 1, LKH = 2, LGA = 3, LGW = 4, LZK = 5, LBJ = 6, LAH = 7;

    
    class Punkt {
        int port;       // JTextArea oder Datei
        String kz;
        String pnr;
        String lst;
        String y;
        String x;
        String ozeile;  // die fertige Ausgabezeile
    }    
    Punkt opunkt = new Punkt();

    String[] lst = new String[LSTMAX];
    boolean[] marked = new boolean[LSTMAX];
    int anzlst = 0;
    
    
    public EdbsHusky() {
        super("EdbsHusky V0.1(kn) : EDBS-Daten in Husky-Koordinatendatei konvertieren");

        //Create the log first, because the action listeners
        //need to refer to it.
        log = new JTextArea(20,80);
        log.setMargin(new Insets(5,5,5,5));
        log.setEditable(false);
        JScrollPane logScrollPane = new JScrollPane(log);

        //Create the open button.  We use the image from the JLF
        //Graphics Repository (but we extracted it from the jar).
        openButton = new JButton("EDBS-Datei öffnen...", createImageIcon("Open.gif"));
        openButton.addActionListener(this);

        //LST-Button
        lstButton = new JButton("LST-Auswahl");
        lstButton.addActionListener(this);
        
        //Test-Button
        testButton = new JButton("Testen...");
        testButton.addActionListener(this);

        //Speichern-Button
        saveButton = new JButton("Speichern", createImageIcon("Save.gif"));
        saveButton.addActionListener(this);
        
        //For layout purposes, put the buttons in a separate panel
        JPanel buttonPanel = new JPanel(); //use FlowLayout
        buttonPanel.add(openButton);
        buttonPanel.add(lstButton);
        buttonPanel.add(testButton);
        buttonPanel.add(saveButton);

        //Add the buttons and the log to this panel.
        getContentPane().add(buttonPanel, BorderLayout.PAGE_START);
        getContentPane().add(logScrollPane, BorderLayout.PAGE_END);
    }

    public void actionPerformed(ActionEvent e) {
        //Handle open button action.
        if (e.getSource() == openButton) {
            // Title und Filter setzen
            fc = new JFileChooser();
            fc.setDialogTitle("EDBS-Datei auswählen");
            fc.setCurrentDirectory(new File("c:\\arbeiten\\develop\\java\\edbshusky\\PUDAT"));
            fc.setFileFilter( new FileFilter() {
                public boolean accept( File f ) {
                    return f.isDirectory() || f.getName().toLowerCase().endsWith(".edb");
                }
                public String getDescription() {
                    return "EDBS-Dateien";
                }
            });
            
            int returnVal = fc.showOpenDialog(EdbsHusky.this);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                iFile = fc.getSelectedFile();
                //This is where a real application would open the file.
                log.append("EDBS-Datei     : " + iFile.getPath() + "." + newline);
                log.append("Lade..." + newline);
                opunkt.port = LADEN;
                leseEdbs();
                log.append("...fertig!!" + newline);

            } else {
                log.append("Auswahl: Abbruch durch Anwender." + newline);
            }
            log.setCaretPosition(log.getDocument().getLength());

        //Handle lst button action.
        } else if (e.getSource() == lstButton) {
            log.append("LST-Auswahl starten..." + newline);
            createLSTFrame(); 
            log.append("...fertig!!" + newline);

        //Handle test button action.
        } else if (e.getSource() == testButton) {
            log.append("Starten..." + newline);
            
        for (int i = 0; i < anzlst; i++)
            System.out.println ("LST=" + lst[i] + " " + (marked[i]?"true":"false"));
            
            
            opunkt.port = TESTEN;
            log.setText("");
            leseEdbs();
            log.append("...fertig!!" + newline);

        //Handle save button action.
        } else if (e.getSource() == saveButton) {
            // Title und Filter setzen
            fc = new JFileChooser();
            fc.setDialogTitle("HUSKY-Koordinatendatei auswählen");
            fc.setCurrentDirectory( new File( holeZielOrdner()));
            fc.setFileFilter( new FileFilter() {
                public boolean accept( File f ) {
                    return f.isDirectory() || f.getName().toLowerCase().endsWith(".stp");
                }
                public String getDescription() {
                    return "HUSKY-Koordinatendatei (*.stp)";
                }
            });

            int returnVal = fc.showSaveDialog(EdbsHusky.this);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                oFile = fc.getSelectedFile();
                //This is where a real application would save the file.
                log.append("Ausgabe-Datei: " + oFile.getPath() + "." + newline);
                
                log.append("Speichern starten..." + newline);
                opunkt.port = SCHREIBEN;
                leseEdbs();
                log.append("...fertig!!");

            } else {
                log.append("Speichern: Abbruch durch Anwender." + newline);
            }
            log.setCaretPosition(log.getDocument().getLength());
        }
    }

    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = EdbsHusky.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
   
    
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {

        //Make sure we have nice window decorations.
        JFrame.setDefaultLookAndFeelDecorated(true);
        JDialog.setDefaultLookAndFeelDecorated(true);

        //Create and set up the window.

        EdbsHusky frame = new EdbsHusky();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    
    //Create a new internal frame.
    protected void createLSTFrame() {
        //Create and set up the window.
        if ( frame == null ) 
            frame = new JFrame ("LST-Auswahl");
        
        //Make sure we have nice window decorations.
        JFrame.setDefaultLookAndFeelDecorated(true);

        if (anzlst < 2) {
            JOptionPane.showMessageDialog(frame, (anzlst<1? "Kein":"Nur ein") + " Lagestatus in EDBS-Auftrag vorhanden");
        } else {
            //Create and set up the content pane.
            JComponent newContentPane = new LSTCheckBox ( lst, marked, anzlst );
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(newContentPane);
            //Display the window.
            frame.setResizable(false);
            frame.setLocation(300,200);
            frame.setSize(200,anzlst*30);
            //frame.pack();
            frame.setVisible(true);
        } 
        

    }
    
    
    private String holeZielOrdner() { 
        BufferedReader in = null;
        String sDatenPath = "", buff;
        
        try {
            String IniFile = "c:\\Programme\\Geofeld\\Trans\\CONFIG\\" + System.getProperty("user.name") + ".ini";
            in = new BufferedReader(new FileReader(IniFile));
            while ((buff = in.readLine()) != null) {
                if (buff.indexOf("DATENVERZEICHNIS") >= 0 ) {
                    sDatenPath = buff.substring(buff.lastIndexOf('=')+1);
                    sDatenPath = sDatenPath.replace('\'',' ').trim();
                    log.append("DatenPath        : " + sDatenPath + newline);
                    return sDatenPath;
                }
            }    
        } catch (IOException e) {
            // catch io errors from FileInputStream or readLine()
            log.append("Fehler: " + e.getMessage() + newline);
        } finally {
           // if the file opened okay, make sure we close it
            if (in != null) {
                try {
                    in.close();
                } catch (IOException ioe) {
                }
            }
        }
        return sDatenPath;
    }    

    
    private void outp (int typ) {
        switch (typ) {
            case  9 : 
                break;
            case 21 : 
                opunkt.ozeile = "21" 
                    + lstkz(opunkt.lst) 
                    + "     " 
                    + opunkt.pnr.substring(2,4) + opunkt.pnr.substring(6)
                    + opunkt.lst 
                    + "     000000"
                    + opunkt.y.substring(0,7) + "." + opunkt.y.substring(7)
                    + opunkt.x.substring(0,7) + "." + opunkt.x.substring(7)
                    + "-999.999";
                break;
        }
        switch (opunkt.port) {
            case TESTEN :
                log.append (opunkt.ozeile + newline);
                break;        
            case SCHREIBEN :
                break;
        }        
    }    

    public void addlst ( String newlst ) {
        int j = -1;
        for (int i = 0; i < anzlst; i++) {
            if (lst[i].equals(newlst))
                j = i;
       	}
        if (j < 0) {
            lst[anzlst] = newlst;
            marked[anzlst] = false;
            anzlst++;
        }    
    }    
    
    
    public String lstkz (String lst) {
        String kz;
        if      (lst.equals("001")) kz = "01";
        else if (lst.equals("101")) kz = "02";
        else if (lst.equals("201")) kz = "03"; 
        else if (lst.equals("K01")) kz = "04"; 
        else if (lst.equals("073")) kz = "05"; 
        else if (lst.equals("173")) kz = "06"; 
        else if (lst.equals("273")) kz = "07"; 
        else if (lst.equals("K73")) kz = "08"; 
        else if (lst.equals("T73")) kz = "20"; 
        else if (lst.equals("077")) kz = "09"; 
        else if (lst.equals("177")) kz = "10"; 
        else if (lst.equals("277")) kz = "11"; 
        else if (lst.equals("K77")) kz = "12"; 
        else if (lst.equals("086")) kz = "13"; 
        else if (lst.equals("186")) kz = "14"; 
        else if (lst.equals("286")) kz = "15"; 
        else if (lst.equals("K86")) kz = "16"; 
        else if (lst.equals("489")) kz = "89";             
        else kz = "??";
        return kz;
    }    
    
    public void leseEdbs () {
        BufferedReader in = null;
        String str;
        int satzl;
 
        Edbs Esatz = new Edbs ();

        try {
            in = new BufferedReader(new FileReader(iFile));

            while ((str = in.readLine()) != null) {

                satzl = Integer.parseInt(str.substring(5,8));
                while (str.length()<satzl+12) {
                    str = str + in.readLine();
                }

                // Kopf ausgeben
                if (str.indexOf("AKND") > 0 ) {
                    opunkt.ozeile = "09  PUDAT-KOORDINATEN AUS AUFTRAG " + str.substring(40,54).trim() + " VOM " + str.substring(213,219);
                    outp (9);
                    
                } else if (str.substring(8,12).equals("0000") && !str.substring(12,13).equals("A")) {

                    Esatz.ReadEdbs (str);
                    
                    //******  ULPU0000 Punktkennzeichen, Verwaltung  ******
                    opunkt.pnr = Esatz.dlpu0[NBZ][0] + Esatz.dlpu0[PAT][0] + Esatz.dlpu0[PNR][0];

                    //******  ULPU2000 Lage  ******

                    for (int j = 0; j<Esatz.a2; j++) {
                        
                        if (opunkt.port == LADEN) {
                            addlst (Esatz.dlpu2[LST][j]);
                        }	
                        opunkt.lst = Esatz.dlpu2[LST][j];
                        opunkt.y   = Esatz.dlpu2[LKR][j];
                        opunkt.x   = Esatz.dlpu2[LKH][j];
                        outp (21);
                    }
                }    
            }  /******* Datei-Schleife Ende ******/

        } catch (IOException e) {
            // catch io errors from FileInputStream or readLine()
            System.out.println("Uh oh, got an IOException error!" + e.getMessage());

        } finally {
           // if the file opened okay, make sure we close it
            if (in != null) {
                try {
                    in.close();
                } catch (IOException ioe) {
                }
            }
        }
    }
       
    
    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}


LSTCheckBox.java:

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LSTCheckBox extends JPanel
                         implements ItemListener {
    JCheckBox cb[];
    boolean[] mark = new boolean[10];
    
    public LSTCheckBox (String[] lst, boolean[] marked, int cnt ) {
    	super(new BorderLayout());

        System.arraycopy(marked, 0, mark, 0, cnt);
        
        //Create the check boxes.
        cb = new JCheckBox[cnt]; 

        setLayout(new GridLayout(cnt, 1));
        for (int i = 0; i < cnt; i++) {
            cb[i] = new JCheckBox ( " " + lst[i]);
            add(cb[i]);
            cb[i].addItemListener(this);
            cb[i].setSelected(true);
        }
        System.arraycopy(mark, 0, marked, 0, cnt);

    }

    public void itemStateChanged(ItemEvent e) {
        for (int i = 0; i < cb.length; i++) {
            if (e.getSource().equals(cb[i])) {
                if (e.getStateChange() == ItemEvent.SELECTED)
                    mark[i] = true;
                else
                    mark[i] = false;
//System.out.println("CheckBox: mark "+i+"="+(mark[i]?"true":"false"));

            }
        }
    }
}

Edbs.java:

Code:
public class Edbs {

    int a0, a1, a2, a3, a4, a5, a6, alf;

    String[][] dlpu0 = new String[13][10];
    String[][] dlpu1 = new String[1] [10];
    String[][] dlpu2 = new String[8] [10];
    String[][] dlpu3 = new String[8] [10];
    String[][] dlpu4 = new String[8] [10];
    String[][] dlpu5 = new String[2] [10];
    String[][] dlpu6 = new String[14][10];

    public void ReadEdbs ( String str ) {
        int pos;

        //Element-Laenge
        final int NU0 = 13, NU1 = 1, NU2 = 8, NU3 = 8, NU4 = 8, NU5 = 2, NU6 = 14;
        int[] lu0 = {8,1,5,1,1,7,2,2,3,4,15,15,1};
        final int NBZ = 0, PAT = 1, PNR = 2, PRZ = 3, PST = 4, ZST = 5, AKU = 6, SAK = 7, VAT = 8, BEV = 9, ENT = 10, UNT = 11, KEB = 12;
        int   lu1 = 16;
        int[] lu2 = {3,10,10,1,8,1,3,9};
        final int LST = 0, LKR = 1, LKH = 2, LGA = 3, LGW = 4, LZK = 5, LBJ = 6, LAH = 7;
        int[] lu3 = {3,8,5,1,4,1,3,9};
        int[] lu4 = {3,10,5,1,4,1,3,9};
        int[] lu5 = {1,18};
        int[] lu6 = {1,1,1,1,1,1,1,1,1,1,5,2,1,1};


        if (str.substring(8,12).equals("0000") && !str.substring(12,13).equals("A")) {

            //******  ULPU0000 Punktkennzeichen, Verwaltung  ******

            pos = 40;
            a0 = Integer.parseInt(str.substring(pos,pos+4));
            pos += 4;

            for (int j = 0; j<a0; j++) {
                for (int i = 0; i<NU0; i++) {
                    dlpu0[i][j] = str.substring(pos,pos+lu0[i]);
                    pos += lu0[i];
                }
            }

            //******  ULPU1000 Messeinheit  ******

            a1 = Integer.parseInt(str.substring(pos,pos+4));
            pos += 4;

            for (int j = 0; j<a1; j++) {
                for (int i = 0; i<NU1; i++) {
                    dlpu1[i][j] = str.substring(pos,pos+lu1);
                    pos += lu1;
                }
            }

            //******  ULPU2000 Lage  ******

            a2 = Integer.parseInt(str.substring(pos,pos+4));
            pos += 4;

            for (int j = 0; j<a2; j++) {
                for (int i = 0; i<NU2; i++) {
                    dlpu2[i][j] = str.substring(pos,pos+lu2[i]);
                    pos += lu2[i];
                }
            }

            //******  ULPU3000 Hoehe  ******

            a3 = Integer.parseInt(str.substring(pos,pos+4));
            pos += 4;

            for (int j = 0; j<a3; j++) {
                for (int i = 0; i<NU3; i++) {
                    dlpu3[i][j] = str.substring(pos,pos+lu3[i]);
                    pos += lu3[i];
                }
            }

            //******  ULPU4000 Schwere  ******

            a4 = Integer.parseInt(str.substring(pos,pos+4));
            pos += 4;

            for (int j = 0; j<a4; j++) {
                for (int i = 0; i<NU4; i++) {
                    dlpu4[i][j] = str.substring(pos,pos+lu4[i]);
                    pos += lu4[i];
                }
            }

            //******  ULPU5000 Bemerkung zum Punkt  ******

            a5 = Integer.parseInt(str.substring(pos,pos+4));
            pos += 4;

            for (int j = 1; j<=a5; j++) {
                for (int i = 0; i<NU5; i++) {
                    dlpu5[i][j] = str.substring(pos,pos+lu5[i]);
                    pos += lu5[i];
                }
            }

            //******  ULP6ALF ?? for what ever  ???  ******

            alf = Integer.parseInt(str.substring(pos,pos+4));
            pos += 4;

            if (pos < str.length()) {

            //******  ULPU6000 Festpunktverwaltung  ******

                a6 = Integer.parseInt(str.substring(pos,pos+4));
                pos += 4;

                for (int j = 0; j<a6; j++) {
                    for (int i = 0; i<NU6; i++) {
                        dlpu6[i][j] = str.substring(pos,pos+lu6[i]);
                        pos += lu6[i];
                    }
                }
            }
        }
    }
}
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben