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
求大神啊。。应该怎么解决啊。。
或者有什么能够更新jtable显示的方法啊
------解决思路----------------------
从你当前的代码来说,应该将JTable table = new JTable(cellData,columnNames);放在update()后面;即table初始化应该放在cellData,columnNames初始化之后!但这样你也只能更新一次!
如果你想随时更新table的内容,可以通过JTable类的getModel()方法来获取其对应的TableModel对象,利用TableModel对象来更新
JTable的数据(Swing组件设计利用了MVC设计模式),在这给你个小例子提供点思路:
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
public class TestFrame extends JFrame {
private JPanel contentPane;
private JTable table;
public static void main(String[] args) {
public TestFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
table = new JTable();
final DefaultTableModel dtm = new DefaultTableModel();
dtm.addColumn("序号");
dtm.addColumn("随机数");
for (int i = 0; i < 10; ++i) {
dtm.addRow(new Object[] { i, Math.random() * 100 });
}
table.setModel(dtm);
table.setBounds(10, 10, 414, 208);
contentPane.add(table);
JButton button = new JButton("更新");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < dtm.getRowCount(); ++i) {
dtm.setValueAt(Math.random() * 100, i, 1);
}
}
});
button.setBounds(192, 228, 93, 23);
contentPane.add(button);
}
}
点击更新后更新第二列的随机数
希望有帮助