1

So what I am trying to achieve here is, if the first radio button and the first option from the first combobox is selected then I want to display the food under that category, if it is first radio button and the second option from the first combobox then some other food under that category. I got the radiobutton part working fine but not able to get the dropdown part along with it. The line rb1.isSelected() && cmbItems[0].isSelected() is giving me the error. Please help me overcome the error. Here is my code:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Tryout extends JFrame  implements ActionListener {

        private static final long serialVersionUID = 1L;

        private JTabbedPane tabbedPane = new JTabbedPane();
        private JPanel inputpanel;
        private JPanel searchpanel;
        public JButton submit;
        public JRadioButton rb1, rb2;
        public JComboBox <String> cmb;
        public String cmbItems [] ={"North Indian","South Indian","East Indian", "West Indian"};
        JFrame frame=new JFrame("Get selected JRadioButton");  

        public Tryout() {           
                inputpanel = createPage1();
                searchpanel = createPage2();
                tabbedPane.addTab("Input Form", inputpanel);
                tabbedPane.addTab("Search Form", searchpanel);
                this.add(tabbedPane, BorderLayout.CENTER);           
        }

        public JPanel createPage1() {           
            String cmbItems2 [] ={"European","Asian","American"};          
            JPanel panel = new JPanel();
            panel.setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
            //Column1
            c.anchor = GridBagConstraints.LINE_START;
            c.weightx = 0.5;
            c.weighty = 0.5;
            JLabel region = new JLabel("Enter Region");      
            c.gridx = 0;
            c.gridy = 0;
            panel.add(region, c);
            JLabel subregion = new JLabel("Enter Sub-Region");
            c.gridx = 0;
            c.gridy = 1;
            panel.add(subregion, c);
            //Column2
            c.anchor = GridBagConstraints.LINE_START;
            ButtonGroup bg = new ButtonGroup();
            rb1 = new JRadioButton("Indian"); 
            c.gridx = 1;
            c.gridy = 0;
            bg.add(rb1);
            panel.add(rb1, c);
            cmb = new JComboBox<String>(cmbItems);
            c.gridx = 1;
            c.gridy = 1;   
            panel.add(cmb, c);        
            //Column3
            c.anchor = GridBagConstraints.LINE_START;
            rb2 = new JRadioButton("International");
            c.gridx = 2;
            c.gridy = 0;           
            bg.add(rb2);
            panel.add(rb2, c);          
            JComboBox<String> cmb2 = new JComboBox<String>(cmbItems2);
            c.gridx = 2;
            c.gridy = 1;      
            cmb2.setEnabled(false);
            panel.add(cmb2, c); 
            submit = new JButton("Submit");
            c.weighty = 10;
            c.anchor = GridBagConstraints.FIRST_LINE_START;
            c.gridx = 1;
            c.gridy = 2;
            panel.add(submit, c);
            submit.addActionListener(this);           
            return panel;            
        }

        public void actionPerformed(ActionEvent e) {
            if(e.getSource()==submit) {
                if(rb1.isSelected() && cmbItems[0].isSelected()) {
                    JOptionPane.showMessageDialog(frame,"You select : "+rb1.getText());
                }
                else if(rb2.isSelected()) {
                    JOptionPane.showMessageDialog(frame,"You select : "+rb2.getText());
                }               
            }           
        }

        public JPanel createPage2() {           
                JPanel panel = new JPanel();
                panel.setLayout(new GridBagLayout());
                GridBagConstraints c = new GridBagConstraints();
                c.fill = GridBagConstraints.RELATIVE;
                JTextField field = new JTextField(20);
                panel.add(field);
                JButton search = new JButton("SEARCH");
                panel.add(search);
                return panel;
        }

        public static void main(String args[]) {
                SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                                Tryout ex = new Tryout();
                                ex.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                ex.setSize(500,500);
                                ex.setVisible(true);
                        }
                });
        }       
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Ankur Sinha
  • 6,473
  • 7
  • 42
  • 73

1 Answers1

2

cmbItems is an array of strings, so naturally it wouldn't know anything about whether it's "selected." You want to ask the combobox itself what the selected item is, so you should say

if(rb1.isSelected() && cmb.getSelectedIndex() == 0) {
    JOptionPane.showMessageDialog(frame,"You select : "+rb1.getText());
}
jedyobidan
  • 876
  • 1
  • 8
  • 14
  • Worked like a charm. I have one more thing to ask you. What I am also trying to do is, keep both the ComboBox disabled, when Indian is selected I want the dropdown corresponding to Indian to be enabled and on clicking the button, show the relevant message. – Ankur Sinha Mar 21 '13 at 02:32
  • add an `ActionListener` to both radio buttons. For the "Indian" ActionListener, enable the combobox with `cmb.setEnabled(true)`. For the "International" ActionListener, disable the combobox with `cmb.setEnabled(false)`. You also might want to disable the combobox when the GUI loads initially – jedyobidan Mar 21 '13 at 02:39
  • I can't get you completely, a small example would help. – Ankur Sinha Mar 21 '13 at 02:45
  • http://pastebin.com/Z3dqjwDN Also, you'll have to make cmb2 final or it won't compile – jedyobidan Mar 21 '13 at 02:50
  • GOT it :) I did using another way with the help of your hint. Thanks a ton :) – Ankur Sinha Mar 21 '13 at 02:53
  • I am having trouble adding images now. I dragged the image into the package and added this code. ImageIcon img = new ImageIcon(getClass().getResource("pic.JPG")); JLabel li = new JLabel(img); panel.add(li); – Ankur Sinha Mar 21 '13 at 07:43