Netbeans 如何向jTable添加另一行
在开始之前,我已经通读了当前关于如何做到这一点的主题,但这些解决方案并不适合我。我试过table.addRow(…),但不起作用。我试图添加一个对象,但它只是重置表并生成一行。下面是一个基本的netbeans窗格代码:Netbeans 如何向jTable添加另一行,netbeans,jtable,Netbeans,Jtable,在开始之前,我已经通读了当前关于如何做到这一点的主题,但这些解决方案并不适合我。我试过table.addRow(…),但不起作用。我试图添加一个对象,但它只是重置表并生成一行。下面是一个基本的netbeans窗格代码: /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates
/*
* 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
}
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
包装组件;
导入java.awt.event.*;
/**
*
*@作者伊桑
*/
公共类AccountManager扩展了javax.swing.JFrame{
/**
*创建新表单AccountManager
*/
公共会计经理(){
初始化组件();
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//
私有组件(){
jScrollPane1=newjavax.swing.JScrollPane();
jTable1=newjavax.swing.JTable();
jButton1=newjavax.swing.JButton();
jButton2=newjavax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle(“客户经理”);
jTable1.setModel(新的javax.swing.table.DefaultTableModel(
新对象[][]{
{“Admin”,“Admin”,新整数(1),新布尔值(true)},
{“用户名”、“密码”、新整数(1)、新布尔值(真)}
},
新字符串[]{
“用户名”、“密码”、“帐户类型”、“活动”
}
) {
类[]类型=新类[]{
java.lang.String.class、java.lang.String.class、java.lang.Integer.class、java.lang.Boolean.class
};
公共类getColumnClass(int columnIndex){
返回类型[列索引];
}
});
jTable1.getTableHeader().setReorderingAllowed(false);
jScrollPane1.setViewportView(jTable1);
jButton1.setText(“添加帐户”);
类addAccount实现ActionListener{
已执行的公共无效操作(操作事件e){
//添加行的按钮1的操作侦听器
}
}
jButton1.addActionListener(新的addAccount());
jButton2.setText(“删除帐户”);
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1,javax.swing.GroupLayout.DEFAULT\u SIZE,580,Short.MAX\u值)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addGap(18,18,18)
.addComponent(jButton2)
.addGap(0,0,短.MAX_值)))
.addContainerGap())
);
layout.setVerticalGroup(
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,简称最大值))
);
包装();
}//
/**
*@param指定命令行参数
*/
公共静态void main(字符串参数[]){
/*设置Nimbus的外观和感觉*/
//
/*如果Nimbus(在JavaSE6中引入)不可用,请使用默认的外观。
*详情请参阅http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
试一试{
for(javax.swing.UIManager.LookAndFeelInfo:javax.swing.UIManager.getInstalledLookAndFeels()){
if(“Steel”.equals(info.getName())){
setLookAndFeel(info.getClassName());
打破
}
}
}捕获(ClassNotFoundException ex){
getLogger(AccountManager.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(实例化异常){
getLogger(AccountManager.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}捕获(非法访问例外){
getLogger(AccountManager.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(javax.swing.UnsupportedLookAndFeelException ex){
getLogger(AccountManager.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}
//
/*创建并显示表单*/
invokeLater(new Runnable()){
公开募捐{
新建AccountManager().setVisible(true);
}
});
}
//变量声明-不修改
私有javax.swing.JButton jButton1;
私有javax.swing.JButton jButton2;
私有javax.swing.JScrollPane-jScrollPane1;
私有javax.swing.JTable jTable1;
//变量结束声明
}
那么,有人知道如何使用netbean的jTable添加行吗?我非常感谢您在这个话题上的帮助,谢谢
另外,如果删除一行与在代码中添加一行不同,那么我也希望能得到一些关于如何做到这一点的建议 您的问题之一是,您正在为您的表硬编码条目,您将需要某种形式的数据库来存储数据。您不能通过尝试添加特定编码的方法将条目添加到表中
//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);