Nick Clarkson
Nick Clarkson

Reputation: 1

Java Combo Box Error

I have added a combo box . It needs to have value 1 through 20 and needs to call method from the constructor. Now I am bad at placing codes. I get confused where I need to insert code and always end up getting errors.

I am getting error when i run my program. It says for combo box:No suitable method I have named the box yearscombobox

Please help me fix the error. I do not know if i need to put array for having values 1 through 20? If so, I do not know where to place that code. I have errors in line that I have written in BOLD in the code. Ok i tried bolding it. this code is little different when i copy it from net beans. Anything that has yearscomboox has error saying no suitable method. Help me. I am at your mercy. I hate Java

  package murach.ui;

import java.text.NumberFormat;
 import murach.business.FinancialCalculations;

  public class FutureValueFrame extends javax.swing.JFrame {

   /** Creates new form FutureValueFrame */
    public FutureValueFrame() {
     initComponents();
    }

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
   private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    monthlyPaymentTextField = new javax.swing.JTextField();
    yearlyRateTextField = new javax.swing.JTextField();
    calculateButton = new javax.swing.JButton();
    exitButton = new javax.swing.JButton();
    **YearsComboBox = new javax.swing.JComboBox();**
    jScrollPane1 = new javax.swing.JScrollPane();
    FutureValueList = new javax.swing.JList();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Future Value Calculator");
    setResizable(false);

    jLabel1.setText("Monthly Payment:");

    jLabel2.setText("Yearly Interest Rate:");

    jLabel3.setText("Number of Years:");

    jLabel4.setText("Future Value:");

    calculateButton.setMnemonic('c');
    calculateButton.setText("Calculate");
    calculateButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            calculateButtonActionPerformed(evt);
        }
    });

    exitButton.setMnemonic('x');
    exitButton.setText("Exit");
    exitButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            exitButtonActionPerformed(evt);
        }
    });

    YearsComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20" }));
    YearsComboBox.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            YearsComboBoxActionPerformed(evt);
        }
    });

    FutureValueList.setModel(new javax.swing.AbstractListModel() {
        String[] strings = { "Year 1", "Year 2", "Year 3", "Year 4", "Year 5" };
        public int getSize() { return strings.length; }
        public Object getElementAt(int i) { return strings[i]; }
    });
    jScrollPane1.setViewportView(FutureValueList);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLabel1)
                .addComponent(jLabel2)
                .addComponent(jLabel3)
                .addComponent(jLabel4))
            .addGap(16, 16, 16)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(yearlyRateTextField)
                    .addComponent(monthlyPaymentTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)
                    .addComponent(YearsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addGap(6, 6, 6)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(calculateButton, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                            .addComponent(exitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jScrollPane1))))
            .addGap(36, 36, 36))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1)
                .addComponent(monthlyPaymentTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel2)
                .addComponent(yearlyRateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel3)
                .addComponent(YearsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLabel4)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(calculateButton)
                .addComponent(exitButton))
            .addContainerGap(100, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>


private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
    if (isValidData())
    {
        double p = Double.parseDouble(monthlyPaymentTextField.getText());
        double r = Double.parseDouble(yearlyRateTextField.getText());
        int y = Integer.parseInt(YearsComboBox.getItemCount());

        double fv = FinancialCalculations.calculateFutureValue(p, r, y);

        NumberFormat currency = NumberFormat.getCurrencyInstance();
        FutureValueList.setText(currency.format(fv));
    }
}                                               

private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
    System.exit(0);
}                                          

private void YearsComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
 int y = YearsComboBox.getSelectedIndex();

}

private boolean isValidData()
{
    SwingValidator sv = new SwingValidator();
    return
        sv.isPresent(monthlyPaymentTextField, "Monthly Investment") &&
        sv.isDouble(monthlyPaymentTextField, "Monthly Investment") &&
        sv.isPresent(yearlyRateTextField, "Interest Rate") &&
        sv.isDouble(yearlyRateTextField, "Interest Rate") &&
        sv.isPresent(YearsComboBox, "Number of Years") &&
        sv.isInteger(YearsComboBox, "Number of Years");
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            // new FutureValueFrame().setVisible(true);  // generated code
            FutureValueFrame frame = new FutureValueFrame();
            frame.setVisible(true);
            frame.setLocationRelativeTo(null);
        }
    });
}

// Variables declaration - do not modify
private javax.swing.JList FutureValueList;
public javax.swing.JComboBox YearsComboBox;
private javax.swing.JButton calculateButton;
private javax.swing.JButton exitButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField monthlyPaymentTextField;
private javax.swing.JTextField yearlyRateTextField;
// End of variables declaration

  }

Upvotes: 0

Views: 1062

Answers (1)

mKorbel
mKorbel

Reputation: 109813

  1. for better help sooner post an SSCCE , nobody knows rest of classes ...,

  2. there no issue to put Integer or Double value to the Model for JComboBox or JList

  3. use JFormattedTextField with Number Formatter,

  4. then you can to reduce any possible exceptions from parsing String from / to Integer or Double

  5. you can to use JSpinner with SpinnerDateModel rather than JComboBox

Upvotes: 4

Related Questions