Java: Have a JTable’s column preferred width adjusted perfectly to the size of the longest string in the model

Here’s a utility method I’ve coded for FrostWire’s partial download dialog. With it I’m able to adjust a JTable’s column by iterating over the table’s column model data and calculating the exact dimensions required to render the longest string found. You can specify a maximum width (to avoid some really long strings from screwing up the display) and some right padding in case you need some breathing room.

Enjoy

[java]
/**
* It will adjust the column width to match the widest element.
* (You might not want to use this for every column, consider some columns might be really long)
* it assumes model and jtable != null
*/
public static void adjustColumnWidth(TableModel model, int columnIndex, int maxWidth, int rightPadding, JTable table) {

if (columnIndex > model.getColumnCount()-1) {
//invalid column index
return;
}

if (!model.getColumnClass(columnIndex).equals(String.class)) {
return;
}

String longestValue = “”;
for (int row = 0; row < model.getRowCount(); row++) { String strValue = (String) model.getValueAt(row, columnIndex); if (strValue != null && strValue.length() > longestValue.length()) {
longestValue = strValue;
}
}

Graphics g = table.getGraphics();

try {
int suggestedWidth = (int) g.getFontMetrics(table.getFont()).getStringBounds(longestValue, g).getWidth();
table.getColumnModel().getColumn(columnIndex).setPreferredWidth(((suggestedWidth > maxWidth) ? maxWidth : suggestedWidth)+rightPadding);
} catch (Exception e) {
table.getColumnModel().getColumn(columnIndex).setPreferredWidth(maxWidth);
e.printStackTrace();
}

}
[/java]

Note, make sure you invoke this guy only once, usually after the table has been first painted (you could override paint()) or after the model data has been updated (if it gets to be updated and you feel like autoadjusting a particular column)

Kindle Fire HD Unboxing, First Impressions, Thoughts about iPad 3 and iPad Mini

Welcome to my unboxing of the new Kindle Fire HD. If you don’t know me, I’m an Android product designer and developer of FrostWire for Android, been an Android user for the last 3 years, as a developer I’ve had the opportunity to try lots of different Android phones and tablets and this year I got fed up with the shitty experience of all the Android tablets and became an iPad 3 owner.

I’ve bought this Kindle Fire HD hoping that Jeff Bezos and his teams have finally made an Android tablet experience not meant for early adopters or developers willing to withstand a shitty experience, a tablet taking into consideration the average Joe and what they expect a tablet to be like.

So far I can say that I’m not disappointed, join me on this unboxing and see if you agree with my first impressions of the product, my suspicions of why Apple had to go against Steve Jobs wishes and release a 7 inch iPad (iPad Mini) ended today as I tried the Kindle Fire HD.


First Kindle, or Amazon packaging I remember that comes in a black box, first statement of the experience. You don’t need a knife to open the package, just tear the band and you’re done, beautiful.


There it is, and you can’t help but pull that arrow, they play with our curiosity to force us to read the only physical instructions that come in the box, they didn’t waste any resources on printing warranty sheets or manuals.

Continue reading

FrostWire for Android 1.0 preview: Quickly checking what files are shared.

Try it now on your Android (Before the rest gets it on Google Play)

Now when you’re browsing “My Files”, you will see indicators on the top right corner of the screen letting you know how many files are shared and how many files are not shared. If you touch the indicators, FrostWire will show you only the shared ones, the un-shared ones, or all the files.

Very useful if you just shared a file and you want to unshare it, or viceversa, specially when you have hundreds or thousands of files in your device.