I have question regarding parsing values to a different table.
I have a user interface containing a JTable. I also have an admin interface with another Jtable.
If a user added a row to the JTable in the User Interface JFrame, i need it to update or insert the same data into the admin table stored in a different JFrame.
What is the best approach for this?
Currently I have three classes:
Main - Contains all Methods for inserting table values:
public class Main1 {
public Object[][] userData;
public Object[][] adminData;
final public String[] userColumns = {"Acc Number", "Firstname", "Lastname", "Cost ","Hours", "Time","Date","Approved"};
final public String[] adminColumns = {"Acc Number", "Firstname", "Lastname", "Cost ","Hours", "Time","Date","Approved"};
final DefaultTableModel userTableModel = new DefaultTableModel(userData, userColumns);
final DefaultTableModel adminTableModel = new DefaultTableModel(adminData, adminColumns);
final JTable userTable = new JTable(userTableModel);
final JTable adminTable1 = new JTable(adminTableModel);
public static void main(String[] args) {
UserGUI gui = new UserGUI();
gui.GUI();
}
public class ADD implements ActionListener {
public void actionPerformed(ActionEvent ev) {
userTableModel.addRow(new Object[]{1, 1, 1, 1, 1, 1, 1, 1});
adminTableModel.addRow(new Object[]{1, 1, 1, 1, 1, 1, 1, 1});adminTableModel.fireTableDataChanged();
}
}
}
User - Calls the ADD method in the Main table, straightforward actionlistener. And the JTable is pulled using the following:
JPanel userTablePanel = new JPanel();
userTablePanel.setLayout(null);
userTablePanel.setBorder(BorderFactory.createTitledBorder(null, "Request History", TitledBorder.LEFT, TitledBorder.CENTER, new Font("Trebuchet MS", Font.PLAIN, 14), Color.BLACK));
userTablePanel.setBounds(0, 320, frameWidth, 200);
int tablePadding = 10;
JScrollPane userTableScrollPane = new JScrollPane();
userTableScrollPane.setBounds(tablePadding, 20, frameWidth - (2 * tablePadding), 180 - tablePadding);
userTableScrollPane.setBackground(Color.lightGray);
userTable.setModel(userTableModel);
userTableScrollPane.setViewportView(userTable);
userTableScrollPane.getViewport().setBackground(new Color(230,230,230));
userTablePanel.setBackground(Color.LIGHT_GRAY);
userTable.setBackground(new Color(150,150,150));
userTable.setVisible(true);
userTableScrollPane.setVisible(true);
userTablePanel.add(userTableScrollPane);
Admin - Shows the Table and Headers but doesnt insert the data from the "ADD" method above...
adminTable.setModel(adminTableModel);
adminTableModel.addRow(new Object[]{1, 1, 1, 1, 1, 1, 1, 1}); <--This works in its own class but doesnt work outside class....
userTableScrollPane.setViewportView(adminTable);
userTableScrollPane.getViewport().setBackground(new Color(230,230,230));
adminTable.setVisible(true);
userTableScrollPane.setVisible(true);
Essentially, What im after is, How do i insert values from the Main "ADD" method into the Admin JTable? The Tablemodel Headers are pulled but not the data...
Your help is much appreciated, let me know if this is completely wrong...
Thanks for your help, I found the solution.
I needed to parse the DefaultTableModel; which contained the data, as a parameter rather than the actual JTable, silly mistake.
Main Class - open AdminGUI method: Passed the userTableModel stored globally in Main Class.
public Object[][] userData;
final public String[] userColumns = {"Acc Number", "Firstname", "Lastname", "Cost ","Hours", "Time","Date","Approved"};
final DefaultTableModel userTableModel = new DefaultTableModel(userData, userColumns);
final JTable userTable = new JTable(userTableModel);
public class ADMIN implements ActionListener {
public void actionPerformed(ActionEvent ev) {
AdminGUI a = new AdminGUI(userTableModel);
a.getClass();
}
}
AdminGUI Class Admin Class required parsing the TabelModel into a new JTable which seemed to work fine.
public final class AdminGUI extends System1 {
public AdminGUI(DefaultTableModel userTableModelClone) {
JTable adminTable = new JTable(userTableModelClone);
adminTable.setModel(userTableModelClone);
}
}
Thanks for your help. May help someone else in the future...