-1

New to using jswing just wondering how to make a button that has some calculation to display a text on a label? Like 3 buttons that holds 3 different value and then have a Calculate button that displays the result to a label?

JRadioButton rdbtnNewRadioButton = new JRadioButton("Single");
    rdbtnNewRadioButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            TotalPay = TotalPay + Single;
        }

JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("Two or more");
    rdbtnNewRadioButton_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            TotalPay = TotalPay + two;
        }
    });

JRadioButton rdbtnWithPet = new JRadioButton("With Pet");
    rdbtnWithPet.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            TotalPay = TotalPay + Pet;
        }
    });

 JButton btnNewButton = new JButton("Calculate");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        //What code to insert to display label result?  
        }
    });

 JLabel lblNewLabel_2 = new JLabel(""); //result of the total must display here?
    lblNewLabel_2.setBounds(232, 47, 110, 69);
    frmPaulasPortraits.getContentPane().add(lblNewLabel_2);
EAbquina
  • 15
  • 2
  • 3
    1) [What have you tried?](http://www.whathaveyoutried.com/) I mean *besides* asking us. 2) For better help sooner, post an [SSCCE](http://sscce.org/). 3) Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Nov 28 '13 at 14:38
  • You shouldn't calculate totalPay until you get in the action listener of the calculate button. – Gilbert Le Blanc Nov 28 '13 at 14:54

2 Answers2

3
  1. Variable name in Java should not start with capital letter.
  2. Always post a SSCCE : instead of a code snippets, which doesn't really reflect whatever you are doing
  3. Declared all of your using component in the class context instead of inside of a function, as it the declared component instance's will be local to function and we might face trouble working with anonymous class and it's function. So declare lblNewLabel_2 to your class context
  4. As all of the RadioButton in your context does the same task: updaing the label's text content with some particular variable try using one instance of ActionLisnter. To do so, implement it to some class MyActionLister implements ActionListener and implement the actionPerformed function. More discussion about the design sense is off-topic as your question currently stays.

However to set text on each radio button's action event, you will need to do something like following:

      public void actionPerformed(ActionEvent e) {
            TotalPay = TotalPay + Pet;
            lblNewLabel_2.setText(""+TotalPay);
        }
Sage
  • 15,290
  • 3
  • 33
  • 38
3

What code to insert to display label result?

1. Insert this one:

lblNewLabel_2.setText(String.valueOf(TotalPay));

2. Initialize your lblNewLabel_2 before the btnNewButton and make it final:

final JLabel lblNewLabel_2 = new JLabel("");

3. Do not use setBounds method for JLabel directly. LayoutManager will do that for you

4. There is a lot of tutorials for Swing, use the Force, Luke!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
SeniorJD
  • 6,946
  • 4
  • 36
  • 53