Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
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>]

Java
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++){
            //On récupère le modèle de la colonne
            col = tableau.getColumnModel().getColumn(i);
            //On lui affecte la nouvelle valeur
            col.setPreferredWidth(width);
        }
        for(int i = 0; i < tableau.getRowCount(); i++){
            //On affecte la taille de la ligne à l'indice spécifié !
            tableau.setRowHeight(i, height);
        }
    }

    class ZModel extends AbstractTableModel{
        private Object[][] data;
        private String[] title;
        //Constructeur
        public ZModel(Object[][] data, String[] title){
            this.data = data;
            this.title = title;
        }

        //Retourne le nombre de colonnes
        public int getColumnCount() {
            return this.title.length;
        }

        //Retourne le nombre de lignes
        public int getRowCount() {
            return this.data.length;
        }

        //Retourne la valeur à l'emplacement spécifié
        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){
            //On retourne le type de la cellule à la colonne demandée
            //On se moque de la ligne puisque les types de données sont les mêmes quelle que soit la ligne
            //On choisit donc la première ligne
            return this.data[0][col].getClass();
        }
        //Retourne vrai si la cellule est éditable : celle-ci sera donc éditable
        public boolean isCellEditable(int row, int col){
            if(getValueAt(0, col) instanceof JButton)
            return false;
            return true;

        }
    }
} 

And the class TableComponent :
Java
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) {
        //Si la valeur de la cellule est un JButton, on transtype cette valeur
        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.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900