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.