How to output in JLabel if a JCheckbox is selected?

665 views Asked by At

I am attempting to create a currency conversion program, but the output is not working through a JLabel. I have the program so that there are 6 JCheckboxes, each representing a different currency. When the user presses the "Convert" button, any of the selected JCheckboxes output the result of their specific method (in a different class, I don't think that should need to be posted) into a JLabel. For some reason, I have a "The method setText(String) in the type JLabel is not applicable for the arguments (JCheckbox)." Here is my GUI class:

import java.awt.EventQueue;

public class ConverterGUI {

private JFrame frame;
public static JTextField textField;
public static JLabel label1, label2, label3, label4, label5, label6;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                ConverterGUI window = new ConverterGUI();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public ConverterGUI() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    final JCheckBox euros = new JCheckBox("Euros");
    euros.setBounds(50, 50, 97, 23);
    frame.getContentPane().add(euros);

    final JCheckBox pounds = new JCheckBox("Pounds");
    pounds.setBounds(50, 76, 97, 23);
    frame.getContentPane().add(pounds);

    final JCheckBox pesos = new JCheckBox("Mexican Pesos");
    pesos.setBounds(50, 102, 97, 23);
    frame.getContentPane().add(pesos);

    final JCheckBox canadians = new JCheckBox("Canadian Dollar");
    canadians.setBounds(50, 128, 107, 23);
    frame.getContentPane().add(canadians);

    final JCheckBox yen = new JCheckBox("Yen (Japan)");
    yen.setBounds(50, 154, 97, 23);
    frame.getContentPane().add(yen);

    final JCheckBox yuan = new JCheckBox("Yuan (China)");
    yuan.setBounds(50, 180, 97, 23);
    frame.getContentPane().add(yuan);

    JLabel lblInputAmericanDollars = new JLabel("Input American Dollars:");
    lblInputAmericanDollars.setBounds(116, 11, 117, 14);
    frame.getContentPane().add(lblInputAmericanDollars);

    JLabel lblSelectCurrencies = new JLabel("Select currencies:");
    lblSelectCurrencies.setBounds(40, 29, 107, 14);
    frame.getContentPane().add(lblSelectCurrencies);

    textField = new JTextField();
    textField.setBounds(231, 8, 86, 20);
    frame.getContentPane().add(textField);
    textField.setColumns(10);

    final JLabel label1 = new JLabel("");
    label1.setBounds(163, 54, 46, 14);
    frame.getContentPane().add(label1);

    final JLabel label2 = new JLabel("");
    label2.setBounds(163, 80, 46, 14);
    frame.getContentPane().add(label2);

    final JLabel label3 = new JLabel("");
    label3.setBounds(163, 106, 46, 14);
    frame.getContentPane().add(label3);

    final JLabel label4 = new JLabel("");
    label4.setBounds(163, 132, 46, 14);
    frame.getContentPane().add(label4);

    final JLabel label5 = new JLabel("");
    label5.setBounds(163, 158, 46, 14);
    frame.getContentPane().add(label5);

    final JLabel label6 = new JLabel("");
    label6.setBounds(163, 184, 46, 14);
    frame.getContentPane().add(label6);

    JButton btnConvert = new JButton("Convert");
    btnConvert.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(euros.isSelected() == true){
                Converter.euros();
                label1.setText(euros);
            }
            if(pounds.isSelected() == true){
                Converter.pounds();
                label2.setText(pounds);
            }
            if(pesos.isSelected() == true){
                Converter.pesos();
                label3.setText(pesos);
            }
            if(canadians.isSelected() == true){
                Converter.canadians();
                label4.setText(canadians);
            }
            if(yen.isSelected() == true){
                Converter.yen();
                label5.setText(yen);
            }
            if(yuan.isSelected() == true){
                Converter.yuan();
                label6.setText(yuan);
            }
        }
    });
    btnConvert.setBounds(170, 228, 89, 23);
    frame.getContentPane().add(btnConvert);
}
}
2

There are 2 answers

1
girish946 On BEST ANSWER

You should return the String from the relative methods of Converter Class and then use the code like this

if(yuan.isSelected() == true){

            label6.setText(Converter.yuan());
        }
1
Stultuske On

Well, of course it doesn't. setText takes a String as parameter, not a JCheckBox, which is what you are trying to pass.

try this:

if(yuan.isSelected() == true){
                Converter.yuan();
                label6.setText("yuan");
            }

even better, would be:

if(yuan.isSelected()){
                Converter.yuan();
                label6.setText("yuan");
            }

the '== true' is not necessary, since isSelected() itself already returns a boolean expression.