2张桌子中2张桌子之间的互动

时间:2018-06-29 04:31:02

标签: java swing jframe jtable

什么样的框架/窗格是实现下面目标的最佳实践?

  • 主框架具有jtable1
  • ENTER上按下jtable1将调用子帧
  • 子帧的jtable2具有2行数据
  • jtable1jtable2的列名完全相同

通过在ENTER的第2行上按jtable2,将发生以下情况:

  • jtable1当前行的值= jtable2第2行的值
  • jtable1将创建新行
  • 配置子帧

1 个答案:

答案 0 :(得分:1)

以下是示例,可以作为上述问题的答案:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.table.DefaultTableModel;

public class Dialog3 {

    public static void main(String[] args) {     
        //f1=JFrame 1, f1cp=ContentPane in f1, f1table=JTable in f1, f1sp=ScrollPane in f1
        JFrame f1=new JFrame("Frame");
        f1.setBounds(0, 0, 300, 400);
        f1.getContentPane().setLayout(new BorderLayout());
        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f1.setLocationRelativeTo(null);

        JPanel f1cp=new JPanel();
        f1.getContentPane().add(f1cp, BorderLayout.CENTER);

        String[] columnNames = {"CODE", "NAME"};
            Object[][] data = {{"code 8","name 8"}, {null,null}};
            DefaultTableModel f1model = new DefaultTableModel(data, columnNames);
            JTable f1table=new JTable(f1model);
        JScrollPane f1sp=new JScrollPane(f1table);
        f1cp.add(f1sp);

        f1.pack();
        f1.setVisible(true);        

        f1table.getInputMap().put(KeyStroke.getKeyStroke("pressed ENTER"), "f1enter");
        f1table.getActionMap().put("f1enter", new AbstractAction(){
            @Override
            public void actionPerformed(ActionEvent e){showd1(f1,f1table, f1model);}
        }); 

    } //end of main(string[])

    private static void showd1(JFrame f1, JTable f1table, DefaultTableModel f1model){
        //d1=JDialog1, d1cp=ContentPane in d1, d1table=JTable in d1, d1sp=ScrollPane in d1
        JDialog d1=new JDialog(f1, "Dialog", true);
        d1.setBounds(0, 0, 300, 400);
        d1.setLocationRelativeTo(f1);

        JPanel d1cp=new JPanel();
        d1.getContentPane().add(d1cp, BorderLayout.CENTER);

        String[] columnNames = {"CODE", "NAME"};
            Object[][] data = {{"code 1","name 1"}, {"code 2","name 2"}};
            DefaultTableModel model = new DefaultTableModel(data, columnNames);
            JTable d1table=new JTable(model);
        JScrollPane d1sp=new JScrollPane(d1table);
        d1cp.add(d1sp);

        d1table.getInputMap().put(KeyStroke.getKeyStroke("pressed ENTER"), "d1tf");
        d1table.getActionMap().put("d1tf", new AbstractAction(){
            @Override
            public void actionPerformed(ActionEvent e){
                f1table.setValueAt(d1table.getValueAt(d1table.getSelectedRow(), 0),f1table.getSelectedRow(), 0);
                f1table.setValueAt(d1table.getValueAt(d1table.getSelectedRow(), 1),f1table.getSelectedRow(), 1);
                f1model.addRow(data);   
                f1table.setValueAt(null, f1model.getRowCount()-1, 0);
                f1table.setValueAt(null, f1model.getRowCount()-1, 1);
                d1.dispose();
            }
        });

        d1.pack();
        d1.setVisible(true);        
    } //end of showd1
}