使用setValueAt刷新/更新JTable无法正常工作

时间:2013-07-31 16:33:18

标签: java swing jtable

我正在为我的大学做一个项目,我需要两个JFrame。第一个是主菜单,第二个是JButton(Run)按下时可见。 我需要在JFrame中绘制一个内存,所以我用JTable来显示内存。

记忆类是:

    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,490));
        this.panel.setLayout(null);
        this.add(panel);

        String info[][] = new String[256][2];
        for(int j=0;j<256;j++)
        {
            info[j][0] = j;

            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;   
        }
        };
        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment( JLabel.CENTER );
        table.getColumnModel().getColumn(0).setCellRenderer( centerRenderer );
        table.getColumnModel().getColumn(1).setCellRenderer( centerRenderer );
        JTableHeader header = table.getTableHeader();
        header.setBackground(Color.GRAY);
        this.scrollpane = new JScrollPane(this.table);
        this.scrollpane.setBounds(0, 0,315, 490);
        this.panel.add(this.scrollpane);
        return panel;
    }

    public void setAddrValue(int addr,String value)
    {
        Memory.this.array[addr] = value;
        this.table.setValueAt(value,addr , 1);
    }

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

    public String[] getMemory()
    {
        return Memory.this.array;
    }



    public void deleteValue(int i)
    {
        array[i]=null;
        this.table.setValueAt(null, i, 1);
    }
}

我将这个JTable添加到我的主JFrame和另一个JComponents中:

public class MainGUI extends JFrame{

 private JTextField field1;
    private JTextField field2;
private JButton button1;
    private JButton button2;
private Memory mem;

public MainGUI()
    {
        this.GraphicComponents(); 

    }


public final void GraphicComponents()
    {
        this.setTitle("Machine");
        this.setSize(800, 620);
        this.setLayout(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        this.setResizable(false);

        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("Write Value");
        this.button1.setBounds(170,50,130,20);
        this.button1.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e)
         { 
                      MainGUI.this.mem.setAddrValue(MainGUI.this.field1.getText().toString(),MainGUI.this.field2.getText().toString() );
         }

        });
        this.add(button1);

        this.button2 = new JButton("Run");
        this.button2.setBounds(500,550,100,30);
        this.button2.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e)
         { 
                      ExecutionGUI exe = new ExecutionGUI(mem);
                      exe.setVisible(true);
         }

        });
        this.add(button2);

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

}         }

在这段代码中,setValueAt()方法完美无缺,没有任何问题。 但是当我在第二个JFrame中使用相同的JTabel代码时,我按下运行JButton但是在Memory类中创建新的JTable并使用setValueAt()将值插入JTable,JTabel不会更新。

第二个JFrame的代码:

public class ExecutionGUI extends JFrame {


private JTextField field1;
        private JTextField field2;
    private JButton button1;
private JScrollPane scrollpane;
        private JButton button2;
    private Memory mem;
private JTable table;
private static DefaultTableModel model;
private String info[][];
    private String[] title;
private Memory mem;

 public ExecutionGUI(Memory mem)
    {
        this.mem = mem;

        this.GraphicComponents();
    }

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


        this.info = new String[256][2];
        for(int j=0;j<256;j++)
        {
            info[j][0] = j;
            if(mem.getAddrValue(j)==null)
            {

            }
            else
            {
                info[j][1] = mem.getAddrValue(j);
            }
        }
        title = new String[]{"Address","Value"};

        model = new DefaultTableModel(info,title);
        table = new JTable(model){
        @Override
        public boolean isCellEditable(int rowIndex, int colIndex) {
        return false;   
        }
        };
        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment( JLabel.CENTER );
        table1.getColumnModel().getColumn(0).setCellRenderer( centerRenderer );
        table1.getColumnModel().getColumn(1).setCellRenderer( centerRenderer );
        JTableHeader header = table.getTableHeader();
        header.setBackground(Color.GRAY);
        this.scrollpane = new JScrollPane(table);
        this.scrollpane.setBounds(610, 30,170, 470);
        this.add(this.scrollpane);

        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("Write Value");
        this.button1.setBounds(170,50,130,20);
        this.button1.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e)
         { 
                      table.setValueAt(MainGUI.this.field2.getText().toString(),MainGUI.this.field1.getText().toString(),1 );
         }

        });
        this.add(button1);
    }

所以我只有第二个JFrame有问题,我无法用新数据刷新JTable。 我尝试了不止一种方法来取得正确的结果,但没有任何方法。

感谢您的帮助。

0 个答案:

没有答案