iTunes Style JTable
Last week I mentioned I had been working on a custom JTable class for Java that looks like iTunes 7. I’ve finally got my act together to clean up the code and write some javadoc so that other people might be able to use it.
It’s really simple to use, just instantiate JTunesTable instead of JTable (after downloading the jar), and you can use this style of table in your apps! Failing that if you are looking at creating your own style of JTable this may be a good example to use, it looks at renderering cells and headers, selection models and many other of java SWINGs magic components.
Here you can see the difference between JTunesTable and JTable, maybe I’m biased - but mine looks so much better!
If you find this source code useful or have any hints on how to make it better feel free to leave me a comment.
You can download JTunesTable from here or look at my other source code.
Happy SWINGing ![]()
Lauren
UPDATE: I have now updated the JTunesTable classes, to fix rendering issues when repainting on Windows. It should now rendering just as well, in appearance at least, on Windows and MacOSX.
UPDATE: For those wishing to know how to use a jar in Eclipse. Right click on your current project, select “Build Path…” from the menu, then select “Add External Archives…”. You should then be given a file browser, browse to the jar you wish to add and select it. Press Ok. Hey presto you should get the shiny goodness of JTunesTable on ctrl+space!!
May 14th, 2007 at 10:14 pm
Excellent work you have done! I am attempting to use your jtunes table in my application and it looks beautiful!
However, I have some tips to how the performance can be improved. Compared to a regular JTable it is in its current form about 3-5 times as slow. Here are the tricks I found that make a huge difference:
- Make JTunesTableCellRenderer and JTunesHeaderRenderer extend DefaultTableCellRenderer instead of JLabel (makes some unnecessary methods noop)
- The paintComponent(Graphics g) in JTunesTableCellRenderer seems to work better when changed to:
———————————————————————————–
public void paintComponent(Graphics g) {
if (systemColor) {
if (selected) {
setForeground(systemHighlightTextColor);
setBackground(systemHighlightColor);
}
super.paint(g);
return;
} else {
if (selected) {
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(getGradientPaint());
Rectangle2D drawRect = new Rectangle2D.Double(0, 0, getWidth(), getHeight());
g2.fill(drawRect);
} else {
Color bgColor = getBackground();
g.setColor(bgColor);
g.fillRect(0, 0, getWidth(), getHeight());
setForeground(selectedColor);
}
}
super.paintComponent(g);
}
———————————————————————————–
Although I am not sure the paintComponent trick will work on all platforms? If you have any other ways to improve performance I would be very grateful!
May 14th, 2007 at 11:29 pm
I am sorry. There is a small mistake in that code that will make selected rows black instead of blue.
Replace the last 4 lines with:
———————————————————————
}
setBackground(new Color(100, 100, 100, 0));
}
super.paintComponent(g);
}
———————————————————————
June 26th, 2007 at 11:31 pm
Really nice work! Very attractive.
I am, however, having some trouble making it work with the TableExample.java example that comes with Apple Developer Toolkit:
/Developer/Examples/Java/JFC/TableExample/TableExample.java
Has anyone successfully gotten JTunesTable to work with this example? For me, nothing shows up.
June 29th, 2007 at 10:42 pm
Here’s a clarification of the problem I’m having.
Works:
JTunesTable table = new JTunesTable(model);
Doesn’t work:
JTunesTable table = new JTunesTable();
table.setModel(model);
I think the problem with the latter is that AutoCreateColumnsFromModel is set to false, and that’s set to false because createColumnsFromModel resets style. So my gut feeling is that to fix this issue will require overriding createColumnsFromModel with code that doesn’t reset rendering. I’ve given that a stab, but so far it’s not working.
October 11th, 2007 at 4:28 pm
Hello, I think that is a great job and a wonderful style !!!
And run the .jar and I can see the example, but I having a problem to use it on my work. I’m working with eclipse ide in a swing application.
Could someone please tell me how to use it ???
November 21st, 2007 at 12:11 pm
How do i import this jar file into my project in netbeans??
November 23rd, 2007 at 10:53 am
iam trying to use this awesome JTable but i got this exception then i try to add it in the JFrame.. (i use netbeans and have added the JTunesTable to the palette list so i can drag and drop it into my JFrame but i gor exceptions)..
the following execption has been thrown during painting of the form. Use Inspector to fix or remove the problem.
java.lang.NullPointException
at.ui.jtunestabe.JTunesTableCellRenderer.getTableCellRendererComponent(Unknown Source)
at javax.swing.JTable.preperRenderer(JTable.java:5683)
can someone help me out…
March 29th, 2008 at 12:01 pm
this is a very good looking table, and yes it’s hard to find good looking table for java so job well done!
but i think i have problems when trying to sort data by clicking at the column header.
this is the error exception:
java.lang.ClassCastException: models.HighlightModel cannot be cast to javax.swing.table.DefaultTableModel
which makes sense, since i derived my model from AbstractTableModel, but your table expects the model to be DefaultTableModel.
i hope the table will sometime support model which is derived from AbstractTableModel, maybe by simply disabling the sort, or even better by letting the programmer define their own sort function.
but this is a very beautiful table indeed!