JTable Grid Lines

Status
Nicht offen für weitere Antworten.

Oli

Top Contributor
Hallo,

ich habe eine Tabelle, dort färbe ich die Zellen ein. Allerdings verschwinden dann meine GridLines. Was mache ich falsch? Hat jemand nen Tipp?

Code:
JTable table = new JTable(rows, columns) {
    private static final long serialVersionUID = 1L;
    public Component prepareRenderer(final TableCellRenderer renderer, final int row, final int column) {
        Component c = super.prepareRenderer(renderer, row, column);
        if (c instanceof JLabel) {
        if (column == 0) {
	JLabel label = (JLabel) c;
	label.setHorizontalAlignment(JLabel.LEFT);
	if (cellsGray) {
	    label.setBackground(Color.LIGHT_GRAY);
	}
            } else {
	JLabel label = (JLabel) c;
	label.setHorizontalAlignment(JLabel.CENTER);
	if (cellsGray) {
	    label.setBackground(Color.LIGHT_GRAY);
	}
            }
        }
        return c;
    }
    public void setGridColor(Color arg0) {
        super.setGridColor(Color.BLACK);
    }
    public void setShowHorizontalLines(boolean arg0) {
        super.setShowHorizontalLines(true);
    }
    public void setShowVerticalLines(boolean arg0) {
        super.setShowVerticalLines(true);
    }
};


Danke im Voraus,

Oli
 
Hi nochmal,

table.setBackground(Color.LIGHT_GRAY);

löst mein Problem nicht wirklich, denn wenn ich dann Zellen in anderen Farben haben möchte, sind die GridLines wieder weg. Eine Möglichkeit wäre ja den Labels einen Rand zu geben, da ist allerdings das Problem, dass der Rand kleiner dargestellt wird. Kann man die Abstände des Labels auf 0 setzen, so wie bei den Layoutmanagern? flowLayout.setVGap(0); ???

Grüße
 
Also bei mir verschwinden die nicht ???:L

habe mir mal dieses kleine Beispiel gebastelt
Code:
import java.awt.Color;
import java.awt.Component;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

public class T8 extends JFrame{

	public static boolean cell = true;
	public T8() {
		init();
	}
	
	private void init()
	{
		JTable table = new JTable(5, 5) {
		    private static final long serialVersionUID = 1L;
		    public Component prepareRenderer(final TableCellRenderer renderer, final int row, final int column) {
		        Component c = super.prepareRenderer(renderer, row, column);
		        if (c instanceof JLabel) 
		        {
		        	if (column == 0) 
		        	{
		        		JLabel label = (JLabel) c;
		        		label.setHorizontalAlignment(JLabel.LEFT);
		        		if (T8.cell) 
		        		{
		        			label.setBackground(Color.RED);
		        		}
		        		else
		        		{
		        			label.setBackground(Color.BLUE);
		        		}
		        		T8.cell = ! T8.cell;
		        	} 
		        	else 
		        	{
		        		JLabel label = (JLabel) c;
		        		label.setHorizontalAlignment(JLabel.CENTER);
		        		if (T8.cell) 
		        		{
		        			label.setBackground(Color.CYAN);
		        		}
		            }
		        }
		        return c;
		    }
		    public void setGridColor(Color arg0) {
		        super.setGridColor(Color.BLACK);
		    }
		    public void setShowHorizontalLines(boolean arg0) {
		        super.setShowHorizontalLines(true);
		    }
		    public void setShowVerticalLines(boolean arg0) {
		        super.setShowVerticalLines(true);
		    }
		};
		this.add(table);
		this.setSize(200, 200);
		this.setVisible(true);
	}
	
	public static void main(String[] args) {
		new T8();
	}
}

sieht dann so aus:

table.JPG
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Zurück
Oben