0

I'm trying to add some buttons in a JPanel from a comboBox. The comboBox has an array of 8 ints and when one of them is selected I want to be able to press a go button which will then display the number of buttons selected from the comboBox into a JPanel.

The JPanel is initially empty and the go button is disabled until something is selected.

I have created the JPanel, comboBox and the go button, but I'm now lost as to how to get and create the buttons.

The comboBox filled with Strings -

String[] floorStrings = {"Select one", "1", "2", "3", "4", "5", "6", "7", "8"};
//Create the combo box
JComboBox floorList = new JComboBox(floorStrings);

The actionPerformed code -

        public void actionPerformed(ActionEvent e) {

        floorList.getSelectedIndex();
        //int i = Integer.valueOf((String) floorList);

    if (e.getSource() == go) {
        go.setText("Stop");
        System.out.print("Clicked " + floorList);
        p3.add(go, BorderLayout.NORTH);
    } 

}
  • 1
    1) The [Nested Layout Example](http://stackoverflow.com/a/5630271/418556) dynamically adds labels. Make an attempt based on that. 2) *"I'm trying.."* For better help sooner, post an [SSCCE](http://sscce.org/) (the code linked above is an SSCCE - or maybe an MSCCE). – Andrew Thompson Apr 11 '13 at 00:32
  • Can you also post your code here? – Ryan Apr 11 '13 at 01:00

1 Answers1

1

Attach an ActionListener to the "Go" button. Within the actionPerformed method you need to get the value from the JComboBox, simply use getSelectedValue. This returns a Object. Check that the object is not null and try and cast it to an int (ie (int)value).

If the cast is sucessful, simply create a for-next loop that loops n number of times, based on the value from the combo box and create your buttons, adding them to your panel.

Take a look at How to Write an Action Listener and How to use Combo Boxes and The for Statement for more details

Update with example

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.management.StringValueExp;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestComboBox08 {

    public static void main(String[] args) {
        new TestComboBox08();
    }

    public TestComboBox08() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JComboBox floorList;
        private JPanel buttons;

        public TestPane() {
            setLayout(new BorderLayout());
            String[] floorStrings = {"Select one", "1", "2", "3", "4", "5", "6", "7", "8"};
            floorList = new JComboBox(floorStrings);
            JButton go = new JButton("Go");
            go.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    int count = floorList.getSelectedIndex();
                    buttons.removeAll();
                    for (int index = 0; index < count; index++) {
                        buttons.add(new JButton(String.valueOf(index)));
                    }
                    buttons.revalidate();
                }
            });
            JPanel top = new JPanel(new FlowLayout(FlowLayout.CENTER));
            top.add(floorList);
            top.add(go);

            buttons = new JPanel(new GridLayout(0, 4));
            buttons.setPreferredSize(new Dimension(200, 200));

            add(top, BorderLayout.NORTH);
            add(buttons);
        }
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • What, no screen-shot? ;) Good answer. – Andrew Thompson Apr 11 '13 at 00:35
  • @AndrewThompson Next time, I promise ;) – MadProgrammer Apr 11 '13 at 00:36
  • 1
    Thanks guys. I'll give it a shot and see how I go. I'll try to report back soon. – Jason Leverton Apr 11 '13 at 01:01
  • Ok so I am half way there. Now I got caught on the casting. Here is what I have... //start of the actionperformed @Override public void actionPerformed(ActionEvent e) { floorList.getSelectedIndex(); //int i = Integer.valueOf((String) floorList); if (e.getSource() == go) { go.setText("Stop"); System.out.print("Clicked " + floorList); p3.add(go, BorderLayout.NORTH); } } //end of the actionperformed – Jason Leverton Apr 11 '13 at 01:33
  • `floorList.getSelectedIndex();` isn't going anything. Not sure if it's what you want, but, if the list of values is in order from 0-n it's actually not a bad idea, but you need to assign it to something. `int i = Integer.valueOf((String) floorList)` is never going to work, as far as I can tell, `floorList` is your combo box and not a `String`. It might be better to update you question with these details in the future ;) – MadProgrammer Apr 11 '13 at 01:38
  • @JasonLeverton You need to assign the return value of `floorList.getSelectedIndex();` to something. This will give you the bases for your `for-next` loop – MadProgrammer Apr 11 '13 at 02:11
  • @MadProgrammer Can you please give me an example please? – Jason Leverton Apr 11 '13 at 09:16