0

I wrote code that creates a JComboBox with three options and is supposed to open a new JFrame when one is selected, but nothing happens. Where have I gone wrong?

    JComboBox comboBox = new JComboBox();
    comboBox.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent arg0) {
            JComboBox comboBox = (JComboBox) arg0.getSource();
            Object newItem = comboBox.getSelectedItem();

             boolean same = newItem.equals(oldItem);
             oldItem = newItem;

             if (" Loan".equals(arg0.getActionCommand())){
                 Loan loan = new Loan();
                 loan.setVisible(true);
                 frame.setVisible(false);

             }
             else if (" Return".equals(arg0.getActionCommand())){
                 //Code to open new jframe.
             }
             else if (" Renew".equals(arg0.getActionCommand())){
                 //Code to open new jframe.
             }
        }
    });
  • 1) For better help sooner post a proper [mcve], 2) See [The use of multiple Frames, Good / Bad practice?](https://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-or-bad-practice) (The general consensus says it's bad). Instead build your GUI towards `JPanel`s using Dialogs or a `CardLayout` for switching views and asking for more information or displaying it to the user. Regarding point 1, we don't know how does `Loan` looks like, but I guess it extends `JFrame` and it's never a good practice either, because `JFrame` is a rigid container that cannot be contained by others.. – Frakcool Oct 30 '17 at 17:50
  • ... (continuation): See [Extends JFrame or creating it inside the program](https://stackoverflow.com/questions/22003802/extends-jframe-vs-creating-it-inside-the-program) for extra information – Frakcool Oct 30 '17 at 17:51
  • `Where have I gone wrong?` - we don't know. You tell us. Did you add any debug code? Does the ActionListener code get executed?. Do any of the if conditions get executed? Do you combo box items have a space at the beginning? – camickr Oct 30 '17 at 19:13
  • Does `" Loan"` really have a leading space? – MadProgrammer Oct 30 '17 at 20:23
  • `Does " Loan" really have a leading space? ` - already mentioned in my comment. Although I guess my comment was designed to "teach someone how to fish" rather than "give them a fish" by showing some basic debugging techniques. – camickr Oct 30 '17 at 20:32

0 Answers0