Java 从netbeans中的另一个表单向JTable添加数据
我有这两种表格。一个包含几个按钮。另一个包含两个表。单击一个按钮,数据保存在数据库中,然后以另一种形式传输到JTable。我只通过第一个表单编写代码。但它不起作用。 代码如下:Java 从netbeans中的另一个表单向JTable添加数据,java,mysql,forms,swing,netbeans,Java,Mysql,Forms,Swing,Netbeans,我有这两种表格。一个包含几个按钮。另一个包含两个表。单击一个按钮,数据保存在数据库中,然后以另一种形式传输到JTable。我只通过第一个表单编写代码。但它不起作用。 代码如下: int dress1=100; DefaultTableModel CurrPurchases=(DefaultTableModel) CurrentPurchases.getModel(); double price1=Double.parseDouble(Price1.getText()); int rows=Cur
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{}
如何以其他形式将数据添加到该表(CurrentBurchases)中?
注意:我不想在表单中添加任何按钮,我只想显示数据
如有任何帮助,将不胜感激。来自评论部分:
CurrentPurchases.getModel()
带红色下划线。事实上
CurrentPurchases
是另一种形式的jtable名称。所以
我现在该怎么办
我现在明白你的问题了。您正试图从另一个窗体直接访问窗体中定义的变量。这就是编译器抱怨的原因。您需要创建此CurrentPurchases
的公共getter,以便从其他类访问它。大概是这样的:
public JTable getCurrentPurchasesTable() {
return this.CurrentPurchases;
}
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;
}
}
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;
}
}
然后进行以下更改:
DefaultTableModel CurrPurchases= (DefaultTableModel) otherFormVariable.getCurrentPurchasesTable().getModel();
当然,您需要引用另一个表单(我在示例中称之为otherFormVariable
),以便按照您的意愿获取表
离题提示:
- 始终尝试跟随。它使人们说同一种语言
编辑:实现示例 既然您是Java新手(欢迎来到这个世界:p),我将给您两个可能的实现示例 首先,假设您有两种形式,如下所示:
:这是您的桌子李>Form1
:在这里,您可以将新记录添加到数据库和上一个数据库中 桌子Form2
public JTable getCurrentPurchasesTable() {
return this.CurrentPurchases;
}
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;
}
}
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;
}
}
示例2:将TableModel设置为Form2
第二种方法是将TableModel
设置为Form2
对象。这样,您就不需要再公开访问您的表了。应该是这样的:
public JTable getCurrentPurchasesTable() {
return this.CurrentPurchases;
}
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;
}
}
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;
}
}
你面临的错误到底是什么?这是个例外吗?编译错误?如果不知道这个错误,很难帮助你。哦,对不起!我的意思是它用红色下划线,这只是一种警告,而不是一个错误。IDE对这行怎么说?为什么它在抱怨?缺少变量?未知标识符?表达式的开头非法?它说“找不到符号”…然后我怀疑这个导入丢失了:
import javax.swing.table.DefaultTableModel代码>。试试看,让我知道它是怎么回事:)对不起,我是新手,我应该在哪里进入公共getter?不用担心,不需要道歉:)假设您的表位于Form1
位置。您应该在此表中添加公共访问权限。现在您有了另一个类Form2
,可以在其中添加数据。因此,第二个必须包含对Form1
的引用,然后才能调用Form1.getCurrentPurchasesTable().getModel()代码>。让我知道是否足够清楚:)@Lululalano不确定我是否用了正确的方法,但我只是按照你的指示做了,它显示了一个警告:非静态方法getCurrentPurchasesTable()不能从静态上下文引用
…我应该在哪里键入公共getter,我的意思是在哪一行或哪一页?@mKorbel我的魔法球失去了几个小时的连接,但现在我认为它又起作用了:P@LuluLala你得到的新错误是因为你没有保留对Form1
对象的任何引用。请参阅更新的答案。我已经包括了两个实现示例。希望这些对您有所帮助,并能引导您走上正确的道路:)