-1

The title says it. What I wanted to happen is that when I hit 'Nails' button there will be an output of '5' written on the textfield and when I hit 'Steel' button there will be an output of 5+40 (that is 45) on the textfield, and when I hit 'Nails' again it'll be 50 so on.

my code so far

**UPDATE: I GOT IT*******************

private int price;
private int setPrice;

*othercodes here*

private void setPrice(int price) {
    setPrice = setPrice + price;
    textField.setText(Integer.toString(setPrice));
}

btnNails.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            setPrice(5);
        }
*************************************

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTable;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class Frame52 extends JFrame {

    private JPanel contentPane;
    private JTextField textField;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Frame52 frame = new Frame52();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Frame52() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);            
        JButton btnNails = new JButton("Nails");
        btnNails.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int getPrice = 0;
                getPrice = getPrice + 5;
            }
        });
        btnNails.setBounds(43, 70, 114, 23);
        contentPane.add(btnNails);          
        JButton btnWood = new JButton("Wood");
        btnWood.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int getPrice = 0;
                getPrice = getPrice + 20;
            }
        });
        btnWood.setBounds(43, 114, 114, 23);
        contentPane.add(btnWood);

        JButton btnNewButton = new JButton("Galvanized Iron");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int getPrice = 0;
                getPrice = getPrice + 30;
            }
        });
        btnNewButton.setBounds(43, 159, 114, 23);
        contentPane.add(btnNewButton);          
        JButton btnNewButton_1 = new JButton("Steel");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int getPrice = 0;
                getPrice = getPrice + 40;
            }
        });
        btnNewButton_1.setBounds(43, 203, 114, 23);
        contentPane.add(btnNewButton_1);            
        JLabel lblTotalIs = new JLabel("Total is:");
        lblTotalIs.setBounds(203, 163, 46, 14);
        contentPane.add(lblTotalIs);            
        textField = new JTextField();
        textField.setText(Integer.toString(getPrice));
        textField.setBounds(259, 160, 86, 20);
        contentPane.add(textField);
        textField.setColumns(10);           
        JButton btnConfirm = new JButton("Confirm");
        btnConfirm.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            }
        });
        btnConfirm.setBounds(257, 203, 91, 23);
        contentPane.add(btnConfirm);
    }
}
user3736846
  • 5
  • 1
  • 6
  • Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Nov 14 '14 at 13:22
  • See also [`Adder`](http://stackoverflow.com/a/8703807/230513). – trashgod Nov 14 '14 at 16:17

3 Answers3

1

Inside each button listener, you should call a new private method setPrice() that looks like this:

private void setPrice(int price) {
    getPrice = price;
    textField.setText(Integer.toString(getPrice));
}

If you use that to set the value of the price field, the text field will get changed for you at the same time.

You will also need int getPrice and JTextField textField to be instance fields, rather than local variables. As things stand, you're creating a new int getPrice every time you want to update it, which is not right: if you move it to an instance field, you can use it inside your ActionListeners.

chiastic-security
  • 20,430
  • 4
  • 39
  • 67
0

You are not using the actual price you have at each ActionListener

public void actionPerformed(ActionEvent arg0) {
  int getPrice = 0;
  getPrice = getPrice + 40;
}

where do you get the actual price your textfield has?

Instead of the above use this:

public void actionPerformed(ActionEvent arg0) {
  textfield.setText(Integer.parseInt(textField.getText) + 40);
}

Of course your approach has a lot of bloated code, so you can use a single ActionListener instead of anonymous ones by checking the ActionEvent parameter to see which button was pressed.

Eypros
  • 5,370
  • 6
  • 42
  • 75
0

you may try to do something like this for each button:

JButton btnNails = new JButton("Nails");
btnNails.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        incrementPrice(5); // change the integer insede the method
    }
});

and define the method below inside your class(Frame52):

private synchronized void incrementPrice(int value) {
        getPrice = getPrice + value;
        textField.setText(Integer.toString(getPrice));
    }

this solve the problem that you have. this solution is thread safe.

alex_au
  • 240
  • 1
  • 2
  • 11