1

I'm working on a project for my college and I need two of JFrame. The first one is the Main menu and the second one visible when JButton(Run) pressed. I need to paint a memory in both of JFrame, So I used JTable to show the memory.

Memory class is:

    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);
    }
}

I add this JTable into my main JFrame and another 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);

} }

Here in this code, setValueAt() method work perfect and there is no any problem. but when I use same code of JTabel in the second JFrame when I press run JButton but create new JTable not that in the Memory class and insert value to the JTable using setValueAt(), JTabel doesn't update.

Code of the second 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);
    }

so I have a problem only with the second JFrame and I can't refresh JTable with new data. I have tried more than one way to take right result but there is nothing.

Thanks for your help.

Ibrahim
  • 39
  • 1
  • 2
  • 5
  • 4
    Don't use a null layout and setBounds(). Swing was designed to be used with layout managers for proper functionality. – camickr Jul 31 '13 at 17:11
  • Ok I agree, but I think my problem isn't here. So I will do it into my code later. – Ibrahim Jul 31 '13 at 17:17
  • `table.setValueAt(MainGUI.this.field2.getText().toString(),MainGUI.this.field1.getText().toString(),1 );` jtable `setValueAt(Object value, int row, int column)` – nachokk Jul 31 '13 at 18:00
  • +1 @camickr. Also see [this](http://stackoverflow.com/questions/12646240/jtable-doesnt-refresh-update-data/12646338#12646338) similar question/answer – David Kroukamp Jul 31 '13 at 18:59
  • Have a read on [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice) and dont extend JFrame class unnecessarily simply create instance and call methods on instance – David Kroukamp Jul 31 '13 at 19:01
  • 2
    Please learn java naming conventions and stick to them. – kleopatra Aug 01 '13 at 10:11

0 Answers0