2016-04-06 15 views
0

私が作業を始める前に、現在のトピックを読んでこれを行う方法について解説しています。私はtable.addRow(...)を試したが、それは動作しません。私はオブジェクトを追加しようとしましたが、テーブルをリセットして1つの行を作成します。ここに基本的なネットビーンのペインコード:jTableに別の行を追加する方法

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package components; 

import java.awt.event.*; 

/** 
* 
* @author Ethan 
*/ 
public class AccountManager extends javax.swing.JFrame { 

/** 
* Creates new form AccountManager 
*/ 
public AccountManager() { 
    initComponents(); 
} 

/** 
* 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() { 

    jScrollPane1 = new javax.swing.JScrollPane(); 
    jTable1 = new javax.swing.JTable(); 
    jButton1 = new javax.swing.JButton(); 
    jButton2 = new javax.swing.JButton(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
    setTitle("Account Manager"); 

    jTable1.setModel(new javax.swing.table.DefaultTableModel(
     new Object [][] { 
      {"Admin", "Admin", new Integer(1), new Boolean(true)}, 
      {"Username", "Password", new Integer(1), new Boolean(true)} 
     }, 
     new String [] { 
      "Username", "Password", "Account Type", "Active" 
     } 
    ) { 
     Class[] types = new Class [] { 
      java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Boolean.class 
     }; 

     public Class getColumnClass(int columnIndex) { 
      return types [columnIndex]; 
     } 
    }); 
    jTable1.getTableHeader().setReorderingAllowed(false); 
    jScrollPane1.setViewportView(jTable1); 

    jButton1.setText("Add Account"); 

    class addAccount implements ActionListener{ 

     public void actionPerformed(ActionEvent e){ 
      //Action listener for button 1 that adds the row 
     } 
    } 
    jButton1.addActionListener(new addAccount()); 

    jButton2.setText("Delete Account"); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addContainerGap() 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 580, Short.MAX_VALUE) 
       .addGroup(layout.createSequentialGroup() 
        .addComponent(jButton1) 
        .addGap(18, 18, 18) 
        .addComponent(jButton2) 
        .addGap(0, 0, Short.MAX_VALUE))) 
      .addContainerGap()) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addContainerGap() 
      .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addGap(18, 18, 18) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(jButton1) 
       .addComponent(jButton2)) 
      .addContainerGap(78, Short.MAX_VALUE)) 
    ); 

    pack(); 
}// </editor-fold>       

/** 
* @param args the command line arguments 
*/ 
public static void main(String args[]) { 
    /* Set the Nimbus look and feel */ 
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
    * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
    */ 
    try { 
     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
      if ("Steel".equals(info.getName())) { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (ClassNotFoundException ex) { 
     java.util.logging.Logger.getLogger(AccountManager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(AccountManager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(AccountManager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(AccountManager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
    //</editor-fold> 

    /* Create and display the form */ 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      new AccountManager().setVisible(true); 
     } 
    }); 
} 

// Variables declaration - do not modify      
private javax.swing.JButton jButton1; 
private javax.swing.JButton jButton2; 
private javax.swing.JScrollPane jScrollPane1; 
private javax.swing.JTable jTable1; 
// End of variables declaration     
} 

だから、誰もがネットビーンのjTableで行を追加する方法を知っていますか?私は非常に感謝のトピックに、感謝のおかげで!

また、コードを削除する行がコードに行を追加するようなものではない場合は、それを行う方法についてのいくつかの参考にもなります。

答えて

2

あなたの問題の1つは、テーブルのエントリをハードコーディングしていることです。データを格納するためのデータベースの形式が必要です。詳細のコーディングを追加する方法でテーブルにエントリを追加することはできません。

SQLiteやMySQLなどのサーバーデータベースなどの組み込みデータベースを使用することをお勧めします。

データベースを使用してこのような方法を実行するには、さまざまな方法があります。テーブルとの間でエントリを追加、編集、または削除する最も良い方法は、データベースとの接続および統合によるものです。シンプルで使いやすく、最も効果的で効率的な方法です。

MySQLデータベースへの接続をコード:

//used to add the entry to database table 
     String query = "insert into tableName (col1, col2, col3,col4) values ('data','data','data','data')"; 
     Class.forName("com.mysql.jdbc.Driver"); 
     //connection 
     Connection conn = (Connection) 
       //root and username and password for access to the database 
     DriverManager.getConnection("jdbc:mysql://localhost:3306/NameOfDatabase","root","password"); 
     //create the statement that will be used 
     Statement stmt=conn.createStatement(); 
     //executes the query statement 
     stmt.executeUpdate(query); 

は、あなたが同様にあなたのmysqlのライブラリをインポートし、これまでずっと、あなたのコードの統合時に必要とされている輸入品を確認する必要があります。

エントリを追加したら、テーブルを再度呼び出すだけでテーブルを更新できます。簡単な方法ですが、最も効果的ではありませんが、このセクションのためだけに役立ちます。

希望、これはこれは必ずしも、私は特定のタスクのためにと考えているだろうが、偶然にそれがあるので、私は、後でSQLdatabaseを統合する方法を考え出すことを計画し、そうされた解決策ではありません

+0

を支援あなたは素晴らしいです。ありがとうございました! :D –

+1

これは喜ばしいことですが、これを行う方法はいくつかありますが、これは簡単な方法であり、単純なSQLの統合だけで、これをさらに進歩させることができます。喜んで。あなたを助けてうれしい! – John

関連する問題