1

My problem is this. I have a table on my first JFrame and now I want the data on my first table to be duplicated on the table of my second JFrame. I'm using GUI, I have 2 JFrames, both JFrames have tables and i want both tables to display same data.

I have come up to this solution but I don't know how to set the data on my second table.

This is the code:

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

How do I solve the problem?

To make it more clear. ill attach photo

this is my first JFrame Photo

this is my second JFrame

my second jframe is just a summary of what is being entered in first JFrame. that is the reason why i use 2 JFrames.

i have 2 JFrame forms. in First JFrame Form i have this Code, i tried to modify the code above a bit.

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

and then at the second JFrame i have this partof code

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

i instantiate my JFrame1 on JFrame2 to use the method getEquipTableData() on JFrame1. IDK if what im doing is right.

1 Answers1

1

First and foremost take some time to read if multiple JFrames is good or bad practice.

Secondly, you can change the TableModel of the second JTable, by using setModel() method passing a DefaultTableModel. The getTableData method you have created seems to be ok in order to achieve something like this.

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

(After some comments taking place):

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 Z.
  • 6,643
  • 4
  • 27
  • 47
  • [George Zorwell](https://i.stack.imgur.com/DpkHo.png) I presume? (Sorry, silly pun based on your current rep. being the title of a famous book.) – Andrew Thompson Aug 10 '19 at 03:47
  • i doesnt display the data on table2 bro. i updated the question please take a look at the pictures i attached. – Charlie Magne Aguda Aug 10 '19 at 07:50
  • @CharlieMagneAguda Without giving us your code or a [mcve] to see how exactly you create each `JTable` we really cannot help you. The code works fine for me. – George Z. Aug 10 '19 at 08:04
  • Object[][] firstTableData = getTableData(firstTable); secondJTable.setModel(new DefaultTableModel(firstTableData, columnNames)); This solution works if both tables are on the same JFrame. My goal is to copy the data of my Table1 on JFrame1 to Table2 of JFrame2 – Charlie Magne Aguda Aug 10 '19 at 08:25
  • I created my table in Netbeans by GUI Drag and Drop. JTable. – Charlie Magne Aguda Aug 10 '19 at 08:26
  • @CharlieMagneAguda Then you have one more reason to avoid using a second JFrame. However, give me some time to check it myself. – George Z. Aug 10 '19 at 08:27
  • allright bro. thank you . update me bro if you found a solution for it. thanks! – Charlie Magne Aguda Aug 10 '19 at 08:35
  • @CharlieMagneAguda Check my example. It works fine. With 2 JFrames. I will mention again that you should post a [mcve] or more parts of your code. There might be one million reasons why this does not work for you (i mean in your main application). – George Z. Aug 10 '19 at 08:37
  • @George Z. hey bro thanks. ive tried your code and its working. Have you checked the pictures i attached bro? I have 2 JFrame Forms there and the table 1 is one the first JFrame Form while table2 is on second JFrame form, Jframe form 1 and 2 have different source file bro soo thats why i have difficulty linking both tables. – Charlie Magne Aguda Aug 10 '19 at 08:50
  • hey bro. i added some info on my question. – Charlie Magne Aguda Aug 10 '19 at 08:59
  • @CharlieMagneAguda In the screenshots you have posted, the data seems pretty copied to me.. – George Z. Aug 10 '19 at 09:01
  • @George Z i didnt put any value on table1 bro which is the first screenshot . i want the value on table1 to be copied also in my table2 on JFrame2. i copied table1 thats why they look the same bro. – Charlie Magne Aguda Aug 10 '19 at 09:03
  • @CharlieMagneAguda Sorry i can't help you more without a [mcve]. – George Z. Aug 10 '19 at 09:07