# JTable zeile einfärben



## pope (4. Mai 2009)

Ich möchte in einer JTable eine bestimmte Zeile einfärben, das RenderModel macht dies auch, allerdings möchte ich das standard-render für rowselection nicht verlieren. Wie kann ich dieses Problem lösen?


```
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableCellRenderer;

public class PDatabase extends JPanel {
	private Model dmodel;
	private Settings settings;
	private DBController dbController;
	private DBContentModel fileTableModel;
	private DataImport dataImport;
	private JTable table;
	private JScrollPane tableSP;
	private JButton import_data = new JButton("importieren"); // ein neues
																// File in die
																// Datenbank
																// importieren
	private JButton delete_data = new JButton("löschen"); // bestehender
															// Eintrag aus
															// Datenbank löschen
	private JButton search_data = new JButton("suchen"); // Eintrag in
															// Datenbank suchen
	private JButton options = new JButton("optionen"); // Einstellungen für
														// Datenbank
	private JButton read_data = new JButton("einlesen");
	private Border border = new TitledBorder(BorderFactory.createEtchedBorder(
			Color.white, new Color(165, 163, 151)), " Datenbank ");
	private Border geoborder = new TitledBorder(BorderFactory
			.createEtchedBorder(Color.white, new Color(165, 163, 151)),
			" Geometriedaten ");

	private JCheckBox showDB = new JCheckBox("Datenbank anzeigen", true);

	File selectedFile;

	Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	Dimension frameSize = getSize();

	public PDatabase(Model dmodel_parm) {
		this.dmodel = dmodel_parm;
		this.setLayout(new GridBagLayout());
		this.setBorder(border);

		settings = new Settings();
		dbController = new DBController();

		fileTableModel = new DBContentModel(dbController, settings.getVisibleColumns());
		table = new JTable(fileTableModel);
		table.setDefaultRenderer(Object.class, new MyTableCellRender(2));  
		table.setAutoCreateRowSorter(true);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		tableSP = new JScrollPane(table);

		GridBagConstraints c = new GridBagConstraints();

		// this.add(showDB, new GridBagConstraints(0, 0, 6, 1, 0, 0,
		// GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
		// 2, 2, 2, 2), 0, 0));
		this.add(tableSP, new GridBagConstraints(0, 1, 6, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						2, 2, 2, 2), 0, 0));
		this.add(read_data, new GridBagConstraints(0, 2, 1, 1, 1, 0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						2, 2, 2, 2), 0, 0));
		this.add(import_data, new GridBagConstraints(2, 2, 1, 1, 1, 0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						2, 2, 2, 2), 0, 0));
		this.add(delete_data, new GridBagConstraints(3, 2, 1, 1, 1, 0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						2, 2, 2, 2), 0, 0));
		this.add(search_data, new GridBagConstraints(4, 2, 1, 1, 1, 0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						2, 2, 2, 2), 0, 0));
		this.add(options, new GridBagConstraints(5, 2, 1, 1, 1, 0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						2, 2, 2, 2), 0, 0));

		// Action Listeners

		read_data.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (table.getSelectedRow() != -1) {
					int row = table.getSelectedRow();
					File file = new File("data\\"
							+ fileTableModel.rowSourceFile(row) + ".z1p");
					DataRead read = new DataRead(dbController, file, dmodel);
					read.readSelectedFile();
//					table.setDefaultRenderer(Object.class, new MyTableCellRender(table.getSelectedRow()));  
//					table.setModel(fileTableModel);

					
				}
			}
		});

		delete_data.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				if (table.getSelectedRow() != -1) {
					DataDelete delEntry = new DataDelete(dbController,
							fileTableModel, settings, table);
					delEntry.deleteSelectedTableEntry();
				}

			}
		});

		options.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				TableOptions tabOptions = new TableOptions(dbController,
						fileTableModel, settings, table);
				tabOptions.setTableOptions();

			}
		});

		import_data.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				File selectedFile = dbController.selectFile();
				if (selectedFile != null) {

					dbController.openFile(selectedFile);
					String inductance = "6.23mH";

					dataImport = new DataImport(dbController, fileTableModel,
							settings, table, inductance);
					dataImport.showWin();
				}
			}
		});

		search_data.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				DataSearch search = new DataSearch(dbController, settings);
				search.searchTableData();

			}
		});
	}

}

class MyTableCellRender extends DefaultTableCellRenderer {
	int selectedRow;
	public MyTableCellRender(int selectedRow) {
		super();
		this.selectedRow = selectedRow;
		setOpaque(true);
	}

	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		if(selectedRow != -1){
			if (row == selectedRow) {
				Color backgroundColor = new Color(160, 220, 150);
	//			setForeground(Color.black);
				setBackground(backgroundColor);
			} else {
				setBackground(Color.white); 
			
			}
		}
		setText(value != null ? value.toString() : "");
		return this;
	}
}
```


----------



## MiDniGG (4. Mai 2009)

Forensuche?
http://www.java-forum.org/awt-swing-swt/81583-farbe-einer-einzelnen-zeile-einer-jtable-aendern.html
Oder passt das nicht zu Deinem Problem?


----------

