如何从表中获取数据并将其显示在Java中的另一个表中? 我从表中选择一行并获取其值,但我不知道如何将其显示为另一个表。
答案 0 :(得分:0)
将数据从一个表复制到另一个表的Netbean项目 希望这会有所帮助。
import javax.swing.table.DefaultTableModel;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author razak
*/
public class CopyDataFromTableToAnotherTable extends javax.swing.JFrame {
/**
* Creates new form CopyDataFromTableToAnotherTable
*/
public CopyDataFromTableToAnotherTable() {
initComponents();
populateTableToCopyFrom();
}
/**
* This populates tableToCopyDataFrom with test data
*/
private void populateTableToCopyFrom() {
String [] columnName = {"Name","Age"};
DefaultTableModel tableModel = new DefaultTableModel(columnName,0);
for (int i = 0; i < 5; i++){
String temp[] = {"Student " + i, (i+ 1)* 10+ ""};
tableModel.addRow(temp);
}
tableToCopyDataFrom.setModel(tableModel);
}
/**
* This methods copies values from tableToCopyDataFrom to tableToCopyDataTo
*/
private void copyDataToTableFromTable(){
DefaultTableModel tableModel = (DefaultTableModel)tableToCopyDataFrom.getModel();
tableToCopyDataTo.setModel(tableModel);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
tableToCopyDataFrom = new javax.swing.JTable();
jScrollPane2 = new javax.swing.JScrollPane();
tableToCopyDataTo = new javax.swing.JTable();
copyToTableButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
tableToCopyDataFrom.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
}
));
jScrollPane1.setViewportView(tableToCopyDataFrom);
tableToCopyDataTo.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
}
));
jScrollPane2.setViewportView(tableToCopyDataTo);
copyToTableButton.setText("Copy To Table below");
copyToTableButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
copyToTableButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
.addContainerGap(19, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(copyToTableButton)
.addGap(50, 50, 50))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(29, 29, 29)
.addComponent(copyToTableButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(51, 51, 51))
);
pack();
}// </editor-fold>
/**
* Button click that copies data to the second table
* @param evt event listener
*/
private void copyToTableButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
copyDataToTableFromTable();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(CopyDataFromTableToAnotherTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(CopyDataFromTableToAnotherTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(CopyDataFromTableToAnotherTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(CopyDataFromTableToAnotherTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CopyDataFromTableToAnotherTable().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton copyToTableButton;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTable tableToCopyDataFrom;
private javax.swing.JTable tableToCopyDataTo;
// End of variables declaration
}