Search code examples
javaswingawtjtextfieldjcombobox

JComboBox, ActionListener, How do I really use them?


Im currently learning java and stuck onto JComboBox. I have a feel things that I am trying out and hitting the wall for the pass 4 hours.

I am trying to let a user select 1-10 from a ComboBox. How do I get the value of the combobox? The value of the combo box is equivalent to quantity.

So I have another value which is maybe $10. If the user choose quantity 2.

I want to get the value of what the user choose, then take the value of $10 and times it by 2.

The result which is $20 will be displayed on the JTextField.

Please help :(

public class Panel extends JPanel {

    public Panel(){
        JPanel test = new JPanel(new GridBagLayout());

        String[] quantities1 = {"0","1","2","3","4","5","6","7","8","9","10"};
        JComboBox quantitiesCB = new JComboBox(quantities1);
        quantitiesCB.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        JComboBox combo = (JComboBox)e.getSource();
                        String currentQuantity = (String)combo.getSelectedItem();
                    }
                }            
        );

        JTextField result = new JTextField();

        setLayout(new GridBagLayout());
        setPreferredSize(new Dimension(640,480));
        GridBagConstraints gbc = new GridBagConstraints();

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0.1;
        gbc.weighty = 0.1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.NORTH;
        add(quantitiesCB, gbc);
    } 
}

Solution

  • Just few changes:

    public class Panel extends JPanel {
    
        public Panel(){
            JPanel test = new JPanel(new GridBagLayout());
            String value = "10";
            final JTextField result = new JTextField();
    
            String[] quantities1 = {"0","1","2","3","4","5","6","7","8","9","10"};
            JComboBox quantitiesCB = new JComboBox(quantities1);
            quantitiesCB.addActionListener(
                    new ActionListener(){
                        public void actionPerformed(ActionEvent e){
                            JComboBox combo = (JComboBox)e.getSource();
                            String currentQuantity = (String)combo.getSelectedItem();
                            int value1 = Integer.valueOf(value);
                            int value2 = Integer.valueOf(currentQuantity);
    
                            String resultText = String.valueOf(value1*value2);
                            result.setText("$" + resultText);
                        }
                    }            
            );
    
            setLayout(new GridBagLayout());
            setPreferredSize(new Dimension(640,480));
            GridBagConstraints gbc = new GridBagConstraints();
    
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 0.1;
            gbc.weighty = 0.1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.anchor = GridBagConstraints.NORTH;
            add(quantitiesCB, gbc);
        } 
    }