Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: iTunes-like JTable (alternating row colors)...



Steve Roy <email@hidden> wrote:

> Come to think of it, it's the component that lays out the table that should be
> overriden. That's probably the viewport.

Just for fun, I played around some more with this code and the above idea was
the right one. It was rather easy to implement, I implement a custom viewport
class to force the table to fill the viewport, and I added painting of the dummy
rows by overriding paintComponent in the table. Here's the new code. Enjoy!

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;

/**
* This is an iTunes-like table.
*/
public class Test
{
public static void main(String[] args)
{
new Test();
}

private Test()
{
JFrame f = new JFrame();

JPanel c = (JPanel)f.getContentPane();
c.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

// Create our iTunes-like table
MyTable t = new MyTable(new MyTableModel());

// Install it in the scrollpane with our custom viewport
JScrollPane sp = new JScrollPane(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
MyViewport vp = new MyViewport();
vp.setView(t);
sp.setViewport(vp);
c.add(sp);

f.setSize(500, 500);
f.show();
}

/**
* A custom viewport to force the view (table) to fill the viewport
* at all times.
*/
private class MyViewport extends JViewport
{
public void setViewSize(Dimension newSize)
{
Dimension s = getSize();
if (newSize.height < s.height)
newSize = new Dimension(newSize.width, s.height);
super.setViewSize(newSize);
}
}

/**
* Some bogus data to populate the table.
*/
private class MyTableModel extends DefaultTableModel
{
public int getRowCount()
{
return 10;
}

public int getColumnCount()
{
return 3;
}

public String getColumnName(int column)
{
switch (column)
{
case 0:
return "Song Name";
case 1:
return "Time";
default:
return "Artist";
}
}

public Object getValueAt(int row, int column)
{
switch (column)
{
case 0:
return "Fooing In The Wind";
case 1:
return "3:51";
default:
return "Foo Guy";
}
}

public boolean isCellEditable(int row, int column)
{
return false;
}
}
}

/**
* A simple implementation of an iTunes-like table.
*/
class MyTable extends JTable
{
private Color whiteColor = new Color(254, 254, 254);
private Color alternateColor = new Color(237, 243, 254);
private Color selectedColor = new Color(61, 128, 223);

public MyTable(TableModel model)
{
super(model);
getTableHeader().setDefaultRenderer(new MyHeaderRenderer());
setDefaultRenderer(Object.class, new MyCellRenderer());
setIntercellSpacing(new Dimension(1, 1));
setShowHorizontalLines(false);
setShowVerticalLines(true);
setGridColor(Color.lightGray);
}

public void paintComponent(Graphics g)
{
// Let the table paint the real rows
super.paintComponent(g);

// Now check if we need to paint some empty ones
Dimension s = getSize();
int rh = getRowHeight();
int n = getRowCount();
int th = n * rh;
if (th < s.height)
{
// Paint the empty rows
int y = th;
while (y < s.height)
{
g.setColor(n % 2 == 0 ? alternateColor : whiteColor);
g.fillRect(0, y, s.width, rh);
y += rh;
n++;
}

// Paint the vertical grid lines
g.setColor(Color.lightGray);
TableColumnModel cm = getColumnModel();
n = cm.getColumnCount();
y = th;
int x = 0;
for (int i = 0; i < n; i++)
{
TableColumn col = cm.getColumn(i);
x += col.getWidth();
g.drawLine(x - 1, y, x - 1, s.height);
}
}
}

/**
* The cell renderer.
*/
private class MyCellRenderer extends DefaultTableCellRenderer
{
public Component getTableCellRendererComponent(JTable table,
Object value, boolean selected, boolean focused,
int row, int column)
{
super.getTableCellRendererComponent(table, value,
selected, focused, row, column);

// Set the background color
Color bg;
if (!selected)
bg = (row % 2 == 0 ? alternateColor : whiteColor);
else
bg = selectedColor;
setBackground(bg);

// Set the foreground to white when selected
Color fg;
if (selected)
fg = Color.white;
else
fg = Color.black;
setForeground(fg);

return this;
}
}

/**
* The header renderer. All this does is make the text left aligned.
*/
private class MyHeaderRenderer extends DefaultTableCellRenderer
{
public Component getTableCellRendererComponent(JTable table,
Object value, boolean selected, boolean focused,
int row, int column)
{
super.getTableCellRendererComponent(table, value,
selected, focused, row, column);
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
return this;
}
}
}

Steve

--
Steve Roy <email@hidden>
Personal homepage: <http://homepage.mac.com/sroy>
Projects homepage: <http://www.roydesign.net>
_______________________________________________
java-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/java-dev
Be sure to read the FAQ http://developer.apple.com/java/faq/ before posting
Do not post admin requests to the list. They will be ignored.



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.