SHARE
TWEET

JTable problem

a guest Mar 19th, 2019 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package testlist;
  7.  
  8. import java.awt.BorderLayout;
  9. import java.awt.Dimension;
  10. import java.awt.FlowLayout;
  11. import java.awt.GridLayout;
  12. import java.awt.event.ActionEvent;
  13. import java.awt.event.ActionListener;
  14. import javax.swing.DefaultCellEditor;
  15. import javax.swing.JButton;
  16. import javax.swing.JFrame;
  17. import javax.swing.JLabel;
  18. import javax.swing.JPanel;
  19. import javax.swing.JScrollPane;
  20. import javax.swing.JTable;
  21. import javax.swing.event.ListSelectionEvent;
  22. import javax.swing.event.ListSelectionListener;
  23. import javax.swing.table.AbstractTableModel;
  24. import javax.swing.table.TableModel;
  25. import javax.swing.table.DefaultTableModel;
  26.  
  27.  
  28. /**
  29.  *
  30.  * @author Evangelion-01
  31.  */
  32. public class TableTestFinal extends javax.swing.JFrame implements ListSelectionListener{
  33.  
  34.     /**
  35.      * Creates new form TableTestFinal
  36.      */
  37.    
  38.     private JTable maTable;
  39.     private JScrollPane scrollPane;
  40.     private JLabel txtField, txtRowCount;
  41.     private JButton btnAjouter, btnSupprimer, btnClear;
  42.     private JPanel global, centre, boutons, jPTextField;
  43.     private DefaultTableModel dtm;
  44.    
  45.     private String[] nouvelleLigne = {"", "", ""};
  46.     private String[] enTete = {"prenom", "nom", "force"};
  47.     private String[][] donnee = {
  48.                 {"alex", "cool","9000"},
  49.                 {"steph", "whiteFox", "9000"},
  50.                 {"grumpy", "cat", "420"}};
  51.     private static int aindex;
  52.     public TableTestFinal() {
  53.         super();
  54.         dtm = new DefaultTableModel(donnee, enTete);
  55.         setVisible(true);
  56.         setSize(500, 200);
  57.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  58.        
  59.        maTable = new JTable();
  60.        scrollPane = new JScrollPane(maTable);
  61.        scrollPane.setPreferredSize(new Dimension(350, 250));
  62.        
  63.         global = new JPanel();
  64.         centre = new JPanel();
  65.         boutons = new JPanel();
  66.         jPTextField = new JPanel();
  67.        
  68.         btnAjouter = new JButton();
  69.         btnSupprimer = new JButton();
  70.         btnClear = new JButton();
  71.        
  72.         txtField = new JLabel();
  73.         txtRowCount = new JLabel();
  74.        
  75.         centre.setLayout(new GridLayout(2,1));
  76.         boutons.setLayout(new GridLayout(3,1));
  77.         global.setLayout(new BorderLayout());
  78.         jPTextField.setLayout(new GridLayout(2, 1));
  79.        
  80.         boutons.add(btnAjouter);
  81.         boutons.add(btnSupprimer);
  82.         boutons.add(btnClear);
  83.         btnAjouter.setText("Ajouter");
  84.         btnSupprimer.setText("Supprimer");
  85.         btnClear.setText("Enlever la Selection");
  86.        
  87.         centre.add(scrollPane);
  88.        
  89.         jPTextField.add(txtRowCount);
  90.         jPTextField.add(txtField);
  91.         txtField.setText("Selection : ");
  92.         txtRowCount.setText(Integer.toString(dtm.getRowCount()) + " " + Integer.toString(aindex));
  93.        
  94.         global.add(centre);
  95.         global.add(boutons, BorderLayout.EAST);
  96.         global.add(jPTextField, BorderLayout.SOUTH);
  97.        
  98.        
  99.         getContentPane().add(global);
  100.        
  101.         setAction();
  102.         setButtons();
  103.         maTable.setModel(dtm);
  104.        
  105.        
  106.     }
  107.     public void setAction(){
  108.         maTable.getSelectionModel().addListSelectionListener((ListSelectionListener) this);
  109.                
  110.     }
  111.     @Override
  112.     public void valueChanged(ListSelectionEvent e) {
  113.         String selectedData = "";
  114.         aindex = maTable.getSelectedRow();
  115.        
  116.         int[] colonne = maTable.getSelectedColumns();
  117.         for(int i = 0; i < 3; i++){
  118.             selectedData += (String) maTable.getValueAt(aindex, i);
  119.         }
  120.         txtField.setText("Selection : 777 -> " + selectedData + "  " + aindex);
  121.         txtRowCount.setText(Integer.toString(dtm.getRowCount()) + "   " + Integer.toString(aindex));
  122.         //maTable.getSelectionModel().clearSelection();
  123.     }
  124.     public void setButtons(){
  125.         btnAjouter.addActionListener(new java.awt.event.ActionListener() {
  126.            
  127.                 public void actionPerformed(java.awt.event.ActionEvent evt) {
  128.                     dtm.addRow(nouvelleLigne);
  129.                 }
  130.             });
  131.         btnSupprimer.addActionListener(new ActionListener() {
  132.            
  133.             @Override
  134.             public void actionPerformed(ActionEvent evt) {
  135.                 DefaultCellEditor dce = (DefaultCellEditor)maTable.getCellEditor();
  136.                 if (dce != null) dce.stopCellEditing();
  137. //                int viewIndex = maTable.getSelectedRow();
  138. //                if(viewIndex != -1) {
  139. //                    int modelIndex = maTable.convertRowIndexToModel(viewIndex);
  140. //                    DefaultTableModel model = (DefaultTableModel)maTable.getModel();
  141. //                    model.removeRow(modelIndex);
  142. //                    modelIndex = maTable.getSelectedRow();
  143. //                }
  144. //                dtm.moveRow(aindex, aindex, 0);
  145.                 dtm.removeRow(aindex);
  146.                 dtm.fireTableRowsDeleted(aindex, aindex);
  147.                
  148.                 //effaceColone(maTable);
  149.             }
  150.         });
  151.     }
  152.     public void effaceColone( JTable table){
  153.         DefaultCellEditor dce = (DefaultCellEditor)maTable.getCellEditor();
  154.         if (dce != null) dce.stopCellEditing();
  155.         DefaultTableModel model = (DefaultTableModel) table.getModel();
  156.         int[] rows = table.getSelectedRows();
  157.         for(int i=0;i<rows.length;i++){
  158.             dtm.removeRow(rows[i]-i);
  159.             dtm.fireTableRowsDeleted(rows[i]-i, rows[i]-i);
  160.         }
  161.     }
  162.        
  163.                        
  164.  
  165.     /**
  166.      * @param args the command line arguments
  167.      */
  168.     public static void main(String args[]) {
  169.         /* Set the Nimbus look and feel */
  170.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  171.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  172.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  173.          */
  174.         try {
  175.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  176.                 if ("Nimbus".equals(info.getName())) {
  177.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  178.                     break;
  179.                 }
  180.             }
  181.         } catch (ClassNotFoundException ex) {
  182.             java.util.logging.Logger.getLogger(TableTestFinal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  183.         } catch (InstantiationException ex) {
  184.             java.util.logging.Logger.getLogger(TableTestFinal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  185.         } catch (IllegalAccessException ex) {
  186.             java.util.logging.Logger.getLogger(TableTestFinal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  187.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  188.             java.util.logging.Logger.getLogger(TableTestFinal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  189.         }
  190.         //</editor-fold>
  191.  
  192.         /* Create and display the form */
  193.         java.awt.EventQueue.invokeLater(new Runnable() {
  194.             public void run() {
  195.                 new TableTestFinal().setVisible(true);
  196.             }
  197.         });
  198.     }
  199.  
  200.    
  201.  
  202.     // Variables declaration - do not modify                    
  203.     // End of variables declaration                  
  204. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top