# JTable Riesenproblem



## redman (3. Mai 2004)

hi,
habe ein table im scrollpane.

bekomme immer wenn ich eine neue zeile oder spalte adden will immer eine ArrayIndexoutofBounds:-1 Exception.

Woran liegt das??

mfg


----------



## redman (3. Mai 2004)

keiner ne ahnung??

hier ein auszug vom code:

```
model = new DefaultTableModel();
theTab =new JTable();
...
theTab.setModel(model);
model.addTableModelListener(this);
model.setRowCount(0);
model.addcolumn("spalte1");  -> hier taucht dann der fehler auf, und die Tabelle wird nicht aufgebaut!
```
mfg


----------



## L-ectron-X (3. Mai 2004)

Bis auf den Schreibfehler...


			
				redman hat gesagt.:
			
		

> ```
> model.addcolumn("spalte1");
> ```


...will mir erst mal nichts auffallen.

```
model.addColumn("Spalte1");
```
wäre legitim.


----------



## redman (3. Mai 2004)

Nee...
Im Prog ists richtig geschrieben.

Ich dreh gleich am Rad.
Ich finde da auch kein Fehler!

Ich habe viele Prog.Teile von anderen Appl übernommen.
Das Kuriose ist ja, dass es zuerst funktioniert dann nicht mehr.

So was ätzendes!


mfg


----------



## Roar (3. Mai 2004)

hmm kanns sein dass der fehler nicht bei dir im programm auftritt sondern im DefaultTableModel? guck mal in den Sacktrace.. ich glaub das hatten wir schonmal  ???:L 
btw: das gehört wohl eher in Swing & AWT


----------



## redman (3. Mai 2004)

Aha,
wo kann ich mir den Stacktrace anschauen??

Möglich wäre es.
Habe mal zum Testen ein 2tes Project geöffnet und kompiliert, und da läuft es einwandfrei.

Ich versteh das nicht!

mfg


----------



## Roar (3. Mai 2004)

stacktrace ist das ding was die konsole bei nem runtime error ausspuckt. dat zeigt dir die rückverfolgung vonner exception an. meistens sind nur die höchstens die ersten 4-5 elemente wirklich interessant.

achja: wa shast du denn beim 2. prog anders gemacht?


----------



## redman (3. Mai 2004)

das 2.prog war ein altes projekt von mir.

das kuriose ist ja,dass da nichts anders ist!!!!

mfg


----------



## redman (3. Mai 2004)

Hier der gesamte Code!


```
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;


// MMediaWorx - Cetintas
//
// *******************************************************************************
// 
// 25.03.2004
// 
// (c) Vatan Cetintas 2004
// 
// 
// *******************************************************************************
// 



public class RMAForm_ extends javax.swing.JApplet
	implements ActionListener, TableModelListener
{

	private String getArtikel(String artnr)
	{
		for (int y=0; y<=upper; y++)
			if (artikel[y][0] == artnr)	return artikel[y][1];
		
		return "";
	}
	
	
	public void tableChanged(TableModelEvent e) 
	{
		int row = e.getFirstRow();
	    int column = e.getColumn();
	    Object data = model.getValueAt(row, column);
	    System.out.println("tc1");
	    if (column ==1)
	    {
	    	if (model.getValueAt(row, column)!= "")
	    	{        	System.out.println("tc1");

	    		System.out.println(model.getValueAt(row, column));
	    		model.setValueAt(new Object[] {getArtikel(model.getValueAt(row, column).toString()),""} ,row,2 );
	    	}
	    }//if
	}
	
	public void actionPerformed(ActionEvent ae) 
	{
		if (ae.getSource()==cmdNeu )
		{
			model.addRow(new Object[] {"-1", "", "", "",""} );
		}  //cmdNeu
		
		int rows = model.getRowCount();
		
		if (ae.getSource()==cmdRMA )
		{ 
		   //System.out.println("SAVE " + rows);
			String data = "";
			for(int row=0; row<rows; row++)
			{
				SimpleDateFormat datum = new SimpleDateFormat("yyyy-MM-dd");
				datum.setTimeZone(TimeZone.getDefault());
				SimpleDateFormat rma_ = new SimpleDateFormat("MM-dd");
				rma_.setTimeZone(TimeZone.getDefault());

				String str = model.getValueAt(row, 1).toString();
				if (str != "")
				{ //alles modifizierte update
					try {
						//System.out.println(dat.Str2strSQL(model.getValueAt(row, 9).toString()));
						 data += URLEncoder.encode("key", "UTF-8") + "=" + 
								URLEncoder.encode(model.getValueAt(row, 1).toString() + "|" + 
										model.getValueAt(row, 2).toString() + "|" +
										model.getValueAt(row, 3).toString() + "|" +
										model.getValueAt(row, 4).toString() + "|" , "UTF-8");
					} catch (UnsupportedEncodingException e1) {
						e1.printStackTrace();
					}
					 data += "&";
				}
				data = data.substring(0, data.length()-1);
				
			}//for
			
			//Daten übermitteln
			try {
		        // Construct data
		        
		        // Send data
		        URL url = new URL( getCodeBase() + "rma.php");
		        URLConnection conn = url.openConnection();
		        conn.setDoOutput(true);
		        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
		        wr.write(data);
		        wr.flush();
		    
		        // Get the response
		        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
		        String line;
		        while ((line = rd.readLine()) != null) {
		            // Process line...
		        	System.out.println(line);
		        }
		        wr.close();
		        rd.close();
		    } catch (Exception e) { 
		    	System.out.println("err " + e);
		    }
			//Daten übermittelt; RMA erzeugen lassen von php-skript
			//Applet schliessen+sperren?

		} //if cmdRMA

	}
	
private void initConnections() throws java.lang.Exception {
	
	try {
		   // Create a URLConnection object for a URL
		   URL url = getCodeBase();
		   //System.out.println("URL: "+ url);
		   URLConnection conn = url.openConnection();

		   gID= getParameter("ID");
		   gRMA= getParameter("RMA");
			   
	   } catch (Exception e) {
	   		System.out.println("Error:NG " + e);
	   }
	
	try {
        // Create a URL for the desired file
		String base = getCodeBase().toString() + "Data/" + gID + ".txt";
		//System.out.println(base);
        URL url = new URL(base); 
        int x=-1, y=-1;
        // Read all the text returned by the server
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String str; int index=0;
        while ((str = in.readLine()) != null) 
        {
            // str is one line of text; readLine() strips the newline character(s)
        	index = str.indexOf(';');
        	artikel[++x][++y] = str.substring(0,index);
         	artikel[x][++y] = str.substring(index+1); 
        	y=-1;
        }
        upper = x;
        in.close();
    } catch (MalformedURLException e) {
    	System.out.println("URL:NG");
    } catch (IOException e) {
    	System.out.println("EX:NG");
    }

}
	


private javax.swing.JPanel getapp_MainContentPane() {
    if (app_MainContentPane == null) {
        try {
        	
           app_MainContentPane = new javax.swing.JPanel();
           app_MainContentPane.setName("app_MainContentPane");
           app_MainContentPane.setBackground(new java.awt.Color(255, 255, 255));
           app_MainContentPane.setForeground(new java.awt.Color(0, 0, 0));
           app_MainContentPane.setFont(new java.awt.Font("Arial", 0, 10));
           app_MainContentPane.setOpaque(true);
           app_MainContentPane.setLayout(null);
           
           getapp_MainContentPane().add(gettheScroll(), gettheScroll().getName());
           //getapp_MainContentPane().add(getLabel100(), getLabel100().getName());
           //getLabel100().setBackground(getapp_MainContentPane().getBackground());
           getapp_MainContentPane().add(getcmdNeu(), getcmdNeu().getName());
           getapp_MainContentPane().add(getcmdRMA(), getcmdRMA().getName());
           getapp_MainContentPane().add(getlblDatum(), getlblDatum().getName());
           getlblDatum().setBackground(getapp_MainContentPane().getBackground());
        } catch (java.lang.Throwable Exc) {
        	System.out.println("ContExc: " + Exc);
        handleException(Exc);
        }
    }
    return app_MainContentPane;
}

private javax.swing.JLabel getLabel100() {
    if (Label100 == null) {
        try {
           Label100 = new javax.swing.JLabel();
           Label100.setBounds(137, 6, 183, 20);
           Label100.setMinimumSize(new java.awt.Dimension(183, 20));
           Label100.setMaximumSize(new java.awt.Dimension(183, 20));
           Label100.setName("Label100");
           Label100.setBackground(new java.awt.Color(204, 204, 204));
           Label100.setForeground(new java.awt.Color(0, 0, 0));
           Label100.setFont(new java.awt.Font("Arial", 1, 12));
           Label100.setOpaque(false);
           Label100.setText("RMA - Anforderung");
        } catch (java.lang.Throwable Exc) {
        handleException(Exc);
        }
    }
    return Label100;
}


private javax.swing.JScrollPane gettheScroll() {
    if (theScroll == null) {
        try {
           theScroll = new javax.swing.JScrollPane();
           theScroll.setName("theScroll");
           theScroll.setBounds(3, 20, 540, 305);
           theScroll.setBackground(new java.awt.Color(255, 255, 255));
           theScroll.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
           theScroll.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
           gettheScroll().setViewportView(gettheTab());
        } catch (java.lang.Throwable Exc) {
        handleException(Exc);
        }
    }
    return theScroll;
}

private javax.swing.JTable gettheTab() {
	model = new DefaultTableModel();
	if (theTab == null) {
        try {
        	theTab = new javax.swing.JTable();
           theTab.setBounds(0, 0, 531, 299);
           theTab.setMinimumSize(new java.awt.Dimension(531, 299));
           theTab.setMaximumSize(new java.awt.Dimension(531, 299));
           theTab.setName("theTab");
           theTab.setBackground(new java.awt.Color(255, 255, 255));
           theTab.setForeground(new java.awt.Color(0, 0, 0));
           theTab.setFont(new java.awt.Font("Arial", 0, 10));
           //theTab.setOpaque(true);
           //theTab.setLayout(null);
           //theTab.setAutoCreateColumnsFromModel(false);
           theTab.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
           
            theTab.setModel(model);
			model.addTableModelListener(this);
			model.setRowCount(0); 
			System.out.println("t01");
			model.addColumn(new Object[] {"HALLO"}); //ID
			System.out.println("t2");
//
//			model.addColumn("Artikel Nr.");
//			model.addColumn("Modellbezeichnung");
//			model.addColumn("Menge");
//			model.addColumn("Fehlerbeschreibung");
        	System.out.println("t2");

//			TableColumn col = theTab.getColumnModel().getColumn(0);
//			col.setMinWidth(0); col.setMaxWidth(0); col.setPreferredWidth(0);
//			//theTab.setRowHeight();
//			col = theTab.getColumnModel().getColumn(1);
//			col.setPreferredWidth(80);
//			col = theTab.getColumnModel().getColumn(2);
//			col.setPreferredWidth(150);
//			col = theTab.getColumnModel().getColumn(3);
//			col.setPreferredWidth(50);
//			col = theTab.getColumnModel().getColumn(4);
//			col.setPreferredWidth(250);
			
System.out.println("1t");
			//setProjects();
			//model.addRow(new Object[] {"-1", ""} );
			
        } catch (java.lang.Throwable Exc) {
        	System.out.println("tabEx" + Exc);

        handleException(Exc);
        }
    }
    return theTab;
}


public void setUpComboColumn(JTable table,
        TableColumn Column, String[] items,
		long itemCount) 
{
	//Set up the editor for the sport cells.
	JComboBox comboBox = new JComboBox();
	comboBox.setBackground(new java.awt.Color(255,255,255));
	comboBox.setForeground(new java.awt.Color(0, 0, 0));
	comboBox.setFont(new java.awt.Font("Arial", 0, 10));
	for(int i=0; i<= itemCount; i++)
	{
		comboBox.addItem(items[i]);
	}
	Column.setCellEditor(new DefaultCellEditor(comboBox));
	
	//Set up tool tips for the sport cells.
	DefaultTableCellRenderer renderer =
			new DefaultTableCellRenderer();
	renderer.setToolTipText("ArtikelNr eingeben");
	Column.setCellRenderer(renderer);
}


private void setProjects()
{
	String[] values = null;
	int c = -1;
	
	for (int a=0; a<= upper; a++)
	{
		values[a] = artikel[a][1];
	}

	setUpComboColumn(theTab, 
			theTab.getColumnModel().getColumn(2),
		values,upper);
		

}


private javax.swing.JButton getcmdNeu() {
    if (cmdNeu == null) {
        try {
           cmdNeu = new javax.swing.JButton();
           cmdNeu.setBounds(10, 335, 85, 21);
           cmdNeu.setMinimumSize(new java.awt.Dimension(85, 21));
           cmdNeu.setMaximumSize(new java.awt.Dimension(85, 21));
           cmdNeu.setName("cmdNeu");
           cmdNeu.setBackground(new java.awt.Color(204, 204, 204));
           cmdNeu.setForeground(new java.awt.Color(0, 0, 0));
           cmdNeu.setFont(new java.awt.Font("Arial", 0, 10));
           cmdNeu.setOpaque(true);
           cmdNeu.setText("Neu");
           cmdNeu.setActionCommand("");
        } catch (java.lang.Throwable Exc) {
        handleException(Exc);
        }
    }
    return cmdNeu;
}

private javax.swing.JButton getcmdRMA() {
    if (cmdRMA == null) {
        try {
           cmdRMA = new javax.swing.JButton();
           cmdRMA.setBounds(100, 335, 113, 21);
           cmdRMA.setMinimumSize(new java.awt.Dimension(113, 21));
           cmdRMA.setMaximumSize(new java.awt.Dimension(113, 21));
           cmdRMA.setName("cmdRMA");
           cmdRMA.setBackground(new java.awt.Color(204, 204, 204));
           cmdRMA.setForeground(new java.awt.Color(0, 0, 0));
           cmdRMA.setFont(new java.awt.Font("Arial", 0, 10));
           cmdRMA.setOpaque(true);
           cmdRMA.setText("RMA Anfordern");
           cmdRMA.setActionCommand("");
        } catch (java.lang.Throwable Exc) {
        handleException(Exc);
        }
    }
    return cmdRMA;
}

private javax.swing.JLabel getlblDatum() {
    if (lblDatum == null) {
        try {
           lblDatum = new javax.swing.JLabel();
           lblDatum.setBounds(470, 5, 65, 14);
           lblDatum.setMinimumSize(new java.awt.Dimension(65, 14));
           lblDatum.setMaximumSize(new java.awt.Dimension(65, 14));
           lblDatum.setName("lblDatum");
           lblDatum.setBackground(new java.awt.Color(204, 204, 204));
           lblDatum.setForeground(new java.awt.Color(0, 0, 0));
           lblDatum.setFont(new java.awt.Font("Arial", 0, 10));
           lblDatum.setOpaque(false);
           Date d = new Date();
           SimpleDateFormat df1 = new SimpleDateFormat("dd.MM.yyyy");
   	       df1.setTimeZone(TimeZone.getDefault());
   		
           lblDatum.setText(df1.format(d));
        } catch (java.lang.Throwable Exc) {
        handleException(Exc);
        }
    }
    return lblDatum;
}

public String getAppletInfo() {
    return "RMA Anforderungsformular (Genius Systems)" + 
        "\n" +
        ".\n" +
        "Creation date: 25.03.2004\n" +
        "@author: MMediaWorx - Vatan Cetintas\n" +
		 "\n";
}
public void init() {
	try {
		setName("RMAForm");
		setSize(550, 350);
       initialize();
		// user code begin {1}
		// user code end
	} catch (java.lang.Throwable Exc) {
		// user code begin {2}
		// user code end
		handleException(Exc);
	}
}


private void initialize() {
    try {
        setSize(550, 350);
        setContentPane(getapp_MainContentPane());
        //String plaf = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
        String plaf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
        try{
        	javax.swing.UIManager.setLookAndFeel(plaf);
        	javax.swing.SwingUtilities.updateComponentTreeUI(this);
        }
        catch(javax.swing.UnsupportedLookAndFeelException ex1){
        	// System.out.println(ex1);
        }
        catch(ClassNotFoundException ex2){
        	// System.out.println(ex2);
        }
        catch(InstantiationException ex3){
        	// System.out.println(ex3);
        }
        catch(IllegalAccessException ex4){
        	// System.out.println(ex4);
        }
        initConnections();
     }
     catch (java.lang.Throwable Exc) {
         handleException(Exc);
     }
}


private void handleException(java.lang.Throwable exception) {
	/* Uncomment the following lines to print uncaught exceptions to stdout */
    // System.out.println("--------- UNCAUGHT EXCEPTION ---------");
    // exception.printStackTrace(System.out);
}



private javax.swing.JPanel app_MainContentPane = null;

private DefaultTableModel model = null;
private javax.swing.JLabel Label100 = null;
private javax.swing.JScrollPane theScroll = null;
private javax.swing.JTable theTab = null;
private javax.swing.JButton cmdNeu = null;
private javax.swing.JButton cmdRMA = null;
private javax.swing.JLabel lblDatum = null;
private String path = "";
private long laufnr = 0;
private String gID = ""; 
private String gRMA = ""; 
private String artikel[][] = new String[300][2];
private long upper =-1;


public static void main(java.lang.String[] args) {
	try {
		javax.swing.JFrame frame = new javax.swing.JFrame();
		RMAForm aRMAForm;
		Class iiCls = Class.forName(".RMAForm");
		ClassLoader iiClsLoader = iiCls.getClassLoader();
		aRMAForm = (RMAForm)java.beans.Beans.instantiate(iiClsLoader,".RMAForm");
		frame.getContentPane().add("Center", aRMAForm);
		frame.setSize(aRMAForm.getSize());
		frame.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				System.exit(0);
			};
		});
		frame.show();
		java.awt.Insets insets = frame.getInsets();
		frame.setSize(frame.getWidth() + insets.left + insets.right, frame.getHeight() + insets.top + insets.bottom);
		frame.setVisible(true);
	} catch (Throwable exception) {
		System.err.println("Exception occurred in main() of javax.swing.JApplet");
		exception.printStackTrace(System.out);
	}
}
}
```


----------



## redman (3. Mai 2004)

Hi,
habe die Ursache endlich herausgefunden!

Es liegt irgendwie am TableModelListener.

Weiß zwar noch nicht warum, aber schaue es mir jetztan.

mfg


----------

