0

Evening
I am writing a small program to present a UI that when selecting 1 item from a drop down list then gives the user a selection of data relating to their selection. For instance if Ford cortina was selected from a list of Ford cars on screen in the GUI would be presented in text boxes the Cc, Tire pressure, MPg etc etc
My program has 3 classes to present this, the Main class to get it all running the UI class that builds the GUI and the data class that holds the Stringarrays of data for each model of car. The UI class calls the information from the data class.

The problem I have is making use of the selection from the drop down list to then reference the correct stringArray in the other class.

Any advice or help much appreciated.
Ian

//instance version 

sa = new StringArrays(); sa.Cortina;//example
public void actionPerformed(ActionEvent ae)
            {
                int idx = list.getSelectedIndex();
                if(idx != -1)
                    jlab.setText("You selected " + model.getElementAt(idx));
                choice = model.getElementAt(idx);//set variable choice to the selection in this case Cortina
                   //should choice be an object or a string? (neither work).
                System.out.println("test point here " + choice);// Just an on screen tester that displays current definition of choice = Cortina.
                if(idx == 0)
                {
               
                jtxt1.setText("Info 1 = "+ sa.Cortina[0]);// if this was sa.choice[0] it would not work as it looks for a StringArray called choice!

                jtxt2.setText("Info 2 = " + sa.Cortina[1]);
                }
                else
                    if(idx>0)
                    {
                    jlab.setText("No Make selected");
                jtxt1.setText("reset");
                jtxt2.setText("reset ");
                    }
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by quuba
0
final JComboBox list = new JComboBox();
        final JTextArea jtxt1 = new JTextArea();
        final JTextArea jtxt2 = new JTextArea();
        final JLabel jlab = new JLabel();

        final StringArrays sa = new StringArrays();
        final DefaultComboBoxModel model = new DefaultComboBoxModel(new String[]{"Cortina", "Fiesta"});
        list.setModel(model);
        //by default fill fields (idx=0)
        sa.fillFields(0, jtxt1, jtxt2);

        list.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent ae) {
                int idx = list.getSelectedIndex();
                //should choice be an object or a string? (neither work).
                // for Object method toString() is used 
                String choice = (String) model.getElementAt(idx); //set variable choice to the selection in this case Cortina
                jlab.setText("You selected " + choice);
                System.out.println("test point here " + choice);// Just an on screen tester that displays current definition of choice = Cortina.
                // force sa-class to do work for you
                sa.fillFields(idx, jtxt1, jtxt2);
            }
        });

in this case you have two Ford cars identified by index idx:
idx = 0 --> Cortina
idx = 1 --> Fiesta

Edited by quuba: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.