Other font in JTable cell

While doing some research, I was looking for an efficient way to change the font inside a JTable. I have made my own TableCellRenderer’s before, but while googling I came across the following link:

http://www.exampledepot.com/egs/javax.swing.table/CustRend.html

Beside the fact I wanted to make a certain cell italic, the last few lines somehow intrigued me to find out more about the author of this Java Developers Almanac, which is Patrick Chan. Great book, but not really my kinda book. Anyway, here is the TableCellRenderer which can be used to create a column in a JTable which has an italic font (thus another font, which I was looking for) :

import java.awt.Component;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableCellRenderer;

public class TableCellRenderer_Italic extends JLabel implements TableCellRenderer {
    // used to pad the cell when it does not have focus
    protected static Border normalBorder = new EmptyBorder(1, 1, 1, 1);

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        if(isSelected) {
            setForeground(table.getSelectionForeground());
            setBackground(table.getSelectionBackground());
        } else {
            setForeground(table.getForeground());
            setBackground(table.getBackground());
        }
        if(hasFocus) {
            setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
        } else {
            setBorder(normalBorder);
        }
        setFont(new Font(this.getFont().getName(), Font.ITALIC, this.getFont().getSize()));
        setText(value.toString());
        return this;
    }
    // the following methods overide the defaults for performance reasons, lines thanks to Patrick Chan
    public void validate() {}
    public void revalidate() {}
    protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {}
    public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {}
}

Note: to use it in another class:

private TableCellRenderer itaRenderer = new TableCellRenderer_Italic();
myTable.getColumnModel().getColumn(3).setCellRenderer(itaRenderer);

Getting date time from MySQL using Java JDBC

Since I seem to forget how to get/convert dates from the database into Java using JDBC (I use Oracle and MySQL for different projects), a reminder:

Suppose there is a MySQL table structure like:

create table prp_models (
  prp_id        int(10) unsigned not null auto_increment,
  prp_created   datetime default null,
  prp_
)

Then a proper way to get the prp_created column from a resultset (res) is:

java.sql.Timestamp myDateTime = res.getTimestamp(2);  // res #1 is the ID

If you want to get the date time in a displayable format (String), you can use:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(myDateTime.getTime());

Note on the side: within Java you can set myDateTime with system time using:
myDateTime = new Timestamp(System.currentTimeMillis());

weblog launched

Today dotJava got it’s own weblog, just like many others on the web.

Main goal of this weblog is to collect scripts, notes and other interesting documents found around the net. At any time it’s possible for me to access the collection and in the meantime others can enjoy the collection as well.

So, enjoy!