JTable

Achso sorry! Hab ich falsch verstanden!
Ähm es werden nicht ZELLE 9 und 10 sondern SPALTE 9 und 10 gefärbt!
Ich hab den Teil mit dem Focus raus genommen gehabt weils nicht ging mit dem Focus sieht der ganze Code so aus

Java:
public class BK_ColoredTableCellRenderer implements TableCellRenderer{

	private Color lightgrey = new Color(220, 220, 220);
	private Font font		= new Font("Arial", Font.BOLD, 12);
	
	public Component getTableCellRendererComponent(JTable tabelle, Object value, boolean isSelected, boolean hasFocus, int row, int column){
		JLabel label = new JLabel((String)value);
		label.setOpaque(true);
		Border b = BorderFactory.createEmptyBorder(1, 1, 1, 1);
		label.setBorder(b);
		label.setFont(tabelle.getFont());
		label.setForeground(tabelle.getForeground());
		label.setBackground(tabelle.getBackground());
		
		if(isSelected){
			label.setFont(font);
		}

		if(hasFocus){
			label.setBackground(Color.BLACK);
		}
		
		//Angezeigte Spalte in Modellspalte umwandeln
		column = tabelle.convertColumnIndexToModel(column);
		
		if(column == 9 || column == 10){
			label.setBackground(lightgrey);
		}
		
		return label;
	}
	
}

MfG
Saban
 
Zuletzt bearbeitet von einem Moderator:
Na ist doch auch klar. Du setzt die Farbe der Celle auf schwarz und danach, wenn die Zelle in Spalte 9 oder 10 ist, machst du sie wieder grau.

Mach es doch so.
Code:
column = tabelle.convertColumnIndexToModel(column);
    if (hasFocus)
    {
      label.setBackground(Color.BLACK);
    }
    else if (column == 9 || column == 10)
    {
      label.setBackground(lightgrey);
    }

MFG

Sascha
 
Ich bin so ein Depp :eek:
Vielen vielen Dank für deine Hilfe!
Ich ändere des jetzt noch was du mir am Anfang empfohlen hast mit dem Anlegen der Objekte...

Danke nochmal :) :) :)

MfG
Saban
 
Hi!

ähm ich hab des ganze jetzt so geändert...
Java:
package GUI_Tool;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.table.TableCellRenderer;

public class BK_ColoredTableCellRenderer extends JLabel implements TableCellRenderer{

	private Color 	LIGHTGREY 		= new Color(220, 220, 220);
	private Font 	ZELLEN_FONT		= new Font("Arial", Font.BOLD, 12);
	private Border 	ZELLEN_BORDER	= BorderFactory.createEmptyBorder(1, 1, 1, 1);
	
	public BK_ColoredTableCellRenderer(){
		setOpaque(true);
		setBorder(ZELLEN_BORDER);
	}
	
	public Component getTableCellRendererComponent(JTable tabelle, Object value, boolean isSelected, boolean hasFocus, int row, int column){
		setText(value.toString());
		setFont(tabelle.getFont());
		setForeground(tabelle.getForeground());
		setBackground(tabelle.getBackground());
		
		if(isSelected){
			setFont(ZELLEN_FONT);
		}
		
		//Angezeigte Spalte in Modellspalte umwandeln
		column = tabelle.convertColumnIndexToModel(column);
		
		if(hasFocus){
			setBackground(Color.BLACK);
		} else if(column == 9 || column == 10){
			setBackground(LIGHTGREY);
		}
		
		return this;
	}
	
}

Ich bekomme jetzt aber ne java.lang.NullPointerException :confused:

MfG
Saban
 
Zuletzt bearbeitet von einem Moderator:
Hier die Exception:

Java:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at GUI_Tool.BK_ColoredTableCellRenderer.getTableCellRendererComponent(BK_ColoredTableCellRenderer.java:25)
	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.paintChildren(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.JLayeredPane.paint(Unknown Source)
	at javax.swing.JComponent.paintChildren(Unknown Source)
	at javax.swing.JComponent.paintToOffscreen(Unknown Source)
	at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
	at javax.swing.RepaintManager.paint(Unknown Source)
	at javax.swing.JComponent.paint(Unknown Source)
	at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
	at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
	at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
	at java.awt.Container.paint(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
	at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
	at java.awt.event.InvocationEvent.dispatch(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.NullPointerException
	at GUI_Tool.BK_ColoredTableCellRenderer.getTableCellRendererComponent(BK_ColoredTableCellRenderer.java:25)
	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.BufferStrategyPaintManager.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.seqPaintDirtyRegions(Unknown Source)
	at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
	at java.awt.event.InvocationEvent.dispatch(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.NullPointerException
	at GUI_Tool.BK_ColoredTableCellRenderer.getTableCellRendererComponent(BK_ColoredTableCellRenderer.java:25)
	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.BufferStrategyPaintManager.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.seqPaintDirtyRegions(Unknown Source)
	at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
	at java.awt.event.InvocationEvent.dispatch(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)

MfG
Saban
 
Zuletzt bearbeitet von einem Moderator:
Ok, es liegt daran, dass ich nicht getestet hab, ob value null ist.

Ändere diese Zeile
Code:
setText(value.toString());

in

Code:
if(value != null)
   setText(value.toString());

MFG

Sascha
 
Ich muss leider doch sagen das es nicht geht!

Wenn ich des so mach wie du des sagst dann klick ich in eine Zelle tip asdf ein und wenn ich dann in die Zelle drunter klick dann wird asdf in alle Zelle bis zu der drunter (also sprich von Zelle(0/1) bis Zelle(1/1)) vollgeschrieben.

Ich glaub man muss jedes mal ein neues Objekt erzeugen...

MfG
Saban
 
Nein, musst du nicht.

Du musst natürlich auch das Label zurück setzen. Also, wenn kein Text drin stehen soll, dann musst du auch den String im Label löschen.

Code:
if(value != null)
   setText(value.toString());
else
   setText("");

Da man immer das gleiche Object hat muss man das tun.
Wenn man immer ein neues Object anlegt, spart man sich das natürlich, aber dafür hast du mehr Objecte.

Bsp:

Tabelle 10 Zeilen und 10 Spalten
Wenn man immer ein neues Label anlegt, hast du 100 Labels bei jedem repaint der Tabelle.

Wenn du immer das gleiche nutzt, hast du immer nur 1 Label egal wie oft ein repaint ausgeführt wird.

MFG

Sascha
 
Zurück