I want to make a booking rooms application with a schedule. I create a table classes in the database:
idseance, idsalle, IdProf, idmodule, day, 8H (type boolean), 10H (type boolean), 14h, 16H.
My code displays the table like this : <a href="http://s15.postimg.org/5m4l78hvf/Capture.png">http://s15.postimg.org/5m4l78hvf/Capture.png</a>[<a href="http://s15.postimg.org/5m4l78hvf/Capture.png" target="_blank" title="New Window">^</a>]
public class Fenetre extends JFrame {
private Statement st = null;
private ResultSet rs;
private JTable tableau;
connection con=new connection();
public Fenetre() throws SQLException{
st=con.getStatement();
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Emploi de Temps");
this.setSize(700, 500);
Object[][] data= new Object[5][4] ;
String Sql="Select * from seance";
rs=st.executeQuery(Sql);
while(rs.next()) {
switch (rs.getString("jour")){
case "lundi":
if (rs.getBoolean("8")){
data[0][0] = rs.getInt("Idmodule");
}else{
data[0][0] = new JButton("Ajouter");
}
if (rs.getBoolean("10")){
data[0][1] = rs.getInt("Idmodule");
}else {
data[0][1] = new JButton("Ajouter");
}
if(rs.getBoolean("14")){
data[0][2] = rs.getInt("Idmodule");
}else {
data[0][2] =new JButton("Ajouter");
}
if(rs.getBoolean("16")){
data[0][3] = rs.getInt("Idmodule");
}else {
data[0][3] =new JButton("Ajouter") ;
}
break;
case "mardi" :
if (rs.getBoolean("8")){
data[1][0] = rs.getInt("Idmodule");
}else {
data[1][0] = new JButton("Ajouter");
}
if(rs.getBoolean("10")){
data[1][1] = rs.getInt("Idmodule");
}else {
data[1][1] = new JButton("Ajouter");
}
if(rs.getBoolean("14")){
data[1][2] = rs.getInt("Idmodule");
}else {
data[1][2] = new JButton("Ajouter");
}
if(rs.getBoolean("16")){
data[1][3] = rs.getInt("Idmodule");
} else {
data[1][3] = new JButton("Ajouter");
}
}
}
String[] title = {"8h_10h","10h_12h"," 14h_16h","16h_18h"};
ZModel model = new ZModel(data, title);
this.tableau = new JTable(model);
changeSize(80, 80);
this.getContentPane().add(new JScrollPane(tableau), BorderLayout.CENTER);
this.tableau.setDefaultRenderer(JButton.class, new TableComponent());
}
public void changeSize(int width, int height){
TableColumn col;
for(int i = 0; i < tableau.getColumnCount(); i++){
col = tableau.getColumnModel().getColumn(i);
col.setPreferredWidth(width);
}
for(int i = 0; i < tableau.getRowCount(); i++){
tableau.setRowHeight(i, height);
}
}
class ZModel extends AbstractTableModel{
private Object[][] data;
private String[] title;
public ZModel(Object[][] data, String[] title){
this.data = data;
this.title = title;
}
public int getColumnCount() {
return this.title.length;
}
public int getRowCount() {
return this.data.length;
}
public Object getValueAt(int row, int col) {
return this.data[row][col];
}
public String getColumnName(int col) {
return this.title[col];
} public Class getColumnClass(int col){
return this.data[0][col].getClass();
}
public boolean isCellEditable(int row, int col){
if(getValueAt(0, col) instanceof JButton)
return false;
return true;
}
}
}
And the class TableComponent :
import java.awt.Component;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
public class TableComponent extends DefaultTableCellRenderer implements TableCellRenderer{
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setText((value != null) ? value.toString() : "Ajouter");
if (value instanceof JButton)
return (JButton) value;
else
return this;
}
}
The problem is that the button is not active and the table cell at [1][0] does not display the button.