0

Mycode

I'm very new to java.My question is,is it possible to use JButton v,v1 ? Inside this actionPerformed() method ? If it is possible,then how ? Thanks in advance .

PlaceOrder.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class PlaceOrder extends JFrame implements ActionListener
{
JButton confirm,cancel,logOut;
JRadioButton rb1,rb2,rb3,rb4,rb5,rb6,rb7,rb8;
JTextField r1,r2,r3,r4,r5,r6,r7,r8;
JLabel Item,Quantity;
Food f[] = new Food[10];

double total = 0;

public PlaceOrder()
{
    f[0] = new Food("Chicken Burger",120);
    f[1] = new Food("Chicken BBQ Burger",150);
    f[2] = new Food("Chicken BBQ Cheese Burger",170);
    f[3] = new Food("Beef Burger",130);
    f[4] = new Food("Beef BBQ Burger",160);
    f[5] = new Food("Pizza",180);
    f[6] = new Food("Coffee",80);
    f[7] = new Food("Soft Drinks(Can)",35);

    setSize(700,700);
    setVisible(true);
    setLayout(null);

    rb1 = new JRadioButton("Chicken Burger");
    rb2 = new JRadioButton("Chicken BBQ Burger");
    rb3 = new JRadioButton("Chicken BBQ Cheese Burger");
    rb4 = new JRadioButton("Beef Burger");
    rb5 = new JRadioButton("Beef BBQ Burger");
    rb6 = new JRadioButton("Pizza");
    rb7 = new JRadioButton("Coffee");
    rb8 = new JRadioButton("Soft Drinks(Can)");

    rb1.setBounds(150,100,150,30);
    add(rb1);
    rb2.setBounds(150,130,150,30);
    add(rb2);
    rb3.setBounds(150,160,150,30);
    add(rb3);
    rb4.setBounds(150,190,150,30);
    add(rb4);
    rb5.setBounds(150,220,150,30);
    add(rb5);
    rb6.setBounds(150,250,150,30);
    add(rb6);
    rb7.setBounds(150,280,150,30);
    add(rb7);
    rb8.setBounds(150,310,150,30);
    add(rb8);

    r1 = new JTextField();
    r2 = new JTextField();
    r3 = new JTextField();
    r4 = new JTextField();
    r5 = new JTextField();
    r6 = new JTextField();
    r7 = new JTextField();
    r8 = new JTextField();

    Item = new JLabel("Item");
    Quantity = new JLabel("Quantity");

    Item.setBounds(160,20,100,50);
    add(Item);
    Quantity.setBounds(360,20,100,50);
    add(Quantity);

    r1.setBounds(350,100,150,20);
    add(r1);
    r2.setBounds(350,130,150,20);
    add(r2);
    r3.setBounds(350,160,150,20);
    add(r3);
    r4.setBounds(350,190,150,20);
    add(r4);
    r5.setBounds(350,220,150,20);
    add(r5);
    r6.setBounds(350,250,150,20);
    add(r6);
    r7.setBounds(350,280,150,20);
    add(r7);
    r8.setBounds(350,310,150,20);
    add(r8);

    confirm = new JButton("Order Status");
    cancel = new JButton("Cancel Order");
    logOut = new JButton("LogOut");

    confirm.setBounds(150,500,120,30);
    add(confirm);

    cancel.setBounds(380,500,120,30);
    add(cancel);

    logOut.setBounds(550,50,80,30);
    add(logOut);

    confirm.addActionListener(this);
    cancel.addActionListener(this);
    logOut.addActionListener(this);
}

public void actionPerformed(ActionEvent a)
{
    if(a.getSource() == confirm)
    {
        JFrame YourOrder = new JFrame();
        YourOrder.setSize(700,800);
        YourOrder.setLayout(null);
        YourOrder.setVisible(true);

        JLabel confirmation = new JLabel("Your Order");
        confirmation.setBounds(250,30,150,30);
        YourOrder.add(confirmation);

        JLabel s[] = new JLabel[3];
        s[0] = new JLabel("Item");
        s[0].setBounds(100,100,50,30);
        YourOrder.add(s[0]);
        s[1] = new JLabel("Quantity");
        s[1].setBounds(250,100,50,30);
        YourOrder.add(s[1]);
        s[2] = new JLabel("Total");
        s[2].setBounds(400,100,50,30);
        YourOrder.add(s[2]);

        JLabel l,l1,l2;
        JLabel netTotal = new JLabel("Net Amount  =  ");
        netTotal.setBounds(400,550,100,30);
        YourOrder.add(netTotal);

        JButton v,v1;
        v = new JButton("Change");
        v.setBounds(100,600,100,30);
        YourOrder.add(v);
        v.addActionListener(this);

        v1 = new JButton("Confirm Order");
        v1.setBounds(400,600,120,30);
        YourOrder.add(v1);
        v1.addActionListener(this);

        if(rb1.isSelected() && r1.getText() != "")
        {
            total = total + ((Integer.parseInt(r1.getText()))*(f[0].getFoodPrice()));
            l = new JLabel(rb1.getText());
            l.setBounds(100,150,100,30);
            YourOrder.add(l);

            l1 = new JLabel(r1.getText());
            l1.setBounds(250,150,100,30);
            YourOrder.add(l1);

            l2 = new JLabel(String.valueOf((Integer.parseInt(r1.getText()))*(f[0].getFoodPrice())));
            l2.setBounds(400,150,100,30);
            YourOrder.add(l2);
        }
        if(rb2.isSelected() && r2.getText() != "")
        {
            total = total + ((Integer.parseInt(r2.getText()))*(f[1].getFoodPrice()));
            l = new JLabel(rb2.getText());
            l.setBounds(100,200,100,30);
            YourOrder.add(l);

            l1 = new JLabel(r2.getText());
            l1.setBounds(250,200,100,30);
            YourOrder.add(l1);

            l2 = new JLabel(String.valueOf((Integer.parseInt(r2.getText()))*(f[1].getFoodPrice())));
            l2.setBounds(400,200,100,30);
            YourOrder.add(l2);
        }
        if(rb3.isSelected() && r3.getText() != "")
        {
            total = total + ((Integer.parseInt(r3.getText()))*(f[2].getFoodPrice()));
            l = new JLabel(rb3.getText());
            l.setBounds(100,250,100,30);
            YourOrder.add(l);

            l1 = new JLabel(r3.getText());
            l1.setBounds(250,250,100,30);
            YourOrder.add(l1);

            l2 = new JLabel(String.valueOf((Integer.parseInt(r3.getText()))*(f[2].getFoodPrice())));
            l2.setBounds(400,250,100,30);
            YourOrder.add(l2);
        }
        if(rb4.isSelected() && r4.getText() != "")
        {
            total = total + ((Integer.parseInt(r4.getText()))*(f[3].getFoodPrice()));
            l = new JLabel(rb4.getText());
            l.setBounds(100,300,100,30);
            YourOrder.add(l);

            l1 = new JLabel(r4.getText());
            l1.setBounds(250,300,100,30);
            YourOrder.add(l1);

            l2 = new JLabel(String.valueOf((Integer.parseInt(r4.getText()))*(f[3].getFoodPrice())));
            l2.setBounds(400,300,100,30);
            YourOrder.add(l2);
        }
        if(rb5.isSelected() && r5.getText() != "")
        {
            total = total + ((Integer.parseInt(r5.getText()))*(f[4].getFoodPrice()));
            l = new JLabel(rb5.getText());
            l.setBounds(100,350,100,30);
            YourOrder.add(l);

            l1 = new JLabel(r5.getText());
            l1.setBounds(250,350,100,30);
            YourOrder.add(l1);

            l2 = new JLabel(String.valueOf((Integer.parseInt(r5.getText()))*(f[4].getFoodPrice())));
            l2.setBounds(400,350,100,30);
            YourOrder.add(l2);
        }
        if(rb6.isSelected() && r6.getText() != "")
        {
            total = total + ((Integer.parseInt(r6.getText()))*(f[5].getFoodPrice()));
            l = new JLabel(rb6.getText());
            l.setBounds(100,400,100,30);
            YourOrder.add(l);

            l1 = new JLabel(r6.getText());
            l1.setBounds(250,400,100,30);
            YourOrder.add(l1);

            l2 = new JLabel(String.valueOf((Integer.parseInt(r6.getText()))*(f[5].getFoodPrice())));
            l2.setBounds(400,400,100,30);
            YourOrder.add(l2);
        }
        if(rb7.isSelected() && r7.getText() != "")
        {
            total = total + ((Integer.parseInt(r7.getText()))*(f[6].getFoodPrice()));
            l = new JLabel(rb7.getText());
            l.setBounds(100,450,100,30);
            YourOrder.add(l);

            l1 = new JLabel(r7.getText());
            l1.setBounds(250,450,100,30);
            YourOrder.add(l1);

            l2 = new JLabel(String.valueOf((Integer.parseInt(r7.getText()))*(f[6].getFoodPrice())));
            l2.setBounds(400,450,100,30);
            YourOrder.add(l2);
        }
        if(rb8.isSelected() && r8.getText() != "")
        {
            total = total + ((Integer.parseInt(r8.getText()))*(f[7].getFoodPrice()));
            l = new JLabel(rb8.getText());
            l.setBounds(100,500,100,30);
            YourOrder.add(l);

            l1 = new JLabel(r8.getText());
            l1.setBounds(250,500,100,30);
            YourOrder.add(l1);

            l2 = new JLabel(String.valueOf((Integer.parseInt(r8.getText()))*(f[7].getFoodPrice())));
            l2.setBounds(400,500,100,30);
            YourOrder.add(l2);
        }

        JLabel pay = new JLabel(String.valueOf(total));
        pay.setBounds(500,550,100,30);
        YourOrder.add(pay);

        if(a.getSource() == v)
        {
            YourOrder.setVisible(false);
        }
        else if(a.getSource() == v1)
        {
            JOptionPane.showMessageDialog(this,"Printing Invoice ...");
        }

    }
    else if(a.getSource() == cancel)
    {
        this.setVisible(false);
        PlaceOrder o1 = new PlaceOrder();
    }
    else
    {
        this.setVisible(false);
    }

}
}

Food.java

class Food 
{
private String foodName;
private double foodPrice;

public Food(String foodName,double foodPrice)
{
    this.foodName = foodName;
    this.foodPrice = foodPrice;
}

public String getFoodName()
{
    return this.foodName;
}

public double getFoodPrice()
{
    return this.foodPrice;
}
}

These two classes ..

inhaler
  • 175
  • 1
  • 2
  • 12
  • 1
    Please don't post code as an image but rather as code-formatted text. For more on this, please see [Why may I not upload images of code on SO when asking a question?](http://meta.stackoverflow.com/a/285557/522444). For one, no one can copy an image into their IDE and run it. – Hovercraft Full Of Eels Dec 03 '16 at 18:11
  • My question is pretty much clear here, & the code is so lengthy that no one will be interested copy and run it .. That's why i uploaded only this portion – inhaler Dec 03 '16 at 18:16
  • 1
    It may be clear to you, but it's not clear to me. Maybe that's just me, but if you don't get a decent solution soon, consider telling more about your problem, and yes, posting your code, whatever code you choose to post, as code-formatted text. Best to create and post a valid [mcve] -- not your whole program, and not a non-runnable snippet, but a small program that we can run and that shows us your problem. – Hovercraft Full Of Eels Dec 03 '16 at 18:18
  • What specifically do you mean by "use inside of this actionPerformed"? Other problems -- it looks like that view creation code should be in its own class and not buried within the actionPerformed. That would probably solve what I *think* is your problem -- gaining access to the JButton variables, since in the new class, those variables will be fields. – Hovercraft Full Of Eels Dec 03 '16 at 18:23
  • Question is Edited,please check again – inhaler Dec 03 '16 at 18:38
  • @inhaler Your requirements aren't very clear from your question. If you're looking to click a JButton via code, you should look at this: [programmatically-clicking-a-gui-button-in-java-swing](http://stackoverflow.com/questions/5109274/programmatically-clicking-a-gui-button-in-java-swing) – byxor Dec 03 '16 at 18:46

1 Answers1

1

My guess: you want to be able to refer to the local variables in your code image above, v and v1, but being local to the method, their scope or "visibility" is also limited to the method. If so, then get that code out of the actionPerformed and instead into its own class, one that creates a JPanel that is set up as you desire. You can then make v and v1 fields of the class, and allow outside code access to necessary properties via getter methods.

If you need more detailed help and less guesses, again please post a viable mcve. If you do this, we can work with and even sometimes help enhance your code.

Other issues: avoid null layouts and setBounds since these lead to rigid and hard to enhance GUI's that look bad on most platforms other than your own.

Also check out The Use of Multiple JFrames, Good/Bad Practice?


Edit

That confirm order window should be a modal JDialog and not a JFrame. You want the program to stop and not go forward until the user has dealt with it, which is what a modal dialog will do. Also, I would use JSpinner and not JTextFields to get the quantity information. Why? You must always assume that the user is an idiot, and using a JSpinner you would prevent the user from entering non-numeric or other incorrect information.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373