【问题标题】:How to duplicate or copy data on my first Jframe table to second jframe table?如何将我的第一个 Jframe 表上的数据复制或复制到第二个 Jframe 表?
【发布时间】:2019-08-09 21:45:57
【问题描述】:

我的问题是这个。我的第一个 JFrame 上有一个表,现在我希望将第一个表上的数据复制到第二个 JFrame 的表上。我正在使用 GUI,我有 2 个 JFrame,两个 JFrame 都有表格,我希望两个表格都显示相同的数据。

我想出了这个解决方案,但我不知道如何在我的第二张表上设置数据。

这是代码:

public Object[][] getTableData (JTable table) {
    TableModel dtm = table.getModel();
    int nRow = dtm.getRowCount(), nCol = dtm.getColumnCount();
    Object[][] tableData = new Object[nRow][nCol];
    for (int i = 0 ; i < nRow ; i++)`enter code here`
        for (int j = 0 ; j < nCol ; j++)
            tableData[i][j] = dtm.getValueAt(i,j);
    return tableData;
}

我该如何解决这个问题?

为了更清楚。附上照片

this is my first JFrame Photo

this is my second JFrame

我的第二个 jframe 只是第一个 JFrame 中输入内容的摘要。这就是我使用 2 个 JFrame 的原因。

我有 2 个 JFrame 表单。在第一个 JFrame 表单中,我有这个代码,我试图修改上面的代码。

public Object[][] getEquipTableData () {
    TableModel dtm = equipmentBorrowTable.getModel();
    int nRow = dtm.getRowCount(), nCol = dtm.getColumnCount();
     tableDataE = new Object[nRow][nCol];
    for (int i = 0 ; i < nRow ; i++)
        for (int j = 0 ; j < nCol ; j++)
            tableDataE[i][j] = dtm.getValueAt(i,j);
    return tableDataE;
}

然后在第二个 JFrame 我有这部分代码

 public StudentSumarry() {
        StudentModeDashboard SMD = new StudentModeDashboard();
        String[] columnNames = {"Employee 1", "Employee 2", "Employee 3", "Employee 4"};
        Object[][] firstTableData = SMD.getEquipTableData();
        initComponents();
           equipmentBorrowTable2.setModel(new DefaultTableModel(firstTableData, columnNames));
}

我在 JFrame2 上实例化我的 JFrame1 以在 JFrame1 上使用方法 getEquipTableData()。如果我做的是对的,IDK。

【问题讨论】:

    标签: java swing jframe jtable


    【解决方案1】:

    首先需要一些时间阅读if multiple JFrames is good or bad practice.

    其次,您可以通过使用setModel() 方法传递DefaultTableModel 来更改第二个JTable 的TableModel。您创建的getTableData 方法似乎可以实现这样的目标。

    Object[][] firstTableData = getTableData(firstTable);
    secondJTable.setModel(new DefaultTableModel(firstTableData, columnNames));
    

    (在一些 cmet 发生之后):

    SSCCE:

    public class JTables {
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> {
                JFrame frame = new JFrame("Frame 1");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JTable table = new JTable(randomData(), new String[] { "FirstTableCol1", "FirstTableCol2" });
    
                frame.setLayout(new BorderLayout());
                frame.add(new JScrollPane(table));
    
                JFrame frame2 = new JFrame("Frame 2");
                frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JTable table2 = new JTable(randomData(), new String[] { "SecondTableCol1", "SecondTableCol2" });
    
                frame2.setLayout(new BorderLayout());
                frame2.add(new JScrollPane(table2));
    
                JButton button = new JButton("copy");
                button.addActionListener(e -> {
                    Object[][] data = getTableData(table);
                    table2.setModel(new DefaultTableModel(data, new String[] { "SecondTableCol1", "SecondTableCol2" }));
                });
                frame.add(button, BorderLayout.PAGE_END);
    
                frame.pack();
                frame.setVisible(true);
                frame2.pack();
                frame2.setVisible(true);
            });
        }
    
        private static Object[][] randomData() {
            Object arr[][] = new Object[5][2];
            for (int i = 0; i < arr.length; i++) {
                arr[i][0] = String.valueOf((int) (Math.random() * 10000));
                arr[i][1] = String.valueOf((int) (Math.random() * 10000));
            }
            return arr;
        }
    
        public static Object[][] getTableData(JTable table) {
            TableModel dtm = table.getModel();
            int nRow = dtm.getRowCount(), nCol = dtm.getColumnCount();
            Object[][] tableData = new Object[nRow][nCol];
            for (int i = 0; i < nRow; i++)
                for (int j = 0; j < nCol; j++)
                    tableData[i][j] = dtm.getValueAt(i, j);
            return tableData;
        }
    
    }
    

    【讨论】:

    • George Zorwell 我猜? (对不起,愚蠢的双关语,基于您当前的代表。是一本名著的书名。)
    • 我不显示table2上的数据兄弟。我更新了问题请看我附上的图片。
    • @CharlieMagneAguda 如果不向我们提供您的代码或minimal reproducible example 以了解您如何准确地创建每个JTable,我们真的无法帮助您。该代码对我来说很好。
    • 对象[][] firstTableData = getTableData(firstTable); secondJTable.setModel(new DefaultTableModel(firstTableData, columnNames));如果两个表都在同一个 JFrame 上,则此解决方案有效。我的目标是将 JFrame1 上的 Table1 的数据复制到 JFrame2 的 Table2
    • 我通过 GUI 拖放在 Netbeans 中创建了我的表。 JTable。
    猜你喜欢
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    • 1970-01-01
    • 2019-02-22
    相关资源
    最近更新 更多