0

http://i415.photobucket.com/albums/pp235/wong93_photos/Untitled-3.png

yo guys I have few questions here, before I ask, what I want do with this program is to count number of toggles that have been pressed and set the total in the jTextField below "Quantity . For example, I pressed on A and B toggle buttons, so quantity will display 2, and jLabel7 will display 20

1) How can I count number of toggles that have been pressed?? I can't find this anywhere

2) How to input value into jTextField?? I know it is basic but I don't know how, I searched around and all of them are about retrieving information from textfield instead of inserting

3) How can I pass the value from jLabel7 to my other jFrame?? Cause I wanna use it as receipt, e.g: Total cost is 20

Thanks a lot !

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user3144549
  • 13
  • 1
  • 1
  • 6
  • 1
    *"yo guys I have few questions here"* For many reasons, SO works best for everyone if you ask one question per thread. Please choose which one you want answered on this thread, and move the other two to new questions. I have voted to close this question as 'too broad', but if you narrow it down to a single question, I'll probably offer an answer. – Andrew Thompson Jan 08 '14 at 12:12
  • *"to my other jFrame"* See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Jan 08 '14 at 12:14

1 Answers1

2
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Example extends JPanel {

    private static final long serialVersionUID = 1L;

    private int toggle_count = 0;
    private JTextField text_field;
    private JLabel label;

    public Example() {

        this.setLayout(new FlowLayout());

        JButton button_a = new JButton("Button A");
        this.add(button_a);

        JButton button_b = new JButton("Button B");
        this.add(button_b);

        text_field = new JTextField("0");
        text_field.setPreferredSize(new Dimension(50, 20));
        text_field.setEditable(false);
        this.add(text_field);

        label = new JLabel("x 10 = 0");
        this.add(label);

        button_a.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub

                updateToggle();
            }
        });

        button_b.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub

                updateToggle();
            }
        });

        JButton receipt = new JButton("Receipt");
        this.add(receipt);

        receipt.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub

                SomeClass.main(toggle_count*10);
            }
        });

    }

    public void updateToggle() {

        toggle_count++;
        text_field.setText("" +toggle_count);
        label.setText("x 10 = " + toggle_count * 10);
    }

    public static void main(String[] args) {

        JFrame frame = new JFrame("Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(640, 480);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setContentPane(new Example());

        frame.setVisible(true);
    }
}

The SomeClass is the other class with the main method that takes your toggle_count*10 as an argument

Morten Høgseth
  • 338
  • 2
  • 4
  • 17