如何将一行从JTable复制到另一个JTable中

时间:2017-03-13 21:31:16

标签: java postgresql swing jtable mouseevent

我在我的projekt工作,我需要将一行从JTable复制到另一个JTable,第二个JTable应该只是一行表。我创建了mouselistener到第一个JTable,在doubleclick它应该复制行并将其插入另一个JTable但它没有正常工作,任何想法如何解决它?我从第一个表中的数据库中获取数据。代码:

public void cputable() {
    try {
        conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/test","postgres","postgres");
        stat = conn.createStatement();
        result = stat.executeQuery("SELECT name,bus_speed,socket,cores,chipset,price*1.3 FROM CPU");

        cputable.setModel(DbUtils.resultSetToTableModel(result));
        result.close();
        stat.close();
    }
    catch (Exception e){
        e.printStackTrace();
    }
}

这是我尝试复制行的代码:

 cputable.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent me) {
                JTable cputable =(JTable) me.getSource();
                int row = cputable.getSelectedRow();
                int col = cputable.getColumnCount();

                if (me.getClickCount() == 2) {
                    cputablebottom.repaint();
                    for(int i = 0; i < col; i++) {
                         DefaultTableModel model = (DefaultTableModel) cputable.getModel();
                         List<String>list = new ArrayList<String>();
                         list.add( cputable.getValueAt(row, i).toString());
                         model.addRow(list.toArray());
                         cputablebottom.setModel(model);
                        }

之前和之后的结果:

编辑:

我在代码中重新编写了一下,现在它复制整个列表而不是只复制一行。

cputable.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent me) {
            JTable cputable =(JTable) me.getSource();
            int row = cputable.getSelectedRow();
            int col = cputable.getColumnCount();

            if (me.getClickCount() == 2) {
                cputablebottom.repaint();
                 DefaultTableModel model1 = (DefaultTableModel) cputable.getModel();
                 List<String>list = new ArrayList<String>();

                 model1.addRow(list.toArray());
                for(int i = 0; i < col; i++) {
                    list.add( cputable.getValueAt(row, i).toString());
                    cputablebottom.setModel(model1);
                    System.out.println(model1);
                    System.out.println(list);
                    }
                cputablebottom.setModel(model1);

2 个答案:

答案 0 :(得分:1)

我没试过这个,但是......

DefaultTableModel model1 = (DefaultTableModel) cputable.getModel();
Vector data = model1.getDataVector();
Object rowObj = data.get(row);

Vector newData = new Vector(1);
newData.add(rowObj);

DefaultTableModel model2 = (DefaultTableModel) cputablebottom.getModel();
model2.setRowCount(0);
model2.addRow(newData);

这应该保留第二个表的列信息。

或者,您可以创建新的DefaultTableModel,但每次都必须重新配置列信息

我建议您阅读JavaDocs for DefaultTableModel

经过测试的例子

我经常不使用DefaultTableModel,更喜欢在每一行中放置一个实际的对象,然后我可以定义它的显示方式,这样可以使它更简单,但是,如果是{{ 1}}就是你所拥有的......

DefaultTableModel

答案 1 :(得分:0)

1。我们制作表的第一类代码->

Sell.java

public class Sell extends JFrame {
  public static void main(String args[]) {
    new Sell();
  }

  JTextField tf1, tf2, tf3;
  JButton b1, b2, b3;
  JTable t1;
  String s1;

  public Sell() {
    setBounds(450, 200, 600, 300);
    setTitle("Sell");
    Container con = getContentPane();
    con.setBackground(Color.WHITE);
    setLayout(null); // FlowLayout,GridLayout By default it is FlowLayout.
    setVisible(true);
    setResizable(false); // It allows to resize application size.
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    tf1 = new JTextField();
    tf2 = new JTextField();
    tf3 = new JTextField();
    b1 = new JButton("Select");
    b2 = new JButton("Add");
    b3 = new JButton("Invoice");
    t1 = new JTable(0, 2);
    DefaultTableModel model = (DefaultTableModel) t1.getModel();
    model.addRow(new Object[] { "Product", "Price" });

    add(tf1);
    add(tf2);
    add(tf3);
    add(b1);
    add(b2);
    add(b3);
    add(t1);

    tf1.setBounds(50, 50, 50, 20);
    tf2.setBounds(25, 100, 100, 20);
    tf3.setBounds(150, 100, 100, 20);
    b1.setBounds(150, 45, 75, 30);
    b2.setBounds(100, 195, 75, 30);
    b3.setBounds(200, 195, 75, 30);
    t1.setBounds(350, 50, 225, 200);

    b1.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent e) {

        s1 = tf1.getText();

        try {
          Class.forName("com.mysql.cj.jdbc.Driver");
          Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/vanisb",
              "root", "spirit");
          Statement stmt = conn.createStatement();
          ResultSet rs = stmt.executeQuery("select * from product where ID='" + s1 + "';");

          while (rs.next()) {
            tf2.setText(rs.getString("product_name"));
            tf3.setText(rs.getString("price"));

          }
          conn.close();
        } catch (Exception ex) {
          JOptionPane.showMessageDialog(b1, ex);
        }
      }
    });

    b2.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent e) {

        model.addRow(new Object[] { tf2.getText(), tf3.getText() });

      }
    });

    b3.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent e) {

        new Invoice(model);
      }
    });
  }
}

2。我们必须获取最后一个表数据的第二类代码->

Invoice.java

public class Invoice extends JFrame {

  public static void main(String[] args) {

  }

  JLabel l1, l2;
  JTextField tf1, tf2;
  JTable t2;
  JButton b1;

  public Invoice(DefaultTableModel model) {
    setBounds(450, 200, 600, 300);
    setTitle("Invoice");
    Container con = getContentPane();
    con.setBackground(Color.WHITE);
    setLayout(null);
    setVisible(true);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    l1 = new JLabel("Name : ");
    l2 = new JLabel("Phone No. : ");
    tf1 = new JTextField();
    tf2 = new JTextField();
    b1 = new JButton("Add Customer");
    t2 = new JTable(model);

    add(l1);
    add(l2);
    add(tf1);
    add(tf2);
    add(b1);
    add(t2);

    l1.setBounds(50, 50, 100, 20);
    l2.setBounds(50, 100, 100, 20);
    tf1.setBounds(150, 50, 100, 20);
    tf2.setBounds(150, 100, 100, 20);
    b1.setBounds(100, 175, 150, 30);
    t2.setBounds(300, 50, 275, 200);

    b1.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent arg0) {

      }
    });
  }
}