日期:2014-05-20  浏览次数:20697 次

高手救命,关于TABLE的数据
有一个主界面FRAME里有一个JTable,现在单击按纽 "添加 ",则弹出一个对话框

对话框也有一个JTable,可以选择你要添加的东西.对话框有个确定按钮..点确定后,

如何在主界面里显示你刚才在对话框里选中项?

      另一问题:JTable的数据如何写入数据库?

------解决方案--------------------
看看能不能给你点提示!

import java.awt.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.table.*;
import java.util.*;

public class Frame1
extends JFrame implements ActionListener {
public Frame1() {
try {
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
}

private void jbInit() throws Exception {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jButton2.setText( "清空 ");
jButton2.addActionListener(this);
jButton3.setText( " < < ");
this.getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);
jButton1.addActionListener(this);
jPanel1.add(jButton1);
jPanel1.add(jButton2);
this.getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);
jPanel2.add(jScrollPane2);
jScrollPane2.getViewport().add(jTable2);
jPanel2.add(jButton3);
jPanel2.add(jScrollPane1);
jScrollPane1.getViewport().add(jTable1);
jButton3.addActionListener(this);
jButton1.setText( "添加一行 ");
this.setBounds(0, 0, 300, 300);
this.setVisible(true);
}

JPanel jPanel1 = new JPanel();
JButton jButton1 = new JButton();
DefaultTableModel dtm = new DefaultTableModel(0, 3);
DefaultTableModel dtm1 = new DefaultTableModel(0, 3);
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JPanel jPanel2 = new JPanel();
JScrollPane jScrollPane1 = new JScrollPane();
JTable jTable1 = new JTable(dtm);
JScrollPane jScrollPane2 = new JScrollPane();
JTable jTable2 = new JTable(dtm1);
public static void main(String[] args) {
Frame1 f = new Frame1();
}

public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(jButton2)) { //清空
while (dtm.getRowCount() > 0) {
dtm.removeRow(0);
}
}
else if (e.getSource().equals(jButton1)) { //添加一行
String[] a = {
String.valueOf(Math.random()), String.valueOf(Math.random()), String.valueOf(Math.random())};
dtm.addRow(a);
}else if(e.getSource().equals(jButton3)){
if(this.jTable1.getSelectedRow()> =0){
//System.out.println(dtm.getDataVector().get(jTable1.getSelectedRow()).getClass());
//dtm1.addRow((String[])dtm.getDataVector().get(jTable1.getSelectedRow()));
dtm1.addRow((Vector)dtm.getDataVector().get(jTable1.getSelectedRow()));
}
}
}
}