| |||
| [Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] |
David Thorup <email@hidden> wrote:_______________________________________________
Has anybody attempted to create a JTable that has the iTunes (and other
iApps) look and feel? That is, alternating blue and white row colors,
vertical grid lines (no horizontal grid lines), and blue column headers
when a column is selected as the sorted column.
For the alternating blue and white rows, what you have to do is make a custom
cell renderer. For the grid line, JTable has methods to set that. For the sorted
column, you're going to have to implement sorting in your table data model, and
you're gonna have to set a custom table header renderer to do the blue yourself.
Here's a start of something to play with. It doesn't have the sorting.
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
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 the iTunes-like table
JTable t = new JTable(new MyTableModel());
t.getTableHeader().setDefaultRenderer(new MyHeaderRenderer());
t.setDefaultRenderer(Object.class, new MyCellRenderer());
t.setIntercellSpacing(new Dimension(1, 1));
t.setShowHorizontalLines(false);
t.setShowVerticalLines(true);
t.setGridColor(Color.lightGray);
JScrollPane sp = new JScrollPane(t,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
sp.setBackground(Color.white);
c.add(sp);
f.setSize(500, 500);
f.show();
}
/**
* The cell renderer.
*/
private class MyCellRenderer extends DefaultTableCellRenderer
{
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 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.
*/
public 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;
}
}
/**
* 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;
}
}
}
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.
| Home | Archives | FAQ | Terms/Conditions | Contact | RSS | Lists | About |
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.