Swing: JTable mit eingenem CellRenderer - Problem

daniel_sun

Erfahrenes Mitglied
Hallo,

ich habe ich für eine meiner Tabellen einen eigenen CellRenderer geschreiben, da ich in einer Spalte kleiner Bilder einfügen möchte:

Java:
    class ImageRenderer extends DefaultTableCellRenderer {
    	
	    JLabel lbl = new JLabel();
	    
	    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
		    lbl.setIcon((ImageIcon)value);
		    return lbl;
	    }
    }

Hier werden die Bilder aus den Array hinzugefügt. Wenn der wert im Array -1 ist heist es das es zu diesem Eintrag kein Bild gibt.

Java:
for(int j = 0; j<clean.length; j++){
					
				    ImageIcon icon = null;
				    
					try {
						if(clean[j][0] != "-1"){
							icon = new ImageIcon(new URL(clean[j][0]));
						}else{
							icon = new ImageIcon();
						}
						
					} catch (MalformedURLException e) {
						e.printStackTrace();
					}
					
				    this.getColumnModel().getColumn(1).setCellRenderer(new ImageRenderer());
				    DefaultTableModel Tmodel=(DefaultTableModel) this.getModel();
				    this.setRowHeight(120);
				    Tmodel.setValueAt(icon, j, 1);
	
				}

Es funktioniert alles super. Die richtigen Bilder werden an der richtien Stelle ausgegeben und die Stellen wo es kein Bild gibt bleibt der Platz frei. Leider bekomm ich trotzdem noch einen Error ausgegeben den ich beheben möchte.

Code:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to javax.swing.ImageIcon
	at FilmDB.ui.MovieSearch.Elements.Table.ResultTable$ImageRenderer.getTableCellRendererComponent(ResultTable.java:138)
	at javax.swing.JTable.prepareRenderer(Unknown Source)
	at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source)
	at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source)
	at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source)
	at javax.swing.plaf.ComponentUI.update(Unknown Source)
	at javax.swing.JComponent.paintComponent(Unknown Source)
	at javax.swing.JComponent.paint(Unknown Source)
	at javax.swing.JComponent.paintChildren(Unknown Source)
	at javax.swing.JComponent.paint(Unknown Source)
	at javax.swing.JViewport.paint(Unknown Source)
	at javax.swing.JComponent.paintChildren(Unknown Source)
	at javax.swing.JComponent.paint(Unknown Source)
	at javax.swing.JComponent.paintToOffscreen(Unknown Source)
	at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
	at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
	at javax.swing.RepaintManager.paint(Unknown Source)
	at javax.swing.JComponent._paintImmediately(Unknown Source)
	at javax.swing.JComponent.paintImmediately(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.access$700(Unknown Source)
	at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$000(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to javax.swing.ImageIcon
	at FilmDB.ui.MovieSearch.Elements.Table.ResultTable$ImageRenderer.getTableCellRendererComponent(ResultTable.java:138)
	at javax.swing.JTable.prepareRenderer(Unknown Source)
	at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source)
	at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source)
	at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source)
	at javax.swing.plaf.ComponentUI.update(Unknown Source)
	at javax.swing.JComponent.paintComponent(Unknown Source)
	at javax.swing.JComponent.paint(Unknown Source)
	at javax.swing.JComponent.paintToOffscreen(Unknown Source)
	at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
	at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
	at javax.swing.RepaintManager.paint(Unknown Source)
	at javax.swing.JComponent._paintImmediately(Unknown Source)
	at javax.swing.JComponent.paintImmediately(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.access$700(Unknown Source)
	at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$000(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)

Aus dem Fehler java.lang.String cannot be cast to javax.swing.ImageIcon schließe ich das das problem damit zusammen hängt wenn eine Zeile kommt in der es kein Bild kommt leider finde ich den Fehler nicht. Ich hoffe ihr könnt helfen.

Gruß
daniel_sun
 

I2oxxi

Mitglied
weil du immer in den ersten if block läufst, aber niemals in den elseblock.
Java:
if(!clean[j][0].equals("-1"))
sollte den fehler beheben. primitve datentypen werden mit == bzw != geprüft, klassen mit equals
hat was damit zu tun das ==die referenz überprüft, equals "den inhalt"
 
Zuletzt bearbeitet:

daniel_sun

Erfahrenes Mitglied
Hate nicht funktioniert! Aber der Ansatz war richtig :)

Übergebe den CellRenderer nun nur den String und im Renderer wird unterschieden ob ein ImageIcon eingefügt wird.

Java:
    class ImageRenderer extends DefaultTableCellRenderer {
    	
	    JLabel lbl = new JLabel();
	    
	    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
	    	if(value.equals("-1")){
	    		lbl.setText("");
	    	}else{
	    		try {
					lbl.setIcon(new ImageIcon(new URL((String) value)));
				} catch (MalformedURLException e) {
					e.printStackTrace();
				}
	    	}
		    return lbl;
	    }
    }
 

I2oxxi

Mitglied
warum übergibs du denn value als object wenns eh nur mit nem string verglichen / in nen string gecastet wird?

edit:: ah schon gut ist ja aus der oberklasse ^^