[英]How to copy row cell values from a JTable and transfer to another JTable
我想从我的第一个表格的行单元格中获取值,然后通过单击按钮将其传输到我的第二个表格,但我未能实现。 我尝试使用for
循环均匀分配每行索引,但它只添加了两次具有重复单元格值的行。 下面是我的代码。
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);
我只是想澄清一下,这里的mainTable
是第一个表, showTable
是第二个表
在一个非常简单的层面上,你可以做一些像......
int[] selectedRows = leftTable.getSelectionModel().getSelectedIndices();
for (int row : selectedRows) {
int modelRow = leftTable.convertRowIndexToModel(row);
rightModel.addRow(leftModel.getDataVector().get(modelRow));
}
如果您的模型使用不同的结构,那么您需要在两者之间转换数据,例如,如果我将rightModel
更改为...
private DefaultTableModel rightModel = new DefaultTableModel(new String[]{"Name"}, 0);
然后我需要做一些更像......
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:这只是复制选定的行
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"},};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.