让我分两部分讨论我的问题。
JTextfiled
中的JTable
检索价值? 姓名和年龄应从JTextfield
jbutton1
行动开始追溯
通过单击jbutton2
,Jtable
的所有数据都应存储在SQL DB中。
以下是我的代码:
package tablefilterdemo;
public class Tableinputhroughtextbox extends javax.swing.JFrame {
public Tableinputhroughtextbox() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jButton1 = new javax.swing.JButton();
jtfname = new javax.swing.JTextField();
jtfage = new javax.swing.JTextField();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null},
{null, null},
{null, null},
{null, null}
},
new String [] {
"Name", "Age"
}
));
jScrollPane1.setViewportView(jTable1);
jButton1.setText("Submit for Table");
jButton2.setText("Submit for SQL");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(29, 29, 29)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(53, 53, 53)
.addComponent(jtfname, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(46, 46, 46)
.addComponent(jtfage, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jButton2)))
.addContainerGap(82, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(92, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jtfname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jtfage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2))
.addGap(47, 47, 47)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 322, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(19, 19, 19))
);
pack();
}// </editor-fold>
/**
* @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(Tableinputhroughtextbox.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Tableinputhroughtextbox.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Tableinputhroughtextbox.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Tableinputhroughtextbox.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 Tableinputhroughtextbox().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JTextField jtfage;
private javax.swing.JTextField jtfname;
// End of variables declaration
}
答案 0 :(得分:4)
1)如何从JTable中的textfiled中检索值?
我认为你的意思是JTextField?请注意拼写,因为编程是一项精确的练习,所以你不能轻率地编写或你的问题。
您可以通过getText()
方法获得教程告诉您的文字。请看一下here。
2)所有数据如何一次性存储在数据库中?
请澄清你的问题。如果您正在查看如何使用Java数据库,请查看here。有关如何使用JTable和表模型,请查看here。我建议您将数据库代码与Swing代码分开,以帮助简化调试和编码。
编辑1
关于将JTable与数据库一起使用,在这个问题上已经发布了大量的答案,你可以在这个网站上搜索这些答案。这不是一件小事,需要你做一些研究。例如,请查看以下链接:
编辑2
你在评论中提问:
好的让我们坚持一个问题。在上面的代码中我创建了2个JTexfield和Button.Now在这里我想要那些检索到的数据如何在Jtable中显示?
如果您希望将两个JTextField中的数据放在JTable的一行中,请考虑执行以下操作:
Vector<String>
或String数组,将两个字符串放入Vector或数组中,然后在DefaultTableModel变量上调用addRow(rowData)
。这假定向量或数组名为rowData。addRow(...)
方法,并且您可能会传入一个包含两个部分的自定义类的对象数据的。如果您选择此路线,请确保您的addRow(...)
方法调用正确的fireTableXXX(...)
方法,此处为fireTableRowsInserted(int firstRow, int lastRow)
。但最重要的是,请务必阅读我上面链接的JTable教程,否则我的答案对你没有意义。
编辑3
例如,使用DefaultTableModel:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
@SuppressWarnings("serial")
public class MyTableEg extends JPanel {
private static final String[] COLUMNS = {"Column A", "Column B"};
private DefaultTableModel model = new DefaultTableModel(COLUMNS, 0);
private JTable table = new JTable(model);
private JTextField fieldA = new JTextField(10);
private JTextField fieldB = new JTextField(10);
private JButton button = new JButton(new ButtonAction("Add Data", KeyEvent.VK_A));
public MyTableEg() {
JPanel topPanel = new JPanel();
topPanel.add(fieldA);
topPanel.add(fieldB);
topPanel.add(button);
setLayout(new BorderLayout());
add(topPanel, BorderLayout.NORTH);
add(new JScrollPane(table), BorderLayout.CENTER);
}
private class ButtonAction extends AbstractAction {
public ButtonAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent evt) {
// ***** here's the important bit of code
Vector<String> rowData = new Vector<String>(); // create a row Vector
rowData.add(fieldA.getText()); // fill it with data from JTextFields
rowData.add(fieldB.getText());
model.addRow(rowData); // and add to table model
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("MyTableEg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new MyTableEg());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
答案 1 :(得分:0)
//I guess you already got value into your JTextField
//Here I'm getting only two column into my JTable, you can take as many as you need
jbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//TODO Auto-generated method stub
DefaultTableModel model=(DefaultTableModel) table.getModel();
try
{
String s1,s2;
s1=tfield1.getText();
s2=tfield2.getText();
model.addRow(new Object[] {s1,s2});
}
catch(Exception ae){
ae.printStackTrace();
}
}
});
可能会有所帮助:)