我有这两种形式。其中一个包含多个按钮。另一个包含两个表。单击一个按钮,数据将保存在数据库中,然后以另一种形式传输到 JTable。我只通过第一种形式编写代码。但这不起作用。 代码如下:
int dress1=100;
DefaultTableModel CurrPurchases=(DefaultTableModel) CurrentPurchases.getModel();
double price1=Double.parseDouble(Price1.getText());
int rows=CurrPurchases.getRowCount();
if(rows > 0){
for (int i = 0; i < rows; i++) {
CurrPurchases.removeRow(0);
}
}
try{
Connection connection=getConnection();
ResultSet curprs=null;
Statement buy1stmt=connection.createStatement();
String Buy1Query1="Update Products set Quantity=Quantity-1 where Product_no=1;";
String Buy1Query2="Insert into Buy values('"+Pname1.getText()+"',"+price1+");";
buy1stmt.executeUpdate(Buy1Query1);
buy1stmt.executeUpdate(Buy1Query2);
dress1--;
if(dress1==0){
JOptionPane.showMessageDialog(null,"Sorry, This Item is Out of Stock!");
}
new ShoppingCart().setVisible(true);
PreparedStatement buy2stmt=connection.prepareStatement("Select * from Buy;");
curprs=buy2stmt.executeQuery();
if(curprs.last()){
CurrPurchases.addRow(new Object[]{curprs.getString(1),curprs.getDouble(2)});
}
}
catch(Exception ex){
ex.printStackTrace();
}
finally{}
但是这一行具体显示错误(我的意思是用红线下划线):
DefaultTableModel CurrPurchases=(DefaultTableModel) CurrentPurchases.getModel();
如何以其他形式将数据添加到该表(CurrentPurchases)中? 注意:我不想向表单添加任何按钮,我只想显示数据。
如有任何帮助,我们将不胜感激。
最佳答案
来自评论部分:
CurrentPurchases.getModel()
underlined with red. ActuallyCurrentPurchases
is the name of the jtable in the other form. So what should I do now?
我现在明白你的问题了。您正在尝试直接从另一个表单访问在一个表单中定义的变量。这就是编译器提示的原因。您需要为此 CurrentPurchases
创建一个公共(public) getter,以便从其他类访问它。像这样的事情:
public JTable getCurrentPurchasesTable() {
return this.CurrentPurchases;
}
然后进行以下更改:
DefaultTableModel CurrPurchases= (DefaultTableModel) otherFormVariable.getCurrentPurchasesTable().getModel();
当然,您需要引用其他表单(我在示例中将其称为 otherFormVariable
),以便按照您的意愿获取表格。
一个题外话的提示:
- 始终尝试关注Java Code Conventions 。它让人们说同一种语言。
编辑:实现示例
既然您是 Java 新手(欢迎来到这个世界:P),我将为您提供两个可能的实现示例。
首先假设您有两种形式,如下所示:
Form1
:这里放置您的表格。Form2
:在这里您可以向数据库添加一条新记录以及之前的记录 表。
示例 1:授予公众访问 Form1 表格的权限
第一种方法是您正在尝试的方法。要使其以这种方式工作,您需要执行以下操作:
public class Form1 extends JDialog {
JTable currentPurchases;
JButton addRecordButton;
private initComponents() {
addRecordButton = new JButton("Add record");
addRecord.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
Form2 form2 = new Form2();
form2.setForm1(this);
form2.setVisible(true);
}
};
... // init the table and other components
}
public JTable getCurrentPurchasesTable() {
return this.currentPurchases;
}
}
public class Form2 extends JDialog {
Form1 form1; // this variable will keep reference to a Form1 instance
JButton saveRecordButton;
private void initComponents() {
saveRecordButton = new JButton("Save record");
saveRecordButton .addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
DefaultTableModel model = (DefaultTableModel) form1.getCurrentPurchasesTable().getModel();
// add new rocord to the model
}
};
... // init the other components
}
public void setForm1(Form1 f) {
this.form1 = f;
}
}
示例 2:将 TableModel 设置为 Form2
第二种方法是将 TableModel
设置为 Form2
对象。这样您就不再需要公开访问您的表了。应该是这样的:
public class Form1 extends JDialog {
JTable currentPurchases;
JButton addRecordButton;
private initComponents() {
addRecordButton = new JButton("Add record");
addRecord.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
Form2 form2 = new Form2();
form2.setTableModel(currentPurchases.getModel());
form2.setVisible(true);
}
};
... // init the table and other components
}
}
public class Form2 extends JDialog {
TableModel model; // this variable will keep reference to a TableModel instance
JButton saveRecordButton;
private void initComponents() {
saveRecordButton = new JButton("Save record");
saveRecordButton .addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
// add new rocord to the model:
((DefaultTableModel)model).addRow(...);
}
};
... // init the other components
}
public void setTableModel(TableModel m) {
this.model = m;
}
}
关于java - 将数据从 netbeans 中的另一个表单添加到 JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20196897/