Reputation: 25
I have these two forms. One contains several buttons. The other contains two tables. On the click of one button the data is saved in a database and is then transferred to the JTable in the other form. I wrote the code through the first form only. But it doesn't work. Here's the code:
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{}
But this line in specific shows error(I mean underlined with a red line):
DefaultTableModel CurrPurchases=(DefaultTableModel) CurrentPurchases.getModel();
How can I add the data into that table (CurrentPurchases) in the other form? Note: I don't want to add any button to the form, I just want the data to be displayed.
Any help would be appreciated.
Upvotes: 0
Views: 6962
Reputation: 17971
From comments section:
CurrentPurchases.getModel()
underlined with red. ActuallyCurrentPurchases
is the name of the jtable in the other form. So what should I do now?
I understand your problem now. You're trying to access a variable defined in a form from another form directly. That's why the compiler is complaining. You'll need to create a public getter to this CurrentPurchases
in order to access it from other class. Something like this:
public JTable getCurrentPurchasesTable() {
return this.CurrentPurchases;
}
Then make this change:
DefaultTableModel CurrPurchases= (DefaultTableModel) otherFormVariable.getCurrentPurchasesTable().getModel();
Of course you'll need a reference to the other form (I called it otherFormVariable
in the example) in order to get the table as you wish.
An off-topic tip:
Well as you're new to Java (welcome to this world :P) I'll give you two possible implementation examples.
First off let's say you have 2 forms as follows:
Form1
: here is placed your table. Form2
: here you can add a new record to your DB and the previous
table.The first way is the one you're trying. To make it work in this way you'll need do something like this:
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;
}
}
This second way is about setting a TableModel
to the Form2
object. This way you don't need public access to your table anymore. It should be something like this:
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;
}
}
Upvotes: 1