IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Composants Java Discussion :

Impossible d'ajouter une malheureuse combobox ds ma jtable


Sujet :

Composants Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    22
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mars 2007
    Messages : 22
    Par défaut Impossible d'ajouter une malheureuse combobox ds ma jtable
    Bonjour,

    depuis hier, je m'acharne à vouloir mettre une combobox ds uen de mes jtables... sans succès...

    Pourtant je suis allé voir le tuto de sun, des infos ici et la sur les différents forums et de en plus, j'ai modifie le fichier RenderDemo de sun , de façon à le réduire à sa plus simple expression sans pouvoir user du même principe ds mes jtable...

    voici le fichier de sun modifier:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    public class TableRenderDemo extends JPanel {
     
     
        public static JTable table;
     
        public TableRenderDemo() {
     
     
            table = new JTable(new MyTableModel());
     
            //Create the scroll pane and add the table to it.
            JScrollPane scrollPane = new JScrollPane(table);
     
            add(scrollPane);
        }
     
     
     
        public static void setUpSportColumn(
                                     TableColumn sportColumn) {
            //Set up the editor for the sport cells.
            JComboBox comboBox = new JComboBox();
            comboBox.addItem("Snowboarding");
            comboBox.addItem("Rowing");
            comboBox.addItem("Knitting");
            comboBox.addItem("Speed reading");
            comboBox.addItem("Pool");
            comboBox.addItem("None of the above");
            sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
     
     
        }
     
        class MyTableModel extends AbstractTableModel {
            private String[] columnNames = {"First Name",
                                            "Last Name",
                                            "Sport",
                                            "# of Years"};
            private Object[][] data ={
                {"Mary", "Campione",
                 "Snowboarding", new Integer(5)}
                 };
     
     
            public void setData(Object [][]newdata){
                this.data=newdata;
     
     
            }
     
     
            public int getColumnCount() {
                return columnNames.length;
            }
     
            public int getRowCount() {
                return data.length;
            }
     
            @Override
            public String getColumnName(int col) {
                return columnNames[col];
            }
     
            public Object getValueAt(int row, int col) {
                return data[row][col];
            }
     
     
            /*
             * Don't need to implement this method unless your table's
             * editable.
             */
            @Override
            public boolean isCellEditable(int row, int col) {
                //Note that the data/cell address is constant,
                //no matter where the cell appears onscreen.
                if (col < 2) {
                    return false;
                } else {
                    return true;
                }
            }
     
            /*
             * Don't need to implement this method unless your table's
             * data can change.
             */
            @Override
            public void setValueAt(Object value, int row, int col) {
                data[row][col] = value;
                fireTableCellUpdated(row, col);
     
            }
        }
     
        /**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
         */
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("TableRenderDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            //Create and set up the content pane.
            TableRenderDemo newContentPane = new TableRenderDemo();
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(newContentPane);
     
            frame.pack();
     
            frame.setVisible(true);
        }
     
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
     
                    setUpSportColumn(table.getColumnModel().getColumn(2));
     
     
                     MyTableModel model=(MyTableModel) TableRenderDemo.table.getModel();
             Object[][] data = {
                {"Mary", "Campione",
                 "Snowboarding", new Integer(5)},
                {"Alison", "Huml",
                 "Rowing", new Integer(3)},
                {"Kathy", "Walrath",
                 "Knitting", new Integer(2)},
                {"Sharon", "Zakhour",
                 "Speed reading", new Integer(20)},
                {"Philip", "Milne",
                 "Pool", new Integer(10)}
            };
             model.setData(data); 
                }
            });
        }
    }
    c'est, on ne peut plus simple et pourtant, je me tape la tête sur le bureau sans pouvoir adapter mes jtable...
    J'utilise netbeans et ds un de mes form, je crée une jtable en la construisant à l'aide d'un de mes tablemodel...
    j'essaye ds le constructeur de ma jframe, d'ajouter la combobox ds une colonne de ma jtable mais sans effet....

    voici mon model:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    public abstract class CfTableModel<E> extends AbstractTableModel {
     
        protected List<E> dataList;
     
        protected String[] columnNames;
        protected Class[] columnClasses;
        protected int[] columnRatios;
     
     
        public CaddyfoxTableModel() {
            buildColumnProperties();
            initDataList();
        }
     
     
        private void buildColumnProperties() {
            Object[][] columnProperties = getColumnProperties();
     
            columnNames = new String[columnProperties.length];
            columnClasses = new Class[columnProperties.length];
            columnRatios = new int[columnProperties.length];
     
            for (int i = 0; i < columnProperties.length; i++) {
                columnNames[i] = (String) columnProperties[i][0];
                columnClasses[i] = (Class) columnProperties[i][1];
                columnRatios[i] = (Integer) columnProperties[i][2];
            }
        }
     
        protected abstract Object[][] getColumnProperties();
     
     
        public void initDataList() {
            this.dataList = nonNullList(buildDataList());
     
            fireTableStructureChanged();
        }
     
        protected abstract List<E> buildDataList();
     
        public void add(E element) {
            dataList.add(element);
            fireTableStructureChanged();
        }
     
        public void setDataList(List<E> newList){
            this.dataList=newList;
            fireTableStructureChanged();
        }
     
        @Override
        public int getColumnCount() {
            return columnNames.length;
        }
     
        @Override
        public int getRowCount() {
            return dataList.size();
        }
     
        private static <E> List<E> nonNullList(List<E> list) {
            return (list != null) ? list : new ArrayList<E>();
        }
     
     
        @Override
        public String getColumnName(int column) {
            return columnNames[column];
        }
     
        public int[] getColumnRatios() {
            return columnRatios;
        }
     
        @Override
            public Class getColumnClass(int c) {
                return getValueAt(0, c).getClass();
            }
     
     
        public void addRow(Vector<E> o){
     
            dataList.addAll(o);
     
        }
     
        public List<E> getValues(){
            return this.dataList;
        }
     
        public abstract String getDefaultTitle();
     
    }
    ensuite je specialise le model:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    public class WeeklyOrderLineTableModel extends CfTableModel<WeeklyOrderLine> {
     
     
     
        @Override
        protected List<WeeklyOrderLine> buildDataList() {
            return new LinkedList<WeeklyOrderLine>();
        }
     
        @Override
        protected Object[][] getColumnProperties() {
            return new Object[][] {
                    {
                            "Product Bar Code", String.class, 10
                    },
                    {
                            "Product Name", String.class, 10
                    },
                    {
                            "Product Quantity", Integer.class, 4
                    },
                    {
                            "DLC/DLUO", String.class, 12
                    },
                    {
                            "Product Subtotal", Float.class, 7
                    },
                    {
                            "Palette", String.class, 7
                    }
     
     
            };
        }
     
        public Object getValueAt(int rowIndex, int columnIndex) {
            WeeklyOrderLine data = dataList.get(rowIndex);
     
             switch (columnIndex) {
     
                case -1:
                     return data;
     
                case 0:
                     return data.getProduct().getBarCode();
     
                case 1:
                     return data.getProduct().getFrDescription();
     
                case 2:
                     return data.getQuantity();
     
                 case 3:
                     SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yyyy");
                     return sdf.format(data.getDlc_dluo());
     
                 case 4:
                     return data.getSubTotal();
     
                 case 5:
                     return data.getPalette();
     
     
                default:
                    return null;
            }
        }
    /*
        @Override
        public Class getColumnClass(int c){
            System.out.println();
            return getValueAt(0,c).getClass();
        }*/
     
        @Override
        public String getDefaultTitle() {
            return "List all weekly order lines";
        }
     
        @Override
        public boolean isCellEditable(int row, int column){
     
            if(column==2 || column==5)
                return true;
            else
            return false;
     
     
        }
     
     
         public void setValueAt(Object value, int row, int column) {
           // dataList[row][column] = value;
     
            WeeklyOrderLine data = dataList.get(row);
     
             switch (column) {
     
                case 2:
                     data.setQuantity((Integer) value);
                     fireTableDataChanged();
     
                 case 5:
                     data.setPalette((String)value);
                     fireTableCellUpdated(row,column);
     
     
                default:
                   break;
            }
     
          }
     
     
         public void removeValue(WeeklyOrderLine line){
     
             dataList.remove(line);
     
     
             fireTableStructureChanged();
         }
     
    }
    ensuite ma jframe... une flèche indique ds le code, l'endroit ou j'essaye d'ajouter ma combobox, tandis que la fonction setUpSportColumn a été betement rajoutée ds la fin du fichier...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    public class WeeklyOrderCreate extends javax.swing.JFrame implements TableModelListener{
     
     
     
     
        /** Creates new form WeeklyOrderCreate */
        public WeeklyOrderCreate() {
            initComponents();
     
            jTable1.setAutoCreateRowSorter(true);
            jTable2.setAutoCreateRowSorter(true);
     
            jTextField1.setText("0.00");
     
            jTable2.getModel().addTableModelListener((TableModelListener) this);
     
     
     
     
            try{
                 List<Franchise>listFranchise=FranchiseDelegate.findFranchises();
     
                 for(Franchise franchise:listFranchise){
                    this.jComboBox1.addItem(franchise);
                 }
     
            }
            catch(Exception e){
                jOptionPane1.showMessageDialog(this,
                             "Cannot retrieve franchises: "+e.getMessage(),
                              "Weekly order create initalization gui error",
                                JOptionPane.ERROR_MESSAGE);
                return;
            }
     
     
     =========>   this.setUpSportColumn(jTable2, jTable2.getColumnModel().getColumn(5));
     
        }
     
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            jOptionPane1 = new javax.swing.JOptionPane();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            jButton3 = new javax.swing.JButton();
            jPanel1 = new javax.swing.JPanel();
            jLabel1 = new javax.swing.JLabel();
            jComboBox1 = new javax.swing.JComboBox();
            jLabel2 = new javax.swing.JLabel();
            jTextField1 = new javax.swing.JTextField();
            jLabel3 = new javax.swing.JLabel();
            jTextField2 = new javax.swing.JTextField();
            jButton4 = new javax.swing.JButton();
            jPanel2 = new javax.swing.JPanel();
            jScrollPane1 = new javax.swing.JScrollPane();
            jTable1 = new javax.swing.JTable();
            jPanel3 = new javax.swing.JPanel();
            jScrollPane2 = new javax.swing.JScrollPane();
            jTable2 = new javax.swing.JTable();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
            setAlwaysOnTop(true);
     
            jButton1.setText("Close");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
     
            jButton2.setText("Save");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
     
            jButton3.setText("Add");
            jButton3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton3ActionPerformed(evt);
                }
            });
     
            jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Summary"));
     
            jLabel1.setText("Franchise:");
     
            jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select" }));
     
            jLabel2.setText("Total:");
     
            jLabel3.setText("CMR:");
     
            org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel2)
                        .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel1))
                    .add(18, 18, 18)
                    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                        .add(jComboBox1, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE))
                    .add(18, 18, 18)
                    .add(jLabel3)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
     
            jPanel1Layout.linkSize(new java.awt.Component[] {jComboBox1, jTextField1, jTextField2}, org.jdesktop.layout.GroupLayout.HORIZONTAL);
     
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jPanel1Layout.createSequentialGroup()
                    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(jLabel1)
                        .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(jLabel3)
                        .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(18, 18, 18)
                    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(jLabel2)
                        .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
     
            jButton4.setText("Remove");
            jButton4.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton4ActionPerformed(evt);
                }
            });
     
            jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Products available"));
     
            jTable1.setModel(new ProductTableModel());
            jScrollPane1.setViewportView(jTable1);
     
            org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
            jPanel2.setLayout(jPanel2Layout);
            jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 790, Short.MAX_VALUE)
            );
            jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jPanel2Layout.createSequentialGroup()
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 238, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
     
            jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Products of the current weekly order"));
     
            jTable2.setModel(new WeeklyOrderLineTableModel());
            jScrollPane2.setViewportView(jTable2);
     
            org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
            jPanel3.setLayout(jPanel3Layout);
            jPanel3Layout.setHorizontalGroup(
                jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 790, Short.MAX_VALUE)
            );
            jPanel3Layout.setVerticalGroup(
                jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 241, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            );
     
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(layout.createSequentialGroup()
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER)
                                    .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(layout.createSequentialGroup()
                                        .add(jButton3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 168, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                        .add(105, 105, 105)
                                        .add(jButton4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 168, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                                .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addContainerGap())
                        .add(layout.createSequentialGroup()
                            .add(97, 97, 97)
                            .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(31, 31, 31)
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                .add(jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .add(jButton2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE))
                            .addContainerGap(103, Short.MAX_VALUE))))
            );
     
            layout.linkSize(new java.awt.Component[] {jPanel2, jPanel3}, org.jdesktop.layout.GroupLayout.HORIZONTAL);
     
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 269, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(24, 24, 24)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(jButton4)
                        .add(jButton3))
                    .add(18, 18, 18)
                    .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(18, 18, 18)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(layout.createSequentialGroup()
                            .add(jButton2)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 28, Short.MAX_VALUE)
                            .add(jButton1)
                            .addContainerGap(41, Short.MAX_VALUE))
                        .add(layout.createSequentialGroup()
                            .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap())))
            );
     
            layout.linkSize(new java.awt.Component[] {jPanel2, jPanel3}, org.jdesktop.layout.GroupLayout.VERTICAL);
     
            pack();
        }// </editor-fold>                        
     
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            this.dispose();
        }                                        
     
        private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
     
            int [] selectedRows=jTable1.getSelectedRows();
     
            if(selectedRows.length==0)
                {
                jOptionPane1.showMessageDialog(this,
                          "You have not choosen a product to add",
                             "Weekly order create warning",
                                     JOptionPane.WARNING_MESSAGE);
           return;
            }
     
     
            ProductTableModel model=(ProductTableModel)jTable1.getModel();
     
            Product product=(Product) model.getValueAt(selectedRows[0], -1);
     
            WeeklyOrderLine wol=new WeeklyOrderLine();
            wol.setProduct(product);
            wol.setQuantity(new Integer(0));
     
            wol.setDlc_dluo(product.getDlc_dluo());
            wol.setTva(product.getTva());
            wol.setPrice(product.getCostunit());
     
            WeeklyOrderLineTableModel modelTo=(WeeklyOrderLineTableModel) jTable2.getModel();
            modelTo.add(wol);
     
        }                                        
     
        private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            int [] selectedRows=jTable2.getSelectedRows();
     
            if(selectedRows.length==0)
                {
                jOptionPane1.showMessageDialog(this,
                          "You have not choosen a product to remove",
                             "Weekly order create warning",
                                     JOptionPane.WARNING_MESSAGE);
           return;
            }
     
     
            WeeklyOrderLineTableModel model=(WeeklyOrderLineTableModel)jTable2.getModel();
     
            WeeklyOrderLine wol=(WeeklyOrderLine) model.getValueAt(selectedRows[0], -1);
     
            List<WeeklyOrderLine> lines=model.getValues();
            lines.remove(wol);
     
            model.setDataList(lines);
        }                                        
     
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
     
     
            if(this.jComboBox1.getSelectedItem().getClass().equals(String.class))
                {
                jOptionPane1.showMessageDialog(this,
                          "You have not choosen a franchise",
                             "Weekly order create warning",
                                     JOptionPane.WARNING_MESSAGE);
           return;
            }
     
            if(jTextField2.getText()==null || "".equals(jTextField2.getText()))
                {
                jOptionPane1.showMessageDialog(this,
                          "You have not enter a crm",
                             "Weekly order create warning",
                                     JOptionPane.WARNING_MESSAGE);
           return;
            }
     
            WeeklyOrder weeklyOrder=new WeeklyOrder();
     
            weeklyOrder.setCmr(jTextField2.getText());
     
            weeklyOrder.setFranchise((Franchise) this.jComboBox1.getSelectedItem());
     
            WeeklyOrderLineTableModel model=(WeeklyOrderLineTableModel) jTable2.getModel();
     
            weeklyOrder.setWeeklyOrderLines(model.getValues());
     
     
     
             try{
             WeeklyOrderDelegate.createWeeklyOrder(weeklyOrder);
            }
            catch(Exception e){
                jOptionPane1.showMessageDialog(this,
                             "Your create is cancelled: "+e.getMessage(),
                              "Weekly order create error",
                                JOptionPane.ERROR_MESSAGE);
                return;
            }
     
     
     
     
           jOptionPane1.showMessageDialog(this,
        "Create successfull",
        "Weekly order create", JOptionPane.INFORMATION_MESSAGE);
     
           this.dispose();
     
     
        }                                        
     
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new WeeklyOrderCreate().setVisible(true);
                }
            });
        }
     
        public void tableChanged(TableModelEvent e){
     
            Float total=0f;
     
     
     
            WeeklyOrderLineTableModel model=(WeeklyOrderLineTableModel)jTable2.getModel();
     
            List<WeeklyOrderLine>lines=model.getValues();
     
     
            for(WeeklyOrderLine line:lines)
                total+=line.getSubTotal();
     
            System.out.println("total de: "+total);
     
            this.jTextField1.setText(total.toString());
     
        }
     
          public static void setUpSportColumn(JTable table,
                                     TableColumn sportColumn) {
            //Set up the editor for the sport cells.
            JComboBox comboBox = new JComboBox();
            comboBox.addItem("Snowboarding");
            comboBox.addItem("Rowing");
            comboBox.addItem("Knitting");
            comboBox.addItem("Speed reading");
            comboBox.addItem("Pool");
            comboBox.addItem("None of the above");
            sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
     
            //Set up tool tips for the sport cells.
            DefaultTableCellRenderer renderer =
                    new DefaultTableCellRenderer();
            renderer.setToolTipText("Click for combo box");
            sportColumn.setCellRenderer(renderer);
        }
     
     
     
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JButton jButton4;
        private javax.swing.JComboBox jComboBox1;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JOptionPane jOptionPane1;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JPanel jPanel2;
        private javax.swing.JPanel jPanel3;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JScrollPane jScrollPane2;
        private javax.swing.JTable jTable1;
        private javax.swing.JTable jTable2;
        private javax.swing.JTextField jTextField1;
        private javax.swing.JTextField jTextField2;
        // End of variables declaration                   
     
    }
    Bref, ce truc a l'air, si simple ds le premier fichier, que je ne vois vrmt pas pq, je n'arrive pas à l'utiliser ds mes jtables.
    si quelqu'un a une piste, je ne saurais que le remercier en avance...

  2. #2
    Membre émérite Avatar de fraco
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    750
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Juin 2006
    Messages : 750
    Par défaut
    comme ça ?

    Avoir un JScrollPane à l'endroit qui me convient dans le JFrame, et

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
     
     private void formComponentShown(java.awt.event.ComponentEvent evt) {           
            JXTable ta = new SpecialTable(
                new Object[][] {                         
                    new Object[] {"Dupont", "Marcel", ""},
                    new Object[] {"Durand", "Anne", ""}
                },
                new Object[] {"A", "B", "C"}
            );
            Dimension dim = new Dimension();
            dim.setSize(100,100);
            ta.setPreferredSize(dim);
            ta.setOpaque(true);        
           java.awt.Color coul;
            coul = new java.awt.Color(0,255,255);           
            ta.setBackground(coul);      
            jScrollPane1.setPreferredSize(ta.getPreferredSize());
            jScrollPane1.setViewportView(ta);      
            jScrollPane1.validate();     
        }
    Créer une classe
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
     
     class SpecialTable extends  JXTable {   
         public SpecialTable(Object[][] rowData, Object[] columnNames) {
              super(rowData, columnNames);
         }    
        JComboBox comboBoxPolitesse = new JComboBox(
            new String[] {"M.", "Mme",  "Mr", "Mrs"}
        ); 
        public TableCellEditor getCellEditor(int row, int column) {
            TableCellEditor tce = null;
             if (column == 2) {
                tce = new DefaultCellEditor( comboBoxPolitesse);            
            }
            else {
                tce = super.getCellEditor(row, column); 
            }
            return tce;                
        }
     }

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    22
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mars 2007
    Messages : 22
    Par défaut
    Merci pour les infos,

    je viens de retester mon code, et le combobox marche parfaitement à l'unique condition que mes données soit initialisées dans mon tablemodel...

    par exemple si j'ajoute un objet dans mes Data de mon modeltable, en cliquant sur un bouton d une frame, je perds l editeur de cell (combobox).

    j'ai identifie la source de ce problème: ma fonction add ajoute un element a ma liste data puis je fais un fireTableStructureChanged et cela me fait perdre le cell editor, je ne sais pas vraiment pourquoi, il faut que je me penche d'avantage sur ce fire...

Discussions similaires

  1. Impossible d'ajouter une imprimante !
    Par Le Pharaon dans le forum Windows XP
    Réponses: 3
    Dernier message: 17/11/2015, 18h27
  2. Impossible d'ajouter une valeur à une combobox
    Par Tikalins dans le forum Macros et VBA Excel
    Réponses: 4
    Dernier message: 02/07/2015, 15h37
  3. [patition magic 8.0] impossible d'ajouter une partition
    Par sorari dans le forum Autres Logiciels
    Réponses: 2
    Dernier message: 30/06/2007, 15h47
  4. Réponses: 1
    Dernier message: 12/03/2007, 18h10

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo