从已调用的类复制JTable并将其粘贴到另一个类中

时间:2013-07-02 13:12:50

标签: java swing class jtable

我在JFrame中有一个JTable,我可以从JText和JButton的JFrame中插入JTable的值,当我按下JButton时,我需要将具有相同值的JTabel复制到另一个JFrame(go)

JTabel的代码存在于使用其他方法扩展JPanel的类中:

public class Memory extends JPanel{

    private JPanel panel;
    private JTable table;
    private String[] array; 
    private JScrollPane scrollpane;

    public Memory()
    {
       array = new String[256];
       this.addMemoryGUI();
    }

    public final JPanel addMemoryGUI()
    {
        this.panel = new JPanel();
        this.panel.setPreferredSize(new Dimension(315,480));
        this.panel.setBackground(Color.red);
        this.add(panel);

        BorderLayout b = new BorderLayout();
        this.panel.setLayout(b);

        String info[][] = new String[256][2];
        for(int j=0;j<256;j++)
        {
            info[j][0] = "0x"+Integer.toHexString(j);
            if(info[j][0].length()==3)
            {
                info[j][0]="0x"+'0'+info[j][0].substring(2);
            }
            info[j][1] = null;
        }
        String[] title = new String[]{"Address","Value"};

        DefaultTableModel model = new DefaultTableModel(info,title);
        table = new JTable(model){
        @Override
        public boolean isCellEditable(int rowIndex, int colIndex) {
        return false;   
        }
        };
        JTableHeader header = table.getTableHeader();
        header.setBackground(Color.GRAY);
        this.scrollpane = new JScrollPane(this.table);
        this.panel.add(this.scrollpane);
        return panel;
    }
public void setAddrValue(int addr,String value)
    {
        Memory.this.array[addr] = value;
        String hexValue=method.binToHexString(value);
        this.table.setValueAt("0x"+hexValue,addr , 1);
    }

    public String getAddrValue(int addr)
    {
        return Memory.this.array[addr];
    }

主JFrame的代码是:

public class MainGUI extends JFrame{

    private JLabel label1;
private JLabel label2;
private JTextField field1;
private JTextField field2;
private JButton button1;


private Memory mem;
private ExecutionGUI exe;

public MainGUI()
{
    this.GraphicComponents();     
}

public final void GraphicComponents()
{
    this.setTitle("Brookshear Machine");
    this.setSize(800, 620);
    this.setLayout(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setResizable(false);
    this.label2=new JLabel("Address:");
    this.label2.setBounds(10, 30, 70, 20);
    this.add(label2);

    this.label3=new JLabel("Value:");
    this.label3.setBounds(90, 30, 70, 20);
    this.add(label3);

    this.field1=new JTextField();
    this.field1.setBounds(10,50,70,20);
    this.add(field1);

    this.field2=new JTextField();
    this.field2.setBounds(90,50,70,20);
    this.add(field2);

    this.button1=new JButton("Set Value");
    this.button1.setBounds(170,50,130,20);
    this.button1.addActionListener(new ActionListener() {

     @Override
     public void actionPerformed(ActionEvent e)
     { 

             MainGUI.this.mem.setAddrValue(field1.gettext(),field2.gettext());

    });
    this.add(button1);
    this.button4 = new JButton("GO");
    this.button4.setBounds(680, 530, 100, 30);
    this.button4.addActionListener(new ActionListener() {

     @Override
     public void actionPerformed(ActionEvent e)
     {

             exe = new ExecutionGUI(MainGUI.this.mem);
             exe.GraphicComponents();


     }
    });
    this.add(button4);

    this.mem = new Memory();
    this.mem.setBounds(450, 30, 315, 480);
    this.add(mem);
}

ExecutionGUI的代码是:

public class ExecutionGUI extends JFrame{


    private JPanel panel;
    private Memory mem;


    public ExecutionGUI(Memory input)
    {
        this.GraphicComponents();
        this.mem = input;
    }

    public final void GraphicComponents()
    {
        this.setTitle("Machine");
        this.setBounds(200, 100, 800, 600);
        this.setLayout(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        this.setResizable(false);

        panel = new JPanel();
        this.panel.setLayout(null);
        panel.setBounds(0, 0, 800, 600);
        add(panel);


        this.mem.setBounds(450, 30, 315, 480);
        this.panel.add(this.mem);

        this.setVisible(true);
    }
}

但是当我按下JButton(GO)时,我得到一个java.lang.NullPointerException。

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at brookshearmachine.ExecutionGUI.GraphicComponents(ExecutionGUI.java:98)
(At this.mem.setBounds(450, 30, 315, 480);)
        at brookshearmachine.ExecutionGUI.<init>(ExecutionGUI.java:33)
(At this.GraphicComponents();)
        at brookshearmachine.MainGUI$5.actionPerformed(MainGUI.java:942)
(At exe = new ExecutionGUI(MainGUI.this.mem);)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
        at java.awt.Component.processMouseEvent(Component.java:6505)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
        at java.awt.Component.processEvent(Component.java:6270)
        at java.awt.Container.processEvent(Container.java:2229)
        at java.awt.Component.dispatchEventImpl(Component.java:4861)
        at java.awt.Container.dispatchEventImpl(Container.java:2287)
        at java.awt.Component.dispatchEvent(Component.java:4687)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
        at java.awt.Container.dispatchEventImpl(Container.java:2273)
        at java.awt.Window.dispatchEventImpl(Window.java:2719)
        at java.awt.Component.dispatchEvent(Component.java:4687)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
        at java.awt.EventQueue.access$200(EventQueue.java:103)
        at java.awt.EventQueue$3.run(EventQueue.java:688)
        at java.awt.EventQueue$3.run(EventQueue.java:686)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
        at java.awt.EventQueue$4.run(EventQueue.java:702)
        at java.awt.EventQueue$4.run(EventQueue.java:700)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

我该如何解决? 谢谢,

0 个答案:

没有答案