Silver Archer Silver Archer - 7 months ago 19
Java Question

How to display only the data which button was clicked ?

I was working on a Bakery Sales System project.
I separate the order page and payment page into two frame.
I want to display the data behind the button in the Payment frame from the Order frame only when the button is clicked.

Is there a way of doing it?

Here is my code for Order Page:

import java.text.DecimalFormat;
class Bun extends javax.swing.JFrame {
String decimal = "#.##";
DecimalFormat df = new DecimalFormat(decimal);
public final String [] bname = new String [] {"Tuna Sandwich","Garlic Bread","Redbean Bun","Sausage Bun","Chicken Burger","Ham Sandwich","Egg Mayo Bun","Chocolate Bread","Potato Bun"};
public final double [] bprice = new double [] {1.20, 2, 1.50, 2, 3, 2, 2, 1.50, 1.20};
public final double [] bsub = new double [] {0,0,0,0,0,0,0,0,0};
public double bsubtotal=0;

public Bun() {
initComponents();
}

private void BtnTunaSandwichActionPerformed(java.awt.event.ActionEvent evt) {
BtnTunaSandwich.setText(bname[1]+" - RM "+bprice[1]);
}

private void BtnGarlicBreadActionPerformed(java.awt.event.ActionEvent evt) {
BtnGarlicBread.setText(bname[2]+" - RM "+bprice[2]);
}

private void BtnRedbeanBunActionPerformed(java.awt.event.ActionEvent evt) {
BtnRedbeanBun.setText(bname[3]+" - RM "+bprice[3]);
}

private void BtnSausageBunActionPerformed(java.awt.event.ActionEvent evt) {
BtnSausageBun.setText(bname[4]+" - RM "+bprice[4]);
}

private void BtnChickenBurgerActionPerformed(java.awt.event.ActionEvent evt) {
BtnChickenBurger.setText(bname[5]+" - RM "+bprice[5]);
}

private void BtnHamSandwichActionPerformed(java.awt.event.ActionEvent evt) {
BtnHamSandwich.setText(bname[6]+" - RM "+bprice[6]);
}

private void BtnEggMayoBunActionPerformed(java.awt.event.ActionEvent evt) {
BtnEggMayoBun.setText(bname[7]+" - RM "+bprice[7]);
}

private void BtnChocolateBreadActionPerformed(java.awt.event.ActionEvent evt) {
BtnChocolateBread.setText(bname[8]+" - RM "+bprice[8]);
}

private void BtnPotatoBunActionPerformed(java.awt.event.ActionEvent evt) {
BtnPotatoBun.setText(bname[9]+" - RM "+bprice[9]);
}

private void numTunaSandwichActionPerformed(java.awt.event.ActionEvent evt) {
bsub[1] = Integer.parseInt(numTunaSandwich.getText())*bprice[1];
}

private void numGarlicBreadActionPerformed(java.awt.event.ActionEvent evt) {
bsub[2] = Integer.parseInt(numGarlicBread.getText())*bprice[2];
}

private void numRedbeanBunActionPerformed(java.awt.event.ActionEvent evt) {
bsub[3] = Integer.parseInt(numRedbeanBun.getText())*bprice[3];
}

private void numSausageBunActionPerformed(java.awt.event.ActionEvent evt) {
bsub[4] = Integer.parseInt(numSausageBun.getText())*bprice[4];
}

private void numChickenBurgerActionPerformed(java.awt.event.ActionEvent evt) {
bsub[5] = Integer.parseInt(numChickenBurger.getText())*bprice[5];
}

private void numHamSandwichActionPerformed(java.awt.event.ActionEvent evt) {
bsub[6] = Integer.parseInt(numHamSandwich.getText())*bprice[6];
}

private void numEggMayoBunActionPerformed(java.awt.event.ActionEvent evt) {
bsub[7] = Integer.parseInt(numEggMayoBun.getText())*bprice[7];
}

private void numChocolateBreadActionPerformed(java.awt.event.ActionEvent evt) {
bsub[8] = Integer.parseInt(numChocolateBread.getText())*bprice[8];
}

private void numPotatoBunActionPerformed(java.awt.event.ActionEvent evt) {
bsub[9] = Integer.parseInt(numPotatoBun.getText())*bprice[9];
}

public void Calculatetotalb(){
for(double i : bsub){
bsubtotal += i;
}
}


I haven't write anything yet in the Payment frame.
But here is my order frame looks like:

Order Frame

And here is my payment frame looks like :

Payment frame

I want to display the data into that Jlist.
Please help, thanks a lot.

Answer

well transferring data from one frame to another is nothing but exchanging data between objects. first thing you need to do is implement a method inside PaymentFrame which will accept data and change the list

class PaymentFrame{

public void setData(String list){
//get the list from the method and update the jlist
jlist.add(list);
}

}

and the next step is to call this method from another class inyour case Orderframe

class orderFrame{
paymentframe pay; 
public void sendData(String data){
if(pay==null){
pay = new paymentframe();
 }    
pay.setdata(data);
pay.setvisible(true);
}

}

here is an example of doing what you have asked bt if you have already opened a PaymentFrame in your project you should access the same object from the order frame.

Comments