大大的尾巴 2014-12-19 04:16 采纳率: 0%
浏览 5339

[java] jtable更新数据的问题

package bms;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.BorderLayout;
import javax.swing.border.TitledBorder;
import javax.swing.UIManager;
import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.Insets;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class test extends JFrame {

static Vector<String> columnNames=new Vector<String>();
static Vector cellData=new Vector();
JPanel panel = new JPanel();
JTable table = new JTable(cellData,columnNames);
private JScrollPane jsp=new JScrollPane(table);
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
static database db=new database();
static ResultSet res=null;
private JTextField textField_3;

public static void main(String[] args) {
    update("*");

    test window = new test();
    window.setVisible(true);
}

public static void update(String temp)
{
    columnNames.clear();
    cellData.clear();
    ResultSetMetaData rsmd;
    try{
        res=db.sta.executeQuery("select "+temp+" from Books");
        rsmd=res.getMetaData();
        for (int i = 1; i <= rsmd.getColumnCount(); ++i)  
            columnNames.addElement(rsmd.getColumnName(i));//存储名字  
        while (res.next()) {  
            Vector<String> currow = new Vector<String>();  
            for (int i = 1; i <= rsmd.getColumnCount(); ++i) {  
                currow.addElement(res.getString(i));  
            }  
            cellData.addElement(currow);  
        }  
    }catch(SQLException e)
    {
        e.printStackTrace();
    }
}

public test()
{
    setTitle("Book Management System");
    this.setBounds(100,100, 800,600);
    GridBagLayout gridBagLayout = new GridBagLayout();
    gridBagLayout.columnWidths = new int[]{0, 0};
    gridBagLayout.rowHeights = new int[]{325, 0, 0};
    gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
    gridBagLayout.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
    getContentPane().setLayout(gridBagLayout);

    GridBagConstraints gbc_panel = new GridBagConstraints();
    gbc_panel.insets = new Insets(0, 0, 5, 0);
    gbc_panel.fill = GridBagConstraints.BOTH;
    gbc_panel.gridx = 0;
    gbc_panel.gridy = 0;
    panel.setBorder(new TitledBorder(null, "图书目录:", TitledBorder.LEFT, TitledBorder.TOP, null, null));
    getContentPane().add(panel, gbc_panel);
    panel.setLayout(new BorderLayout(0, 0));

    panel.add(jsp);

    JPanel panel_1 = new JPanel();
    panel_1.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "检索 增加", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
    GridBagConstraints gbc_panel_1 = new GridBagConstraints();
    gbc_panel_1.fill = GridBagConstraints.BOTH;
    gbc_panel_1.gridx = 0;
    gbc_panel_1.gridy = 1;
    getContentPane().add(panel_1, gbc_panel_1);
    GridBagLayout gbl_panel_1 = new GridBagLayout();
    gbl_panel_1.columnWidths = new int[]{0, 0, 126, 0, 0, 136, 78, 98, 0, 0};
    gbl_panel_1.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
    gbl_panel_1.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
    gbl_panel_1.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
    panel_1.setLayout(gbl_panel_1);

    JLabel lblNewLabel = new JLabel("书名:");
    GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
    gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
    gbc_lblNewLabel.gridx = 1;
    gbc_lblNewLabel.gridy = 1;
    panel_1.add(lblNewLabel, gbc_lblNewLabel);

    textField = new JTextField();
    GridBagConstraints gbc_textField = new GridBagConstraints();
    gbc_textField.insets = new Insets(0, 0, 5, 5);
    gbc_textField.fill = GridBagConstraints.BOTH;
    gbc_textField.gridx = 2;
    gbc_textField.gridy = 1;
    panel_1.add(textField, gbc_textField);
    textField.setColumns(10);

    JLabel label = new JLabel("作者:");
    GridBagConstraints gbc_label = new GridBagConstraints();
    gbc_label.anchor = GridBagConstraints.EAST;
    gbc_label.insets = new Insets(0, 0, 5, 5);
    gbc_label.gridx = 4;
    gbc_label.gridy = 1;
    panel_1.add(label, gbc_label);

    textField_3 = new JTextField();
    GridBagConstraints gbc_textField_3 = new GridBagConstraints();
    gbc_textField_3.insets = new Insets(0, 0, 5, 5);
    gbc_textField_3.fill = GridBagConstraints.BOTH;
    gbc_textField_3.gridx = 5;
    gbc_textField_3.gridy = 1;
    panel_1.add(textField_3, gbc_textField_3);
    textField_3.setColumns(10);

    JLabel lblNewLabel_1 = new JLabel("图书室:");
    GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
    gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
    gbc_lblNewLabel_1.gridx = 1;
    gbc_lblNewLabel_1.gridy = 3;
    panel_1.add(lblNewLabel_1, gbc_lblNewLabel_1);

    textField_1 = new JTextField();
    GridBagConstraints gbc_textField_1 = new GridBagConstraints();
    gbc_textField_1.insets = new Insets(0, 0, 5, 5);
    gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
    gbc_textField_1.gridx = 2;
    gbc_textField_1.gridy = 3;
    panel_1.add(textField_1, gbc_textField_1);
    textField_1.setColumns(10);

    JButton button = new JButton("检索");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
                update("Book_Name");
                db.close();
        }
    });
    GridBagConstraints gbc_button = new GridBagConstraints();
    gbc_button.fill = GridBagConstraints.BOTH;
    gbc_button.insets = new Insets(0, 0, 5, 5);
    gbc_button.gridx = 7;
    gbc_button.gridy = 1;
    panel_1.add(button, gbc_button);

    JButton button_1 = new JButton("返回");
    button_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }
    });
    GridBagConstraints gbc_button_1 = new GridBagConstraints();
    gbc_button_1.fill = GridBagConstraints.BOTH;
    gbc_button_1.insets = new Insets(0, 0, 5, 5);
    gbc_button_1.gridx = 7;
    gbc_button_1.gridy = 3;
    panel_1.add(button_1, gbc_button_1);

    JLabel lblNewLabel_2 = new JLabel("出版社:");
    GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints();
    gbc_lblNewLabel_2.insets = new Insets(0, 0, 0, 5);
    gbc_lblNewLabel_2.gridx = 1;
    gbc_lblNewLabel_2.gridy = 5;
    panel_1.add(lblNewLabel_2, gbc_lblNewLabel_2);

    textField_2 = new JTextField();
    GridBagConstraints gbc_textField_2 = new GridBagConstraints();
    gbc_textField_2.insets = new Insets(0, 0, 0, 5);
    gbc_textField_2.fill = GridBagConstraints.HORIZONTAL;
    gbc_textField_2.gridx = 2;
    gbc_textField_2.gridy = 5;
    panel_1.add(textField_2, gbc_textField_2);
    textField_2.setColumns(10);
}

}


这是我的代码。。我想在调用update()的时时候实现table内容的变更,但是现在实现不了,总是变化不了。。。
我试过在监听器updata("Book_Name")后面加上table.updateUI();,但是会报错:Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
求大神啊。。应该怎么解决啊。。

  • 写回答

4条回答 默认 最新

  • devmiao 2014-12-19 04:19
    关注

    ArrayIndexOutOfBoundsException说明索引下标越界了。

    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况