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 :

[JTable] Fonction getSelectedRow()


Sujet :

Composants Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre habitué
    Inscrit en
    Septembre 2004
    Messages
    8
    Détails du profil
    Informations forums :
    Inscription : Septembre 2004
    Messages : 8
    Par défaut [JTable] Fonction getSelectedRow()
    Bonjour,

    J'ai un problème avec cette fonction .. Qd je fait table.getRowCount() il me retourne bien le nombre de lignes ds ma JTABLE, par contre je selectionne une ligne mais la fonction getSelectedRow() me retourne -1..

    Quelqu'un a une idée ? Je sèche..

    Merci

  2. #2
    Membre confirmé
    Inscrit en
    Juillet 2002
    Messages
    28
    Détails du profil
    Informations forums :
    Inscription : Juillet 2002
    Messages : 28
    Par défaut
    Montre nous un peu plus de code....

    parce que là a part de dire que normalement ça devrait marcher...

  3. #3
    Membre habitué
    Inscrit en
    Septembre 2004
    Messages
    8
    Détails du profil
    Informations forums :
    Inscription : Septembre 2004
    Messages : 8
    Par défaut
    Bah justement mon code est assez complexe, et j'ai peur d'embrouiller plus qu'autre chose..

    Dans ma méthode init :
    tableModel = new JDBCAdapter();
    sorter = new TableSorter(tableModel);
    table = Utils.arrangeTable(table, sorter, listeToolTipsArt);
    JScrollPane JSP = new JScrollPane(table);


    J'ai un tree panel a gauche et un detailpanel a droite, lorsque je selectionne une feuille mon tableau est rafraichi avec des données de la base..

    table est declaree en tant que JTable..

    et jai une action listener (3 boutons , ajouter, modifier, supprimer),..

    sinon voici le code de ma 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
    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
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600
    601
    602
    603
    604
    605
    606
    607
    608
    609
    610
    611
    612
    613
    614
    615
    616
    617
    618
    619
    620
    621
    622
    623
    624
    625
    626
    627
    628
    629
    630
    631
    632
    633
    634
    635
    636
    637
    638
    639
    640
    641
    642
    643
    644
    645
    646
    647
    648
    649
    650
    651
    652
    653
    654
    655
    656
    657
    658
    659
    660
    661
    662
    663
    664
    665
    666
    667
    668
    669
    670
    671
    672
    673
    674
    675
    676
    677
    678
    679
    680
    681
    682
    683
    684
    685
    686
    687
    688
    689
    690
    691
    692
    693
    694
    695
    696
    697
    698
    699
    700
    701
    702
    703
    704
    705
    706
    707
    708
    709
    710
    711
    712
    713
    714
    715
    716
    717
    718
    719
    720
    721
    722
    723
    724
    725
    726
    727
    728
    729
    730
    731
    732
    733
    734
    735
    736
    737
    738
    739
    740
    741
    742
    743
    744
    745
    746
    747
    748
    749
    750
    751
    752
    753
    754
    755
    756
    757
    758
    759
    760
    761
    762
    763
    764
    765
    766
    767
    768
    769
    770
    771
    772
    773
    774
    775
    776
    777
    778
    779
    780
    781
    782
    783
    784
    785
    786
    787
    788
    789
    790
    791
    792
    793
    794
    795
    796
    797
    798
    799
    800
    801
    802
    803
    804
    805
    806
    807
    808
    809
    810
    811
    812
    813
    814
    815
    816
    817
    818
    819
    820
    821
    822
    823
    824
    825
    826
    827
    828
    829
    830
    831
    832
    833
    834
    835
    package view;
     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.util.Iterator;
     
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.SwingConstants;
    import javax.swing.border.BevelBorder;
    import javax.swing.border.TitledBorder;
    import javax.swing.table.DefaultTableModel;
     
    import bo.AnneeBudgetaire;
    import bo.Article;
    import bo.ArticleDB;
    import bo.Budget;
    import bo.BudgetDB;
    import bo.Hierarchie;
    import bo.InfosArticle;
     
    import appConstants.Constants;
     
    import util.JDBCAdapter;
    import util.TableSorter;
    import util.Utils;
     
    public class BudgetDetailPanel extends JPanel {
     
    	private BudgetTreePanel treePanel;
     
    	private final BudgetRootPanel rootPanel;
     
    	private boolean isDataChanged = false;
    	/*
    	 * Déclaration des GUI
    	 * 
    	 */
    	//private JPanel principalPanel;
    	private JPanel centerPane;
    	private JPanel northPane;
    	private JPanel southPane;
    	private JPanel jPanel1;
    	private JPanel jPanel1_1;
    	private JPanel jPanel1Article;
    	private JPanel jPanel1Button;
    	private JPanel jPanel2;
    	private JPanel jPanel3;
    	private JPanel jPanel4;
    	private JPanel jPanelDEtail;
    	private JPanel jPanelJustification;
     
    	private JButton jButtonPrint =
    		new JButton("Imprimer rapport",Utils.getImageIcon("/toolbarButtonGraphics/general/Print"));
    	private JButton jButtonSave =
    		new JButton("Enregistrer",Utils.getImageIcon("/toolbarButtonGraphics/general/Save"));
     
    	private JButton addButton =
    		new JButton("Ajouter un détail",Utils.getImageIcon("/toolbarButtonGraphics/general/New"));
    	private JButton deleteButton =
    		new JButton("Supprimer un détail",Utils.getImageIcon("/toolbarButtonGraphics/general/Delete"));
    	private JButton changeButton =
    		new JButton("Editer le détail",Utils.getImageIcon("/toolbarButtonGraphics/general/Edit"));
     
    	private JButton openButton =
    		new JButton("Sélection Fichier Justification",Utils.getImageIcon("/toolbarButtonGraphics/general/Open"));
    	private JButton silButton =
    		new JButton("Rétablir justification",Utils.getImageIcon("/toolbarButtonGraphics/general/Delete"));
     
    	private JLabel jLabelBV;
     
    	private JLabel jLabelCP;
     
    	private JLabel jLabelDetail;
    	private JLabel jLabelJustification;
    	private JLabel jLabelNumArticle;
    	private JLabel jLabelPB;
     
    	private JLabel jLabelPropBudg;
     
    	private JLabel jLabelSection;
     
    	private JLabel jLabelStatut;
    	private JLabel jLabelStatut1;
     
    	private JScrollPane jScrollPaneArticle;
    	private JScrollPane jScrollPaneDetail;
    	private JScrollPane jScrollPaneJustification;
     
    	//private JTable jTableDetail;
     
    	private JTextArea jTextAreaArticle;
    	private JTextArea jTextAreaJustification;
     
    	private JTextField jTextFieldBV;
    	private JTextField jTextFieldCP;
    	private JTextField jTextFieldNumArticle;
    	private JTextField jTextFieldPB;
    	private JTextField jLabelSection1;
    	private JTextField jLabelBV1;
    	private JTextField jLabelCP1;
    	private JTextField jLabelPB1;
    	private JTextField jLabelPropBudg1;
     
     
    	private Hierarchie hierarchie;
    	private Article article;
    	private AnneeBudgetaire anneeBudgetaire;
     
    	private boolean isFromDB = false;
     
     
    	//JTable
    	public static JDBCAdapter tableModel;
    	public JTable table = new JTable();
    	public static TableSorter sorter;	
     
    	public String[] listeToolTipsArt = { "ID_DETAIL", "Libellé", "Montant" };
    	public String _sqlArtDet = "";	
    	/**
             * @return
             */
    	public boolean isFromDB() {
    		return isFromDB;
    	}
     
    	/**
             * @param isFromDB
             */
    	public void setFromDB(boolean isFromDB) {
    		this.isFromDB = isFromDB;
    	}
     
    	/**
             * @return
             */
    	public AnneeBudgetaire getAnneeBudgetaire() {
    		return anneeBudgetaire;
    	}
     
    	/**
             * @param anneeBudgetaire
             */
    	public void setAnneeBudgetaire(AnneeBudgetaire anneeBudgetaire) {
    		this.anneeBudgetaire = anneeBudgetaire;
    	}
     
    	/**
             * @return
             */
    	public Article getArticle() {
    		return article;
    	}
     
    	/**
             * @param article
             */
    	public void setArticle(Article article) {
    		this.article = article;
    	}
     
    	/**
             * @return
             */
    	public Hierarchie getHierarchie() {
    		return hierarchie;
    	}
     
    	/**
             * @param hierarchie
             */
    	public void setHierarchie(Hierarchie hierarchie) {
    		this.hierarchie = hierarchie;
    	}
     
    	public BudgetDetailPanel(BudgetRootPanel rootPanel) {
    		super();
    		this.rootPanel = rootPanel;
    		init();
    	}
     
    	private void init() {
    		setLayout(new BorderLayout());
    		GridBagConstraints gridBagConstraints;
     
    		northPane = new JPanel();
    		jLabelPropBudg = new JLabel();
    		jLabelPropBudg1 = new JTextField();
    		southPane = new JPanel();
    		jLabelStatut = new JLabel();
    		jLabelStatut1 = new JLabel();
    		centerPane = new JPanel();
    		jPanel1 = new JPanel();
    		jPanel1_1 = new JPanel();
    		jPanel1Article = new JPanel();
    		jTextFieldNumArticle = new JTextField();
    		jLabelSection1 = new JTextField();
    		jLabelSection = new JLabel();
    		jLabelNumArticle = new JLabel();
    		jScrollPaneArticle = new JScrollPane();
    		jTextAreaArticle = new JTextArea();
    		jPanel1Button = new JPanel();
    		jPanel2 = new JPanel();
    		jLabelBV = new JLabel();
    		jLabelCP = new JLabel();
    		jLabelPB = new JLabel();
    		jLabelBV1 = new JTextField();
    		jLabelPB1 = new JTextField();
    		jLabelCP1 = new JTextField();
    		jTextFieldPB = new JTextField();
    		jTextFieldCP = new JTextField();
    		jTextFieldBV = new JTextField();
    		jPanel3 = new JPanel();
    		jPanelJustification = new JPanel();
    		jLabelJustification = new JLabel();
    		jScrollPaneJustification = new JScrollPane();
    		jTextAreaJustification = new JTextArea();
    		jPanel4 = new JPanel();
    		jPanelDEtail = new JPanel();
    		jLabelDetail = new JLabel();
    		jScrollPaneDetail = new JScrollPane();
    		//jTableDetail = new JTable();
     
    		jPanel1_1.setLayout(new BorderLayout());
     
    		jLabelPropBudg.setFont(new Font("Verdana", 1, 14));
    		jLabelPropBudg.setText("Propositions Budg\u00e9taires");
    		northPane.add(jLabelPropBudg);
     
    		jLabelPropBudg1.setFont(new Font("Verdana", 1, 14));
    		jLabelPropBudg1.setForeground(new Color(255, 0, 0));
    		jLabelPropBudg1.setBackground(null);
    		jLabelPropBudg1.setBorder(null);
    		jLabelPropBudg1.setText(
    			String.valueOf(Constants.AnneeBudgetaireCourant.getAnnee()));
    		northPane.add(jLabelPropBudg1);
     
    		add(northPane, BorderLayout.NORTH);
     
    		southPane.setLayout(new BorderLayout());
     
    		southPane.setBorder(new BevelBorder(BevelBorder.RAISED));
    		jLabelStatut.setFont(new Font("Verdana", 1, 10));
    		jLabelStatut.setHorizontalAlignment(SwingConstants.LEFT);
    		jLabelStatut.setText("Statut :");
    		southPane.add(jLabelStatut, BorderLayout.WEST);
     
    		jLabelStatut1.setFont(new Font("Verdana", 1, 10));
    		jLabelStatut1.setForeground(new Color(255, 0, 0));
    		jLabelStatut1.setText("Demande enregistr\u00e9e...");
    		southPane.add(jLabelStatut1, BorderLayout.CENTER);
     
    		add(southPane, BorderLayout.SOUTH);
     
    		centerPane.setLayout(new BoxLayout(centerPane, BoxLayout.Y_AXIS));
     
    		jPanel1.setLayout(new BorderLayout());
     
    		jPanel1.setBorder(
    			new TitledBorder(
    				null,
    				"Informations Article",
    				TitledBorder.DEFAULT_JUSTIFICATION,
    				TitledBorder.DEFAULT_POSITION,
    				new Font("Verdana", 1, 12)));
    		jPanel1Article.setLayout(new GridBagLayout());
     
    		jTextFieldNumArticle.setColumns(10);
    		jTextFieldNumArticle.setFont(new Font("Verdana", 1, 14));
    		jTextFieldNumArticle.setForeground(new Color(255, 0, 0));
    		jTextFieldNumArticle.setBorder(null);
    		gridBagConstraints = new GridBagConstraints();
    		gridBagConstraints.gridx = 1;
    		gridBagConstraints.gridy = 1;
    		gridBagConstraints.gridwidth = 4;
    		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    		gridBagConstraints.anchor = GridBagConstraints.EAST;
    		jPanel1Article.add(jTextFieldNumArticle, gridBagConstraints);
     
    		jLabelSection1.setFont(new Font("Verdana", 1, 15));
    		jLabelSection1.setForeground(new Color(51, 102, 255));
    		jLabelSection1.setBackground(null);
    		jLabelSection1.setBorder(null);
    		jLabelSection1.setText("");
    		gridBagConstraints = new GridBagConstraints();
    		gridBagConstraints.gridx = 1;
    		gridBagConstraints.gridy = 0;
    		gridBagConstraints.gridwidth = 4;
    		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    		gridBagConstraints.anchor = GridBagConstraints.EAST;
    		jPanel1Article.add(jLabelSection1, gridBagConstraints);
     
    		jLabelSection.setFont(new Font("Verdana", 1, 10));
    		jLabelSection.setText("Section :");
    		gridBagConstraints = new GridBagConstraints();
    		gridBagConstraints.gridx = 0;
    		gridBagConstraints.gridy = 0;
    		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    		gridBagConstraints.anchor = GridBagConstraints.WEST;
    		jPanel1Article.add(jLabelSection, gridBagConstraints);
     
    		jLabelNumArticle.setFont(new Font("Verdana", 1, 10));
    		jLabelNumArticle.setText("Num\u00e9ro Article :");
    		gridBagConstraints = new GridBagConstraints();
    		gridBagConstraints.gridx = 0;
    		gridBagConstraints.gridy = 1;
    		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    		gridBagConstraints.anchor = GridBagConstraints.WEST;
    		jPanel1Article.add(jLabelNumArticle, gridBagConstraints);
     
    		jScrollPaneArticle.setVerticalScrollBarPolicy(
    			JScrollPane.VERTICAL_SCROLLBAR_NEVER);
    		jScrollPaneArticle.setPreferredSize(new Dimension(500, 150));
    		jTextAreaArticle.setFont(new Font("Verdana", 1, 12));
    		jTextAreaArticle.setForeground(new Color(255, 0, 0));
    		jTextAreaArticle.setWrapStyleWord(true);
    		jTextAreaArticle.setLineWrap(true);
    		jScrollPaneArticle.setViewportView(jTextAreaArticle);
     
    		gridBagConstraints = new GridBagConstraints();
    		gridBagConstraints.gridx = 1;
    		gridBagConstraints.gridy = 2;
    		gridBagConstraints.gridwidth = 14;
    		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    		jPanel1Article.add(jScrollPaneArticle, gridBagConstraints);
     
    		jPanel1.add(jPanel1Article, BorderLayout.WEST);
     
    		jButtonSave.setToolTipText("Enregistrer");
    		jButtonSave.addActionListener(new BudgetActionListener());
     
    		jButtonPrint.setToolTipText("Imprimer");
    		jButtonPrint.addActionListener(new BudgetActionListener());
     
    		jButtonSave.setFont(new Font("Verdana", 1, 10));
    		jButtonPrint.setFont(new Font("Verdana", 1, 10));
     
    		jPanel1Button.add(Utils.getCommandRow(jButtonSave,jButtonPrint), BorderLayout.CENTER);		
     
     
    		jPanel1_1.add(jPanel1Button, BorderLayout.EAST);
    		jPanel1.add(jPanel1_1, BorderLayout.NORTH);
     
    		centerPane.add(jPanel1);
     
    		jPanel2.setLayout(new GridBagLayout());
     
    		jPanel2.setBorder(
    			new TitledBorder(
    				null,
    				"Informations Budget",
    				TitledBorder.DEFAULT_JUSTIFICATION,
    				TitledBorder.DEFAULT_POSITION,
    				new Font("Verdana", 1, 12)));
    		jLabelBV.setFont(new Font("Verdana", 1, 12));
    		jLabelBV.setHorizontalAlignment(SwingConstants.CENTER);
    		jLabelBV.setText("Budget Vot\u00e9");
    		gridBagConstraints = new GridBagConstraints();
    		gridBagConstraints.gridx = 3;
    		gridBagConstraints.gridy = 0;
    		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    		gridBagConstraints.ipadx = 34;
    		gridBagConstraints.insets = new Insets(0, 0, 0, 75);
    		jPanel2.add(jLabelBV, gridBagConstraints);
     
    		jLabelCP.setFont(new Font("Verdana", 1, 12));
    		jLabelCP.setHorizontalAlignment(SwingConstants.CENTER);
    		jLabelCP.setText("Compte Provisoire");
    		gridBagConstraints = new GridBagConstraints();
    		gridBagConstraints.gridx = 1;
    		gridBagConstraints.gridy = 0;
    		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    		gridBagConstraints.insets = new Insets(0, 0, 0, 75);
    		jPanel2.add(jLabelCP, gridBagConstraints);
     
    		jLabelPB.setFont(new Font("Verdana", 1, 12));
    		jLabelPB.setHorizontalAlignment(SwingConstants.CENTER);
    		jLabelPB.setText("Projet de Budget");
    		gridBagConstraints = new GridBagConstraints();
    		gridBagConstraints.gridx = 5;
    		gridBagConstraints.gridy = 0;
    		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    		gridBagConstraints.insets = new Insets(0, 0, 0, 75);
    		jPanel2.add(jLabelPB, gridBagConstraints);
     
    		jLabelBV1.setFont(new Font("Verdana", 1, 15));
    		jLabelBV1.setForeground(new Color(51, 102, 255));
    		jLabelBV1.setBackground(null);
    		jLabelBV1.setBorder(null);
    		jLabelBV1.setText("2004");
    		gridBagConstraints = new GridBagConstraints();
    		gridBagConstraints.gridx = 3;
    		gridBagConstraints.gridy = 1;
    		gridBagConstraints.insets = new Insets(0, 0, 0, 75);
    		jPanel2.add(jLabelBV1, gridBagConstraints);
     
    		jLabelPB1.setFont(new Font("Verdana", 1, 15));
    		jLabelPB1.setForeground(new Color(51, 102, 255));
    		jLabelPB1.setBackground(null);
    		jLabelPB1.setBorder(null);
    		jLabelPB1.setText(
    			String.valueOf(Constants.AnneeBudgetaireCourant.getAnnee()));
    		gridBagConstraints = new GridBagConstraints();
    		gridBagConstraints.gridx = 5;
    		gridBagConstraints.gridy = 1;
    		gridBagConstraints.insets = new Insets(0, 0, 0, 75);
    		jPanel2.add(jLabelPB1, gridBagConstraints);
     
    		jLabelCP1.setFont(new Font("Verdana", 1, 15));
    		jLabelCP1.setForeground(new Color(51, 102, 255));
    		jLabelCP1.setBackground(null);
    		jLabelCP1.setBorder(null);
    		jLabelCP1.setText("2003");
    		gridBagConstraints = new GridBagConstraints();
    		gridBagConstraints.gridx = 1;
    		gridBagConstraints.gridy = 1;
    		gridBagConstraints.insets = new Insets(0, 0, 0, 75);
    		jPanel2.add(jLabelCP1, gridBagConstraints);
     
    		jTextFieldPB.setColumns(20);
    		jTextFieldPB.setFont(new Font("Verdana", 1, 10));
    		jTextFieldPB.setForeground(new Color(255, 0, 0));
    		jTextFieldPB.setHorizontalAlignment(JTextField.CENTER);		
    		gridBagConstraints = new GridBagConstraints();
    		gridBagConstraints.gridx = 5;
    		gridBagConstraints.gridy = 2;
    		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    		gridBagConstraints.insets = new Insets(0, 0, 0, 75);
    		jPanel2.add(jTextFieldPB, gridBagConstraints);
     
    		jTextFieldCP.setColumns(20);
    		jTextFieldCP.setFont(new Font("Verdana", 1, 10));
    		jTextFieldCP.setForeground(new Color(255, 0, 0));
    		jTextFieldCP.setHorizontalAlignment(JTextField.CENTER);		
    		gridBagConstraints = new GridBagConstraints();
    		gridBagConstraints.gridx = 1;
    		gridBagConstraints.gridy = 2;
    		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    		gridBagConstraints.insets = new Insets(0, 0, 0, 75);
    		jPanel2.add(jTextFieldCP, gridBagConstraints);
     
    		jTextFieldBV.setColumns(20);
    		jTextFieldBV.setFont(new Font("Verdana", 1, 10));
    		jTextFieldBV.setForeground(new Color(255, 0, 0));
    		jTextFieldBV.setHorizontalAlignment(JTextField.CENTER);		
    		gridBagConstraints = new GridBagConstraints();
    		gridBagConstraints.gridx = 3;
    		gridBagConstraints.gridy = 2;
    		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    		gridBagConstraints.insets = new Insets(0, 0, 0, 75);
    		jPanel2.add(jTextFieldBV, gridBagConstraints);
     
    		centerPane.add(jPanel2);
     
    		jPanel3.setLayout(new BorderLayout());
     
    		jPanel3.setBorder(
    			new TitledBorder(
    				null,
    				"Informations sur la justification de l'article",
    				TitledBorder.DEFAULT_JUSTIFICATION,
    				TitledBorder.DEFAULT_POSITION,
    				new Font("Verdana", 1, 12)));
    		jPanelJustification.setLayout(new GridBagLayout());
     
    		jLabelJustification.setFont(new Font("Verdana", 1, 10));
    		jLabelJustification.setText("Justification :");
    		gridBagConstraints = new GridBagConstraints();
    		gridBagConstraints.gridx = 0;
    		gridBagConstraints.gridy = 0;
    		gridBagConstraints.insets = new Insets(0, 0, 0, 15);
    		gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
    		jPanelJustification.add(jLabelJustification, gridBagConstraints);
     
    		jScrollPaneJustification.setPreferredSize(new Dimension(500, 150));
    		jTextAreaJustification.setFont(new Font("Verdana", 1, 12));
    		jTextAreaJustification.setForeground(new Color(255, 0, 0));
    		jTextAreaJustification.setLineWrap(true);
    		jScrollPaneJustification.setViewportView(jTextAreaJustification);
     
    		jPanelJustification.add(
    			jScrollPaneJustification,
    			new GridBagConstraints());
     
    		jPanel3.add(jPanelJustification, BorderLayout.WEST);
    		//Ajout Bouton selection fichier
    		fc = new JFileChooser();
    		openButton.setToolTipText("Sélectionner un fichier de justification");
    		openButton.addActionListener(new OpenActionListener());
     
     
    		silButton.setToolTipText("Cliquer pour effacer la justification et la modifier");
    		silButton.addActionListener(new OpenActionListener());		
     
    		openButton.setFont(new Font("Verdana", 1, 10));
    		silButton.setFont(new Font("Verdana", 1, 10));
    		openButton.setHorizontalAlignment(SwingConstants.LEFT);
    		silButton.setHorizontalAlignment(SwingConstants.LEFT);				
    		jPanel3.add(Utils.getCommandColumn(openButton,silButton), BorderLayout.CENTER);	
    		//Fin
     
    		centerPane.add(jPanel3);
     
    		jPanel4.setLayout(new BorderLayout());
     
    		jPanel4.setBorder(
    			new TitledBorder(
    				null,
    				"Informations sur les d\u00e9tails \u00e9ventuels de l'Article",
    				TitledBorder.DEFAULT_JUSTIFICATION,
    				TitledBorder.DEFAULT_POSITION,
    				new Font("Verdana", 1, 12)));
    		jPanelDEtail.setLayout(new GridBagLayout());
     
    		jLabelDetail.setFont(new Font("Verdana", 1, 10));
    		jLabelDetail.setText("Detail :");
    		gridBagConstraints = new GridBagConstraints();
    		gridBagConstraints.gridx = 0;
    		gridBagConstraints.gridy = 0;
    		gridBagConstraints.insets = new Insets(0, 0, 0, 52);
    		gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
    		jPanelDEtail.add(jLabelDetail, gridBagConstraints);
     
    		tableModel = new JDBCAdapter();
    		sorter = new TableSorter(tableModel);
    		table = Utils.arrangeTable(table, sorter, listeToolTipsArt);
    		JScrollPane JSP = new JScrollPane(table);
    		JSP.setPreferredSize(new Dimension(500, 200));
    		jPanelDEtail.add(JSP, new GridBagConstraints());
     
     
    		jPanel4.add(jPanelDEtail, BorderLayout.WEST);
     
    		addButton.setToolTipText("Ajouter un Détail");
    		deleteButton.setToolTipText("Supprimer le Détail");
    		changeButton.setToolTipText("Editer le Détail");
     
    		addButton.setFont(new Font("Verdana", 1, 10));
    		deleteButton.setFont(new Font("Verdana", 1, 10));
    		changeButton.setFont(new Font("Verdana", 1, 10));
     
    		addButton.setHorizontalAlignment(SwingConstants.LEFT);    
    		deleteButton.setHorizontalAlignment(SwingConstants.LEFT);    
    		changeButton.setHorizontalAlignment(SwingConstants.LEFT);    
     
    		addButton.addActionListener(new ArticleDetailListener());
    		deleteButton.addActionListener(new ArticleDetailListener());
    		changeButton.addActionListener(new ArticleDetailListener());
     
    		jPanel4.add(
    			Utils.getCommandColumn(addButton, changeButton, deleteButton),
    			BorderLayout.CENTER);
     
    		centerPane.add(jPanel4);
     
    		add(centerPane, BorderLayout.CENTER);
     
    	}
     
    	public void setTreePanel(BudgetTreePanel treePanel) {
    		this.treePanel = treePanel;
    	}
     
    	public BudgetTreePanel getTreePanel() {
    		return this.treePanel;
    	}
     
    	public BudgetRootPanel getRootPanel() {
    		return rootPanel;
    	}
     
    	public boolean isDataChanged() {
    		return false;
    	}
     
    	public void setDatachanged(boolean b) {
    		isDataChanged = b;
     
    	}
     
    	/*
    	 * @see SimplePanelInterface#undoChanges()
    	 */
    	public void undoChanges() throws Exception {
     
    	}
     
    	/*
    	 * @see PanelInterface#doRefresh()
    	 */
    	public void doSave() throws Exception {
    		System.out.println("yes save");
     
    		long montant = 0;
    		montant = Long.parseLong(jTextFieldPB.getText());
    		Budget b = new Budget(getHierarchie(),getAnneeBudgetaire(),getArticle(),montant,jTextAreaJustification.getText());
     
    		if (isFromDB()==true) {
    			BudgetDB.getInstance().update(b);
    		} else {
    			BudgetDB.getInstance().insert(b);
    			setFromDB(true);
    		}
     
    	}
     
    	/*
    	 * @see PanelInterface#doRefresh()
    	 */
    	public void doPrint() throws Exception {
    		System.out.println("yes print");
    	}
     
    	public void setInfosArticle(String article) {
    		//Recuperation des donnees Article
     
    		setHierarchie(BudgetMain.getLoggeduser());
    		setArticle(ArticleDB.getInstance().getArticleByString(article));
    		setAnneeBudgetaire(Constants.AnneeBudgetaireCourant);
     
    		InfosArticle ia =
    			BudgetDB.getInstance().getBudget(
    				getHierarchie().getIdHierarchie(),
    				getArticle().getIdArticle(),
    				getAnneeBudgetaire().getIdAnneeBudgetaire());
     
    		//Populate InfosArticle
    		//EnableChamps
    		setEditable(true);
    		if (ia != null) {
    			//Populate InfosArticle
    			jTextAreaArticle.setText(ia.getJTextAreaArticle());
    			jTextFieldBV.setText(ia.getJTextFieldBV());
    			jTextFieldCP.setText(ia.getJTextFieldCP());
    			jTextFieldNumArticle.setText(ia.getJTextFieldNumArticle());
    			jTextFieldPB.setText(ia.getJTextFieldPB());
    			jLabelSection1.setText(ia.getJLabelSection1());
    			jLabelBV1.setText(ia.getJLabelBV1());
    			jLabelCP1.setText(ia.getJLabelCP1());
    			jLabelPB1.setText(ia.getJLabelPB1());
    			jTextAreaJustification.setText(ia.getJTextAreaJustification());
    			jLabelPropBudg1.setText(ia.getJLabelPropBudg1());
    			setFromDB(true);
    			setTable1();
    		} else {
    			JOptionPane.showMessageDialog(
    												this,
    												"Vous n'avez aucune donnée pour cet article!",
    												"Information",
    												JOptionPane.INFORMATION_MESSAGE);			
    			jTextAreaArticle.setText(getArticle().getLibelleArticle());
    			jTextFieldBV.setText("0");
    			jTextFieldCP.setText("0");
    			jTextFieldNumArticle.setText(getArticle().getNumArticle());
    			jTextFieldPB.setText("0");
    			jLabelSection1.setText(getArticle().getSection().getNumSection());
    			jLabelBV1.setText(String.valueOf(getAnneeBudgetaire().getAnnee()-2));
    			jLabelCP1.setText(String.valueOf(getAnneeBudgetaire().getAnnee()-1));
    			jLabelPB1.setText(String.valueOf(getAnneeBudgetaire().getAnnee()));
    			jTextAreaJustification.setText("");
    			jLabelPropBudg1.setText(String.valueOf(getAnneeBudgetaire().getAnnee()));	
    			setFromDB(false);		
    		}
    		//DisableChamps
    		setEditable(false);
     
    	}
    	public void setEditable(boolean b) {
    		jTextAreaArticle.setEditable(b);
    		jTextFieldBV.setEditable(b);
    		jTextFieldCP.setEditable(b);
    		jTextFieldNumArticle.setEditable(b);
    		//jTextFieldPB.setEnabled(b);
    		jLabelSection1.setEditable(b);
    		jLabelBV1.setEditable(b);
    		jLabelCP1.setEditable(b);
    		jLabelPB1.setEditable(b);
    		//jTextAreaJustification.setEnabled(b);
    		jLabelPropBudg1.setEditable(b);
    	}
     
    	private class BudgetActionListener implements ActionListener {
     
    		public void actionPerformed(ActionEvent e) {
    			if (e.getSource() == jButtonSave) {				
    				try {
    					doSave();
    				} catch (Exception e1) {					
    					e1.printStackTrace();
    				}
    			} else if (e.getSource() == jButtonPrint) {
    				try {
    					doPrint();
    				} catch (Exception e1) {					
    					e1.printStackTrace();
    				}				
    			}
    		}
    	}
    	private class OpenActionListener implements ActionListener {
    		public void actionPerformed(ActionEvent e) {		
    			if (e.getSource() == openButton) {
    				int returnVal = fc.showOpenDialog(BudgetDetailPanel.this);
    				if (returnVal == JFileChooser.APPROVE_OPTION) {
    					File file = fc.getSelectedFile();
    					jTextAreaJustification.setText(file.getAbsolutePath());
    					jTextAreaJustification.setEditable(false);					
    				}				
    			} else {
    				jTextAreaJustification.setEditable(true);
    				jTextAreaJustification.setText("");									
    			} 
    		}
    	}
     
    	private JFileChooser fc;
     
    	private class ArticleDetailListener implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
    			if (e.getSource() == addButton) {
     
    				System.out.println(table.getRowCount());
    				//ViewArticleDetail vad = new ViewArticleDetail(tableModel,_sqlArtDet);
    				//vad.show();
    			}
    			if (e.getSource() == changeButton) {
    				System.out.println(table.getSelectedRow());
    //				DefaultCellEditor editor =
    //					(DefaultCellEditor) table.getCellEditor();
    //				if (editor != null) {
    //					editor.stopCellEditing();
    //					if (table.isEditing())
    //						editor.cancelCellEditing();
    //				}
    //				AlaBat a = null;
    //				for (Iterator it = (Iterator) tableModel.getSqlUpdateList();
    //					it.hasNext();
    //					) {
    //						String s = (String) it.next();
    //						int row = Integer.parseInt(s);
    //						String ROUNUMROU = (String)tableModel.getValueAt(row,0);
    //						String ALABATPKMDEB = tableModel.dbRepresentation(1,tableModel.getValueAt(row,1));
    //						String ALABATPKMFIN = tableModel.dbRepresentation(2,tableModel.getValueAt(row,2));
    //
    //						String sALABATPKMDEB = ALABATPKMDEB.substring(1,ALABATPKMDEB.length()-1);
    //						String sALABATPKMFIN = ALABATPKMFIN.substring(1,ALABATPKMFIN.length()-1);
    //						float fALABATPKMDEB = 0;
    //						float fALABATPKMFIN = 0;
    //						try {
    //							fALABATPKMDEB = Float.parseFloat(sALABATPKMDEB);
    //							fALABATPKMFIN = Float.parseFloat(sALABATPKMFIN);
    //						} catch (NumberFormatException ex) {
    //							JOptionPane.showMessageDialog(
    //								new JFrame(),
    //								"Veuillez entrer un nombre correct (ex: 100.0)",
    //								"Warning",
    //								JOptionPane.WARNING_MESSAGE);
    //							return;
    //
    //						}
    //
    //						a = new AlaBat(num,service,ROUNUMROU,fALABATPKMDEB,fALABATPKMFIN);
    //						try {
    //							AlaBatDB.getInstance().update(a);
    //						} catch (Exception ex) {
    //							ex.printStackTrace();
    //						}
    //				}
    //				tableModel.emptySqlUpdateList();
    			}
    			if (e.getSource() == deleteButton) {
    				System.out.println(table.getSelectedRow());			
    				int selectedRowCount = table.getSelectedRowCount();
    				System.out.println(table.getSelectedRows().length);
    				if (selectedRowCount == 0) {
    					JOptionPane.showMessageDialog(
    						new JFrame(),
    						"Veuillez sélectionner un enregistrement à supprimer",
    						"Warning",
    						JOptionPane.WARNING_MESSAGE);
     
    				} else {
    					if (JOptionPane
    						.showConfirmDialog(
    							new JFrame(),
    							"Etes-vous sûr de vouloir supprimer ces enregistrements ?",
    							"Confirmation",
    							JOptionPane.YES_NO_OPTION)
    						== JOptionPane.YES_OPTION) {
     
    						int[] selectedRows = table.getSelectedRows();
    						for (int i = 0; i <= selectedRows.length - 1; i++) {
    							String sqlArtDet = "DELETE FROM BUDGET_ARTICLE_DETAIL WHERE ID_ARTICLE= "+getArticle().getIdArticle()+" AND ID_DETAIL=";
    							sqlArtDet = sqlArtDet
    									+ tableModel.getValueAt(selectedRows[i], 0);
    							System.out.println(sqlArtDet);
    							//tableModel.deleteQuery(sqlArtDet,_sqlArtDet);
    						}
    					}
    				}
     
    			}
    		}
    	}
     
    	public void setTable1() {
    		_sqlArtDet = "SELECT DETAIL.ID_DETAIL, DETAIL.LIBELLE_DETAIL, AD.MONTANT "
    					+ "FROM BUDGET BUDGET, BUDGET_DETAIL DETAIL, BUDGET_ARTICLE_DETAIL AD "
    					+  "WHERE ((DETAIL.ID_DETAIL = AD.ID_DETAIL) "
    					+  "AND (BUDGET.ID_ARTICLE = AD.ID_ARTICLE) "
    					+  "AND (BUDGET.ID_ARTICLE = "+getArticle().getIdArticle()+"))";
    		tableModel.executeQuery(_sqlArtDet);
    		sorter = new TableSorter(tableModel);
    		table = Utils.arrangeTable(table, sorter, listeToolTipsArt);	
    		//table.getColumnModel().removeColumn((table.getColumn("ID_DETAIL")));	
    	}
     
    }

  4. #4
    Membre habitué
    Inscrit en
    Septembre 2004
    Messages
    8
    Détails du profil
    Informations forums :
    Inscription : Septembre 2004
    Messages : 8
    Par défaut
    C'est un code test, en fait ds ma methode init() je construit mon panel avec ma jtable qui doit absolument etre vide.. jusque la tt va bien ensuite dans mon code jai une fonction setTable1() qui va me permettre de remplir cette JTable.. mais le probleme c'est que la fonction getSelectedRow() et toutes les methodes ayant en rapport. me renvoit 0 ou -1.. donc la version initiale de ma jtable.. il ne prend pas en compte le fait que je l'ai rempli alors qu'il l'est bien..


    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
    package test;
     
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
     
    import util.JDBCAdapter;
    import util.TableSorter;
    import util.Utils;
     
    public class testTable extends JPanel {
     
    	private JButton deleteButton =
    		new JButton(
    			"Supprimer un détail",
    			Utils.getImageIcon("/toolbarButtonGraphics/general/Delete"));
     
    	//JTable
    	public static JDBCAdapter tableModel;
    	public JTable table;
    	public static TableSorter sorter;
     
    	public String[] listeToolTipsArt = { "ID_DETAIL", "Libellé", "Montant" };
    	public String _sqlArtDet = "";
     
    	/**
             * @return
             */
    	public JTable getTable() {
    		return table;
    	}
     
    	/**
             * @param table
             */
    	public void setTable(JTable table) {
    		this.table = table;
    	}
     
    	public testTable() {
    		super();
    		init();
    	}
     
    	private void init() {
     
    		deleteButton.addActionListener(new OpenActionListener());
     
    		JPanel jPanelDEtail = new JPanel();
     
    		tableModel = new JDBCAdapter();
    ////		_sqlArtDet = "SELECT DETAIL.ID_DETAIL, DETAIL.LIBELLE_DETAIL, AD.MONTANT "
    ////			+ "FROM BUDGET BUDGET, BUDGET_DETAIL DETAIL, BUDGET_ARTICLE_DETAIL AD "
    ////			+ "WHERE ((DETAIL.ID_DETAIL = AD.ID_DETAIL) "
    ////			+ "AND (BUDGET.ID_ARTICLE = AD.ID_ARTICLE) "
    ////			+ "AND (BUDGET.ID_ARTICLE = 49))";	
    ////		tableModel.executeQuery(_sqlArtDet);	
    		sorter = new TableSorter(tableModel);
    		//table = Utils.arrangeTable(table, sorter, listeToolTipsArt);
    		setTable(new JTable(sorter));
    		JScrollPane JSP = new JScrollPane(table);
    		JSP.setPreferredSize(new Dimension(500, 200));
    		jPanelDEtail.add(JSP, BorderLayout.CENTER);
     
    		jPanelDEtail.add(deleteButton, BorderLayout.SOUTH);
     
    		add(jPanelDEtail, BorderLayout.CENTER);
    		setTable1();
     
    	}
     
    	private class OpenActionListener implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
    			if (e.getSource() == deleteButton) {
    				System.out.println(table.getSelectedRow());
    				int selectedRowCount = table.getSelectedRowCount();
    				System.out.println(table.getSelectedRows().length);
     
    			}
    		}
    	}
     
    	public void setTable1() {
    		_sqlArtDet =
    			"SELECT DETAIL.ID_DETAIL, DETAIL.LIBELLE_DETAIL, AD.MONTANT "
    				+ "FROM BUDGET BUDGET, BUDGET_DETAIL DETAIL, BUDGET_ARTICLE_DETAIL AD "
    				+ "WHERE ((DETAIL.ID_DETAIL = AD.ID_DETAIL) "
    				+ "AND (BUDGET.ID_ARTICLE = AD.ID_ARTICLE) "
    				+ "AND (BUDGET.ID_ARTICLE = 49))";
    		tableModel.executeQuery(_sqlArtDet);
    		sorter = new TableSorter(tableModel);
    		setTable(Utils.arrangeTable(table, sorter, listeToolTipsArt));
    	}
    	public static void main(String[] args) {
    		JFrame a = new JFrame();
    		a.getContentPane().add(new testTable());
    		a.pack();
    		a.show();
    	}
     
     
    }
    en revanche si ma fonction setTable1() je le fai avant d'ajouter ma jtable a mon panel tt marche...

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Jtable : Couleur de row en fonction d'une valeur de cell
    Par Damascus dans le forum Composants
    Réponses: 5
    Dernier message: 30/06/2011, 15h17
  2. Dysfonctionnement fonction print() JTable
    Par RR instinct dans le forum Composants
    Réponses: 1
    Dernier message: 28/11/2008, 14h02
  3. Réponses: 4
    Dernier message: 20/08/2008, 16h20
  4. [JTable] s'efface si je crée la fonction paint()
    Par brunoperel dans le forum Composants
    Réponses: 5
    Dernier message: 25/01/2008, 15h34
  5. Mise à jour d'une JTable en fonction d'une JList
    Par xtaZy dans le forum Composants
    Réponses: 1
    Dernier message: 21/03/2007, 18h25

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