简体   繁体   English

将值插入到不同JFrames中的两个单独表中的最佳方法是什么?

[英]What is the best way to insert values into two separate tables in different JFrames?

I have question regarding parsing values to a different table. 我对将值解析到另一个表有疑问。

I have a user interface containing a JTable. 我有一个包含JTable的用户界面。 I also have an admin interface with another Jtable. 我也有另一个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. 如果用户在用户界面JFrame中的JTable中添加了一行,则我需要它来将相同的数据更新或插入到存储在不同JFrame中的管理表中。

What is the best approach for this? 最好的方法是什么?

Currently I have three classes: 目前我有三节课:

Main - Contains all Methods for inserting table values: Main-包含所有用于插入表值的方法:

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. 用户 -调用主表中的ADD方法,即简单的actionlistener。 And the JTable is pulled using the following: 然后使用以下方法拉出JTable:

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... 管理员 -显示表格和标题,但不插入上面“ ADD”方法中的数据...

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? 从本质上讲,什么是即时消息,我如何将Main“ ADD”方法中的值插入Admin JTable中? The Tablemodel Headers are pulled but not the data... Tablemodel标头被拉出,但不拉取数据...

Your help is much appreciated, let me know if this is completely wrong... 非常感谢您的帮助,如果这完全错误,请告诉我...

Share the TableModel: 共享TableModel:

DefaultTableModel model = new DefaultTableModel(...);
JTable table1 = new JTable( model );
JTable table2 = new JTable( model );

When ever you make a change to table1 the change will be reflected in table2 (and vice versa) because both table share the same TableModel. 每当您对table1进行更改时,更改都会反映在table2中(反之亦然),因为两个表共享相同的TableModel。

Also, don't use a null layout!!! 另外,不要使用空布局!!! Don't use setBounds()!!! 不要使用setBounds()! Swing was designed to be used with layout managers, for too many reasons to list here. Swing被设计为与布局管理器一起使用,出于太多原因在此处列出。

Thanks for your help, I found the solution. 感谢您的帮助,我找到了解决方案。

I needed to parse the DefaultTableModel; 我需要解析DefaultTableModel。 which contained the data, as a parameter rather than the actual JTable, silly mistake. 其中包含数据(作为参数而不是实际的JTable)的愚蠢错误。

Main Class - open AdminGUI method: Passed the userTableModel stored globally in Main Class. 主类-打开AdminGUI方法:传递了全局存储在主类中的userTableModel。

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. AdminGUI类 Admin类需要将TabelModel解析为新的JTable,这似乎可以正常工作。

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... 将来可能会帮助别人...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM