Reputation: 41
I have a table that fetches data from a database. But in my database, sometimes some fields can be null values and JTable
does not show these null values. I want to show null values in my table as NA. How can I do that?
public void displayMovieTable(){
try {
PreparedStatement pr = conn.prepareStatement("select * from movie");
ResultSet s = pr.executeQuery();
movieTable.setModel(DbUtils.resultSetToTableModel(s));
pr.close();
} catch (SQLException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
Upvotes: 1
Views: 1103
Reputation: 109547
Possibly simplest might be a specific table model wrapping the JDBC one.
However changing the display is the general approach:
TableCellRenderer old = movieTable.getDefaultRenderer(); // I.o. super?
movieTable.setDefaultRenderer(String.class, new DefaultTableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
boolean notAvailable = value == null;
if (notAvailable) {
value = "N/A";
}
Component compon = super.getTableCellRendererComponent(table,
value,
isSelected,
hasFocus,
row,
column);
if (notAvailable) {
compon.setBackground(Color.GRAY);
}
return compon;
}
});
The problem here, is that there might be several renderers.
Upvotes: 2