Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Java - JTable
Forum - Java - JTable

Avatar
tano90 (Normal User)
Newbie


Messaggi: 1
Iscritto: 06/06/2012

Segnala al moderatore
Postato alle 11:32
Mercoledė, 06/06/2012
serve aiuto sulle Jtable

ho questo codice:

l'interfaccia grafica č banalmente creata con netbeans

la funzione su cui sto sperimentando l'inserimento č : ErrorActionPerformed alla riga 200...

chi mi sa dire dove sbaglio!!


Codice sorgente - presumibilmente Java

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. import javax.swing.*;
  6. import java.awt.*;
  7. import javax.swing.table.*;
  8. /**
  9.  *
  10.  * @author Gaetano
  11.  */
  12. public class ServerView extends javax.swing.JFrame {
  13.  
  14.         int indR;
  15.         int indRd;
  16.     public ServerView() {
  17.         initComponents();
  18.     }
  19.  
  20.     /**
  21.      * This method is called from within the constructor to initialize the form.
  22.      * WARNING: Do NOT modify this code. The content of this method is always
  23.      * regenerated by the Form Editor.
  24.      */
  25.     @SuppressWarnings("unchecked")
  26.     // <editor-fold defaultstate="collapsed" desc="Generated Code">
  27.     private void initComponents() {
  28.  
  29.         jFrame1 = new javax.swing.JFrame();
  30.         jScrollPane1 = new javax.swing.JScrollPane();
  31.         jTextArea1 = new javax.swing.JTextArea();
  32.         jPanel1 = new javax.swing.JPanel();
  33.         jLabel1 = new javax.swing.JLabel();
  34.         Error = new javax.swing.JButton();
  35.         Performace = new javax.swing.JButton();
  36.         quit = new javax.swing.JButton();
  37.         jScrollPane3 = new javax.swing.JScrollPane();
  38.         model = new DefaultTableModel();
  39.         tabella = new javax.swing.JTable(model);
  40.         jMenuBar1 = new javax.swing.JMenuBar();
  41.         jMenu1 = new javax.swing.JMenu();
  42.         credits = new javax.swing.JMenuItem();
  43.         jSeparator1 = new javax.swing.JPopupMenu.Separator();
  44.         exit = new javax.swing.JMenuItem();
  45.  
  46.         javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
  47.         jFrame1.getContentPane().setLayout(jFrame1Layout);
  48.         jFrame1Layout.setHorizontalGroup(
  49.             jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  50.             .addGap(0, 400, Short.MAX_VALUE)
  51.         );
  52.         jFrame1Layout.setVerticalGroup(
  53.             jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  54.             .addGap(0, 300, Short.MAX_VALUE)
  55.         );
  56.  
  57.         jTextArea1.setColumns(20);
  58.         jTextArea1.setRows(5);
  59.         jScrollPane1.setViewportView(jTextArea1);
  60.  
  61.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  62.  
  63.         jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 18)); // NOI18N
  64.         jLabel1.setText("Menų Manager");
  65.  
  66.         Error.setText("Error/Warning");
  67.         Error.addActionListener(new java.awt.event.ActionListener() {
  68.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  69.                 ErrorActionPerformed(evt);
  70.             }
  71.         });
  72.  
  73.         Performace.setText("Performace");
  74.  
  75.         quit.setText("Exit");
  76.  
  77.         javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  78.         jPanel1.setLayout(jPanel1Layout);
  79.         jPanel1Layout.setHorizontalGroup(
  80.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  81.             .addGroup(jPanel1Layout.createSequentialGroup()
  82.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  83.                     .addGroup(jPanel1Layout.createSequentialGroup()
  84.                         .addContainerGap()
  85.                         .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  86.                     .addGroup(jPanel1Layout.createSequentialGroup()
  87.                         .addGap(22, 22, 22)
  88.                         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  89.                             .addComponent(Error, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  90.                             .addComponent(Performace, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  91.                             .addComponent(quit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  92.                         .addGap(0, 0, Short.MAX_VALUE)))
  93.                 .addContainerGap())
  94.         );
  95.         jPanel1Layout.setVerticalGroup(
  96.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  97.             .addGroup(jPanel1Layout.createSequentialGroup()
  98.                 .addContainerGap()
  99.                 .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
  100.                 .addGap(40, 40, 40)
  101.                 .addComponent(Error)
  102.                 .addGap(18, 18, 18)
  103.                 .addComponent(Performace)
  104.                 .addGap(18, 18, 18)
  105.                 .addComponent(quit)
  106.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  107.         );
  108.  
  109.         tabella.setAutoCreateRowSorter(true);
  110.         tabella.setModel(new javax.swing.table.DefaultTableModel(
  111.             new Object [][] {
  112.                 {1, 0,"Error","prova", 15}
  113.             },
  114.             new String [] {
  115.                 "ID", "Priority", "Type", "Content", "Date"
  116.             }
  117.         ) {
  118.             Class[] types = new Class [] {
  119.                 java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
  120.             };
  121.             boolean[] canEdit = new boolean [] {
  122.                 false, false, false, false, false
  123.             };
  124.  
  125.             public Class getColumnClass(int columnIndex) {
  126.                 return types [columnIndex];
  127.             }
  128.  
  129.             public boolean isCellEditable(int rowIndex, int columnIndex) {
  130.                 return canEdit [columnIndex];
  131.             }
  132.         });
  133.         jScrollPane3.setViewportView(tabella);
  134.         tabella.getColumnModel().getColumn(0).setResizable(false);
  135.         tabella.getColumnModel().getColumn(1).setResizable(false);
  136.         tabella.getColumnModel().getColumn(2).setResizable(false);
  137.         tabella.getColumnModel().getColumn(3).setResizable(false);
  138.         tabella.getColumnModel().getColumn(4).setResizable(false);
  139.  
  140.         jMenu1.setText("Opzioni");
  141.  
  142.         credits.setText("Credits");
  143.         credits.addActionListener(new java.awt.event.ActionListener() {
  144.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  145.                 creditsActionPerformed(evt);
  146.             }
  147.         });
  148.         jMenu1.add(credits);
  149.         jMenu1.add(jSeparator1);
  150.  
  151.         exit.setText("Exit");
  152.         exit.addActionListener(new java.awt.event.ActionListener() {
  153.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  154.                 exitActionPerformed(evt);
  155.             }
  156.         });
  157.         jMenu1.add(exit);
  158.  
  159.         jMenuBar1.add(jMenu1);
  160.  
  161.         setJMenuBar(jMenuBar1);
  162.  
  163.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  164.         getContentPane().setLayout(layout);
  165.         layout.setHorizontalGroup(
  166.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  167.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  168.                 .addContainerGap()
  169.                 .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  170.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
  171.                 .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  172.                 .addGap(46, 46, 46))
  173.         );
  174.         layout.setVerticalGroup(
  175.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  176.             .addGroup(layout.createSequentialGroup()
  177.                 .addContainerGap()
  178.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  179.                     .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  180.                     .addGroup(layout.createSequentialGroup()
  181.                         .addGap(20, 20, 20)
  182.                         .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  183.                         .addGap(0, 150, Short.MAX_VALUE)))
  184.                 .addContainerGap())
  185.         );
  186.  
  187.         pack();
  188.     }// </editor-fold>
  189.  
  190.     private void creditsActionPerformed(java.awt.event.ActionEvent evt) {                                        
  191.         SwingUtilities.invokeLater(new Thread(new Runnable(){public void run(){
  192.                         JOptionPane.showMessageDialog(null,"Belvedere Andrea: 616/002783\nGarufo Gaetano: 616/002887\nMangirratti Mariarosa: 616/002815\nMinardi Pierluigi: 616/002755","Created by",JOptionPane.INFORMATION_MESSAGE);}}));
  193.      
  194.     }                                      
  195.  
  196.     private void exitActionPerformed(java.awt.event.ActionEvent evt) {                                    
  197.         System.exit(0);
  198.     }                                    
  199.  
  200.     private void ErrorActionPerformed(java.awt.event.ActionEvent evt) {
  201.         Events e= new Events(1,0,"prova","Ciao","1551");
  202.                 System.out.println(tabella.getRowCount());
  203.                 Integer id = new Integer(1);
  204.                 Integer p= new Integer(e.getPriority());
  205.                 String a= e.getContent();
  206.                 String b= e.getType();
  207.                 String c= e.getDate();
  208.                 model.addRow(new Object[]{id, p,a,b,c});
  209.                 model.fireTableRowsInserted(0,model.getRowCount());
  210.             /*model.setRowCount(indR);
  211.     model.setValueAt(id, indR, 0);
  212.     model.setValueAt(p, indR, 1);
  213.     model.setValueAt(a, indR, 2);
  214.     model.setValueAt(b, indR, 3);
  215.     model.setValueAt(c, indR, 4);
  216.         tabella.setModel(model);
  217.             indR++;
  218.     indRd++;*/
  219.        
  220.     }
  221.  
  222.     /**
  223.      * @param args the command line arguments
  224.      */
  225.     public static void main(String args[]) {
  226.         /*
  227.          * Set the Nimbus look and feel
  228.          */
  229.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  230.         /*
  231.          * If Nimbus (introduced in Java SE 6) is not available, stay with the
  232.          * default look and feel. For details see
  233.          * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  234.          */
  235.         try {
  236.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  237.                 if ("Nimbus".equals(info.getName())) {
  238.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  239.                     break;
  240.                 }
  241.             }
  242.         } catch (ClassNotFoundException ex) {
  243.             java.util.logging.Logger.getLogger(ServerView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  244.         } catch (InstantiationException ex) {
  245.             java.util.logging.Logger.getLogger(ServerView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  246.         } catch (IllegalAccessException ex) {
  247.             java.util.logging.Logger.getLogger(ServerView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  248.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  249.             java.util.logging.Logger.getLogger(ServerView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  250.         }
  251.         //</editor-fold>
  252.  
  253.         /*
  254.          * Create and display the form
  255.          */
  256.         java.awt.EventQueue.invokeLater(new Runnable() {
  257.  
  258.             public void run() {
  259.                 new ServerView().setVisible(true);
  260.             }
  261.         });
  262.     }
  263.     // Variables declaration - do not modify
  264.     private javax.swing.JButton Error;
  265.     private javax.swing.JButton Performace;
  266.     private javax.swing.JMenuItem credits;
  267.     private javax.swing.JMenuItem exit;
  268.     private javax.swing.JFrame jFrame1;
  269.     private javax.swing.JLabel jLabel1;
  270.     private javax.swing.JMenu jMenu1;
  271.     private javax.swing.JMenuBar jMenuBar1;
  272.     private javax.swing.JPanel jPanel1;
  273.     private javax.swing.JScrollPane jScrollPane1;
  274.     private javax.swing.JScrollPane jScrollPane3;
  275.     private javax.swing.JPopupMenu.Separator jSeparator1;
  276.     private javax.swing.JTextArea jTextArea1;
  277.     private javax.swing.JButton quit;
  278.     private DefaultTableModel model;
  279.     private javax.swing.JTable tabella;
  280.     // End of variables declaration
  281. }


PM Quote