简体   繁体   中英

How to copy row cell values from a JTable and transfer to another JTable

I want to get the values from the row cells on my first table and transfer it to my second table in a button click, but I fail to make it happen. I tried using for loop to evenly assign each row indexes, but it only adds a row twice with repeating cell values. Here is my code below.

int mainTRow = mainTable.getRowCount();
JButton totBTN = new JButton("Total");
totBTN.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        DefaultTableModel mdl = (DefaultTableModel)mainTable.getModel();
        DefaultTableModel mdl1 = (DefaultTableModel)showTable.getModel();
        mdl1.setRowCount(mainTRow);
        for(int i1=0; i1<mainTRow; i1++) {
            mdl1.setRowCount(mainTRow);
            mdl1.addRow(new Object[] {mainTable.getValueAt(i1,0)});
            mdl1.setValueAt(mdl.getValueAt(i1,0), i1, 0 );                  
        }
        
    }
});
totBTN.setBounds(365, 341, 89, 23);
mainPanel.add(totBTN);

I just want to clarify that mainTable here is the first table and showTable is the second one

At a very simplistic level, you could just do something like...

int[] selectedRows = leftTable.getSelectionModel().getSelectedIndices();
for (int row : selectedRows) {
    int modelRow = leftTable.convertRowIndexToModel(row);
    rightModel.addRow(leftModel.getDataVector().get(modelRow));
}

If your models are use different structures, then you need to convert the data between, for example, if I change the rightModel to...

private DefaultTableModel rightModel = new DefaultTableModel(new String[]{"Name"}, 0);

I'd then need to do something more like...

int[] selectedRows = leftTable.getSelectionModel().getSelectedIndices();
for (int row : selectedRows) {
    int modelRow = leftTable.convertRowIndexToModel(row);
    String firstName = (String)leftModel.getValueAt(row, 0);
    String lastName = (String)leftModel.getValueAt(row, 1);
    rightModel.addRow(new Object[] {lastName + " " + firstName});
}

nb: This is just copying the selected rows

Runnable example

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Main {
    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new MainPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class MainPane extends JPanel {

        private DefaultTableModel leftModel = new DefaultTableModel(NAMES, new String[] {"First", "Last"});
        private DefaultTableModel rightModel = new DefaultTableModel(new String[] {"First", "Last"}, 0);

        public MainPane() {
            setLayout(new GridBagLayout());

            JTable leftTable = new JTable(leftModel);
            JTable rightTable = new JTable(rightModel);

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.weightx = 1;
            gbc.weighty = 1;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.gridx = 0;
            gbc.gridy = 0;

            JButton copyToRight = new JButton(">");
            copyToRight.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    int[] selectedRows = leftTable.getSelectionModel().getSelectedIndices();
                    for (int row : selectedRows) {
                        int modelRow = leftTable.convertRowIndexToModel(row);
                        rightModel.addRow(leftModel.getDataVector().get(modelRow));
                    }
                }
            });

            add(new JScrollPane(leftTable), gbc);
            gbc.gridx = 2;
            add(new JScrollPane(rightTable), gbc);

            gbc.gridx = 1;
            gbc.weightx = 0;
            gbc.fill = GridBagConstraints.VERTICAL;

            JPanel actions = new JPanel(new GridBagLayout());
            actions.add(copyToRight);

            add(actions, gbc);
        }

    }

    public static final String[][] NAMES = new String[][]{
        new String[]{"Laura", "Williams"},
        new String[]{"Andrea", "Ellis"},
        new String[]{"John", "King"},
        new String[]{"Kenneth", "Garcia"},
        new String[]{"William", "Miller"},
        new String[]{"William", "Benitez"},
        new String[]{"Michelle", "Hansen"},
        new String[]{"Nicholas", "Jones"},
        new String[]{"Joshua", "Sanchez"},
        new String[]{"Stephen", "Herring"},
        new String[]{"Kyle", "Wallace"},
        new String[]{"Oscar", "Wong"},
        new String[]{"Shane", "Santana"},
        new String[]{"Jeffrey", "Weber"},
        new String[]{"Laura", "Hale"},
        new String[]{"Dr.", "Henry"},
        new String[]{"Tammy", "Mathis"},
        new String[]{"Jennifer", "Rodriguez"},
        new String[]{"Joshua", "Tucker"},
        new String[]{"Alejandra", "Wong"},
        new String[]{"Barbara", "Flores"},
        new String[]{"Kristin", "Sims"},
        new String[]{"Stephanie", "Green"},
        new String[]{"Travis", "Parks"},
        new String[]{"Brian", "Meyers"},
        new String[]{"Haley", "Casey"},
        new String[]{"Laura", "Wilson"},
        new String[]{"Sharon", "Berg"},
        new String[]{"Joshua", "Warren"},
        new String[]{"William", "Martin"},
        new String[]{"David", "Ramos"},
        new String[]{"Jessica", "Dennis"},
        new String[]{"Joel", "Ferrell"},
        new String[]{"Michael", "Johnson"},
        new String[]{"Kim", "Watkins"},
        new String[]{"Loretta", "Reed"},
        new String[]{"Jeffrey", "Williams"},
        new String[]{"Jennifer", "Hale"},
        new String[]{"Alicia", "Padilla"},
        new String[]{"Ian", "Wagner"},
        new String[]{"Jasmine", "Wheeler"},
        new String[]{"Cynthia", "Aguilar"},
        new String[]{"Justin", "Flores"},
        new String[]{"Mitchell", "Stephens"},
        new String[]{"Kristi", "Rodriguez"},
        new String[]{"Renee", "Young"},
        new String[]{"Shane", "Simmons"},
        new String[]{"Beverly", "Werner"},
        new String[]{"Jordan", "Townsend"},
        new String[]{"Carrie", "Solomon"},
        new String[]{"Jessica", "Martin"},
        new String[]{"John", "Pearson"},
        new String[]{"Steven", "Miranda"},
        new String[]{"Jennifer", "Knight"},
        new String[]{"Lindsay", "Martinez"},
        new String[]{"Joshua", "Roy"},
        new String[]{"Jerry", "Bailey"},
        new String[]{"Lauren", "Barr"},
        new String[]{"Frank", "Castaneda"},
        new String[]{"Gary", "Franklin"},
        new String[]{"Robert", "Lewis"},
        new String[]{"Peter", "Vasquez"},
        new String[]{"Brittany", "Rich"},
        new String[]{"Jacob", "White"},
        new String[]{"Anna", "Smith"},
        new String[]{"Michelle", "Davis"},
        new String[]{"Cesar", "Frank"},
        new String[]{"Chad", "Walsh"},
        new String[]{"Thomas", "Johnson"},
        new String[]{"Susan", "Wilkerson"},
        new String[]{"Hunter", "Garrett"},
        new String[]{"Molly", "Hernandez"},
        new String[]{"Gary", "Richmond"},
        new String[]{"Megan", "Price"},
        new String[]{"Daniel", "Mack"},
        new String[]{"Margaret", "Andrade"},
        new String[]{"Erika", "White"},
        new String[]{"Laura", "Carr"},
        new String[]{"Robin", "Schultz"},
        new String[]{"Valerie", "King"},
        new String[]{"Jacob", "Sherman"},
        new String[]{"Monique", "King"},
        new String[]{"Laura", "Strickland"},
        new String[]{"Jonathan", "Zuniga"},
        new String[]{"Danny", "Taylor"},
        new String[]{"Darrell", "Reese"},
        new String[]{"Juan", "Watkins"},
        new String[]{"Valerie", "Cohen"},
        new String[]{"David", "Ortiz"},
        new String[]{"Catherine", "Hawkins"},
        new String[]{"William", "Parker"},
        new String[]{"Christine", "Freeman"},
        new String[]{"Corey", "Keller"},
        new String[]{"James", "Hicks"},
        new String[]{"Nicole", "Petty"},
        new String[]{"Alexandria", "Aguirre"},
        new String[]{"Heather", "Kim"},
        new String[]{"Nichole", "Palmer"},
        new String[]{"Jonathan", "Moore"},
        new String[]{"Cynthia", "Gibbs"},};
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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