swing GUI trouble reading values from comboBoxs


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: swing GUI trouble reading values from comboBoxs

  1. #1
    Join Date
    Mar 2006
    Posts
    2

    swing GUI trouble reading values from comboBoxs

    Ok il try to explain it better

    The logic behind it is the user chooses his choice from each comboBox.
    EnsembleSize, baseModel, Randomize and dataChoices

    Each is populated with an array.
    EnsembleSizeItems, baseModelItems, RandomizeItems and dataItems

    I've constructed other arrays that contain actual arguments that I will later develop code to pass to a much larger evaluation class.
    EnsembleSizeRun, baseModelRun, RandomizeRun and dataRun

    Each comboBox is assigned an actionListener which builds a string and depending on the currently selected items in each comboBox and outputs it to label6, This bit works and outputs like you would expect!

    So what i need is instead for it to take in the position of the currently selected value in each comboBox say position 2 of EnsembleSize and it will then look up its corresponding array EnsembleSizeRun[2] and outputting this value out to label6 instead. I would like this to happen in all the actionListeners

    And the reason im doing is just to show users more simpler form of the arguments instead what I would actually pass

    EXAMPLE
    Choosing ensemble size "x 5" instead of showing the user "-E 5 -N 5" which would be the argument I would be passing later on

    Does this make sense? Any help please!!!


    CODE:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;

    public class Menu extends JPanel implements ActionListener
    {
    private JButton Reset;
    private JButton Run;
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JLabel label6;
    private JLabel label7;
    private JComboBox EnsembleSize = null;
    private JComboBox Randomize;
    private JComboBox baseModel = null;
    private JComboBox dataChoices;
    final static int START_INDEX = 0;
    private String Configs;
    private String[] baseModelRun;
    private String[] dataRun;
    private String[] EnsembleSizeRun;
    private String[] RandomizeRun;


    public Menu()
    {
    constructComboBoxs();
    constructComponents();
    addComponents();
    positionComponents();


    // Set ActionListeners
    Run.setActionCommand("RunButton");
    Run.addActionListener(this);
    Reset.setActionCommand("ResetButton");
    Reset.addActionListener(this);

    EnsembleSize.setActionCommand("ensemble");
    EnsembleSize.addActionListener(this);
    baseModel.setActionCommand("base");
    baseModel.addActionListener(this);
    Randomize.setActionCommand("random");
    Randomize.addActionListener(this);
    dataChoices.setActionCommand("data");
    dataChoices.addActionListener(this);



    }

    public void actionPerformed(ActionEvent e)
    {
    if (e.getActionCommand().equals("RunButton"))
    {
    label5.setText("<html><font color=red>RUNNING NOW!!!</font></html>");
    label6.setText("Configs: "+EnsembleSize.getItemAt(EnsembleSize.getSelectedIndex())
    + " - " + baseModel.getItemAt(baseModel.getSelectedIndex()) + " - "
    + Randomize.getItemAt(Randomize.getSelectedIndex()) + " - "
    + dataChoices.getItemAt(dataChoices.getSelectedIndex()));
    EnsembleSize.setEnabled(false);
    baseModel.setEnabled(false);
    Randomize.setEnabled(false);
    dataChoices.setEnabled(false);

    }
    else if(e.getActionCommand().equals("ResetButton"))
    {
    Toolkit.getDefaultToolkit().beep();
    label5.setText("");
    label6.setText("Configs: ");
    EnsembleSize.setEnabled(true);
    baseModel.setEnabled(true);
    Randomize.setEnabled(true);
    dataChoices.setEnabled(true);
    EnsembleSize.setSelectedIndex(0);
    baseModel.setSelectedIndex(0);
    Randomize.setSelectedIndex(0);
    dataChoices.setSelectedIndex(0);
    }
    else if(e.getActionCommand().equals("ensemble"))
    {
    label6.setText("Configs: " + baseModel.getItemAt(baseModel.getSelectedIndex()) + " - "
    + Randomize.getItemAt(Randomize.getSelectedIndex()) + " - "
    + dataChoices.getItemAt(dataChoices.getSelectedIndex()) + " - "
    + EnsembleSize.getItemAt(EnsembleSize.getSelectedIndex()) );

    /*System.out.print(EnsembleSize.getItemAt(EnsembleSize.getSelectedIndex()));
    String bob = EnsembleSizeRun[EnsembleSize.getSelectedIndex()];
    System.out.print(bob);*/
    }


    else if(e.getActionCommand().equals("base"))
    {
    label6.setText("Configs: " + baseModel.getItemAt(baseModel.getSelectedIndex()) + " - "
    + Randomize.getItemAt(Randomize.getSelectedIndex()) + " - "
    + dataChoices.getItemAt(dataChoices.getSelectedIndex()) + " - "
    + EnsembleSize.getItemAt(EnsembleSize.getSelectedIndex()) );
    }
    else if(e.getActionCommand().equals("random"))
    {
    label6.setText("Configs: " + baseModel.getItemAt(baseModel.getSelectedIndex()) + " - "
    + Randomize.getItemAt(Randomize.getSelectedIndex()) + " - "
    + dataChoices.getItemAt(dataChoices.getSelectedIndex()) + " - "
    + EnsembleSize.getItemAt(EnsembleSize.getSelectedIndex()) );
    }
    else if(e.getActionCommand().equals("data"))
    {
    label6.setText("Configs: " + baseModel.getItemAt(baseModel.getSelectedIndex()) + " - "
    + Randomize.getItemAt(Randomize.getSelectedIndex()) + " - "
    + dataChoices.getItemAt(dataChoices.getSelectedIndex()) + " - "
    + EnsembleSize.getItemAt(EnsembleSize.getSelectedIndex()) );
    }
    }


    void constructComboBoxs()
    {
    // construct preComponents with arrays for comboBoxs
    String[] EnsembleSizeItems = {" x 1", " x 5", " x 10"};
    String[] RandomizeItems = {"Compare all methods"," Random Features", " Random Sampling", " Randomise Outputs"};
    String[] baseModelItems = {"Nearest Neighbour", "Linear Regression", "M5P Tree"};
    String[] DataItems = {"autoprice", "BreastTumor", "cholesterol", "housing","pollution"};

    String[] EnsembleSizeRun = {" -E 1 -N 5", " -E 5 -N 5", " -E 10 -N 5"};
    String[] RandomizeRun = {""," -S 0"," -S 1"," -S 2"};
    String[] baseModelRun = {"research.predictors.knn",
    "weka.classifiers.LinearRegression",
    "weka.classifiers.trees.M5P"};

    String[] dataRun = {"-c 16 -t \"C://ensembleResearchNew//data//autoprice.arff\"",
    "-c 10 -t \"C://ensembleResearchNew//data//BreastTumor.arff\"",
    "-c 14 -t \"C://ensembleResearchNew//data//cholesterol.arff\"",
    "-c 14 -t \"C://ensembleResearchNew//data//housing.arff\"",
    "-c 16 -t \"C://ensembleResearchNew//data//pollution.arff\""};


    // construct components
    baseModel = new JComboBox(baseModelItems);
    baseModel.setSelectedIndex(START_INDEX);
    EnsembleSize = new JComboBox (EnsembleSizeItems);
    EnsembleSize.setSelectedIndex(0);
    Randomize = new JComboBox (RandomizeItems);
    Randomize.setSelectedIndex(2);
    dataChoices = new JComboBox (DataItems);
    dataChoices.setSelectedIndex(0);


    // constructs configurations for running experiment
    /*Configs = (baseModelRun[baseModel.getSelectedIndex()] + " "
    + dataRun[dataChoices.getSelectedIndex()]
    + EnsembleSizeRun[EnsembleSize.getSelectedIndex()]
    + RandomizeRun[Randomize.getSelectedIndex()]);*/

    }

    void constructComponents()
    {
    // constructs labels
    label1 = new JLabel ("<html><font color=red size=10>MAIN MENU</font></html>");
    label2 = new JLabel ("Select size of ensemble:");
    label3 = new JLabel ("Select a Base Model:");
    label4 = new JLabel ("Select randomising method:");
    label5 = new JLabel ();
    label6 = new JLabel ();
    label7 = new JLabel ("Select data set:");

    // constructs buttons
    Reset = new JButton ("Reset");
    Run = new JButton ("Run!");
    }


    void addComponents()
    {
    //adds comboBoxs
    add (EnsembleSize);
    add (Randomize);
    add (baseModel);
    add (dataChoices);

    //adds labels
    add (label1);
    add (label2);
    add (label3);
    add (label4);
    add (label5);
    add (label6);
    add (label7);

    //add buttons
    add (Reset);
    add (Run);

    //add message hints to buttons
    Run.setToolTipText("Press this button to run experiment");
    Reset.setToolTipText("Press this button to reset all options for experiment");
    }


    void positionComponents()
    {

    // Sets size and set layout for panel
    setPreferredSize (new Dimension (420, 400));
    setLayout (null);

    /*************************************************************
    *sets component bounds for Absolute Positioning of components*
    * setup guide = name.setBounds(x1, y, x2, height) *
    *************************************************************/

    //sets positions for combo boxes
    EnsembleSize.setBounds (225, 60, 60, 25);
    Randomize.setBounds (225, 170, 150, 25);
    baseModel.setBounds (225, 115, 137, 25);
    dataChoices.setBounds (225, 225, 150, 25);

    //sets positions for labels on panel
    label1.setBounds (100, 0, 300, 40);
    label2.setBounds (65, 60, 145, 25);
    label3.setBounds (80, 114, 122, 25);
    label4.setBounds (42, 170, 166, 25);
    label5.setBounds (175, 350, 100, 25);//RUNNING NOW!!!
    label6.setBounds (10, 275, 700, 25);//configs
    label7.setBounds (110, 225, 150, 25);

    //sets positions for buttons on panel
    Reset.setBounds (125, 315, 100, 20);
    Run.setBounds (215, 315, 100, 20);
    }


    public static void main(String[] args)
    {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame ("JPanel Preview");
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add (new Menu());
    frame.pack();
    frame.setVisible (true);
    }
    }

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    JComboBox allows you to implement listeners in addition to ActionEventListeners. Have you looked at using a ListDataListener or an ItemEventListener?

  3. #3
    Join Date
    Mar 2006
    Posts
    2
    Thanks everyone I've now got this part of GUI complete and will now be moving on to the next stage of creating an array that will pass arguments from the selected items in the comboBoxs method called runExperiment in a file called Evaluation

    String[] options = new String [3];
    options[0] = current value in ComboBox1;
    options[1] = current value in ComboBox2;
    options[2] = current value in ComboBox3;
    options[3] = current value in ComboBox4;
    Evaluation.runExperiment(options);



    This will be my 1st ime writing a line that passes values like this have I got the general idea right? should it be normal brackets? will I have to pass it in as

    Evaluation.runExperiment(options[0], options[1], options[2], options[3]);


    Here my is my working code if anyone is interested in looking, the string I create numerous times configs is what I will be passing something like these to my other method runExperiment

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


    public class Menu extends JPanel implements ActionListener{
    final static int START_INDEX = 0;

    private JButton reset;
    private JButton run;

    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JLabel label6;
    private JLabel label7;

    private JComboBox ensembleSizeComboBox = null;
    String[] ensembleSizeItems = {" x 1", " x 5", " x 10"};
    String[] ensembleSizeRun = {" -E 1 -N 5", " -E 5 -N 5", " -E 10 -N 5"};

    private JComboBox randomizeComboBox;
    String[] randomizeItems
    = {"Compare all methods"," Random Features", " Random Sampling",
    " Randomise Outputs"};
    String[] randomizeRun = {"", " -S 0", " -S 1", " -S 2"};

    private JComboBox baseModelComboBox = null;
    String[] baseModelItems
    = {"Nearest Neighbour", "Linear Regression", "M5P Tree"};
    String[] baseModelRun
    = {"research.predictors.knn", "weka.classifiers.LinearRegression",
    "weka.classifiers.trees.M5P" };;

    private JComboBox dataChoicesComboBox;
    String[] dataChoicesItems
    = {"autoprice", "BreastTumor", "cholesterol", "housing","pollution"};
    String[] dataChoicesRun = {
    "-c 16 -t \"C://ensembleResearchNew//data//autoprice.arff\"",
    "-c 10 -t \"C://ensembleResearchNew//data//BreastTumor.arff\"",
    "-c 14 -t \"C://ensembleResearchNew//data//cholesterol.arff\"",
    "-c 14 -t \"C://ensembleResearchNew//data//housing.arff\"",
    "-c 16 -t \"C://ensembleResearchNew//data//pollution.arff\""};

    private String configs;

    public Menu(){
    setPreferredSize(new Dimension(700, 350));

    constructComboBoxs();
    constructComponents();
    positionComponents();

    run.addActionListener(this);
    reset.addActionListener(this);
    ensembleSizeComboBox.addActionListener(this);
    baseModelComboBox.addActionListener(this);
    randomizeComboBox.addActionListener(this);
    dataChoicesComboBox.addActionListener(this);

    }

    public void actionPerformed(ActionEvent e){
    Object o = e.getSource();

    if (o == run){
    configs = (baseModelRun[baseModelComboBox.getSelectedIndex()] + " "
    + dataChoicesRun[dataChoicesComboBox.getSelectedIndex()]
    + ensembleSizeRun[ensembleSizeComboBox.getSelectedIndex()]
    + randomizeRun[randomizeComboBox.getSelectedIndex()]);

    label6.setText("Configs: " + configs);
    label7.setForeground(Color.red);
    label7.setText("RUNNING NOW!!!");

    ensembleSizeComboBox.setEnabled(false);
    baseModelComboBox.setEnabled(false);
    randomizeComboBox.setEnabled(false);
    dataChoicesComboBox.setEnabled(false);
    }
    else if(o == reset){
    Toolkit.getDefaultToolkit().beep();

    label6.setText("Configs: ");
    label7.setText("");

    ensembleSizeComboBox.setEnabled(true);
    baseModelComboBox.setEnabled(true);
    randomizeComboBox.setEnabled(true);
    dataChoicesComboBox.setEnabled(true);

    ensembleSizeComboBox.setSelectedIndex(0);
    baseModelComboBox.setSelectedIndex(0);
    randomizeComboBox.setSelectedIndex(0);
    dataChoicesComboBox.setSelectedIndex(0);
    }
    else if(o == ensembleSizeComboBox){
    configs = (baseModelRun[baseModelComboBox.getSelectedIndex()] + " "
    + dataChoicesRun[dataChoicesComboBox.getSelectedIndex()]
    + ensembleSizeRun[ensembleSizeComboBox.getSelectedIndex()]
    + randomizeRun[randomizeComboBox.getSelectedIndex()]);

    label6.setText("Configs: " + configs);
    }
    else if(o == baseModelComboBox){
    configs = (baseModelRun[baseModelComboBox.getSelectedIndex()] + " "
    + dataChoicesRun[dataChoicesComboBox.getSelectedIndex()]
    + ensembleSizeRun[ensembleSizeComboBox.getSelectedIndex()]
    + randomizeRun[randomizeComboBox.getSelectedIndex()]);

    label6.setText("Configs: " + configs);
    }
    else if(o == dataChoicesComboBox){
    configs = (baseModelRun[baseModelComboBox.getSelectedIndex()] + " "
    + dataChoicesRun[dataChoicesComboBox.getSelectedIndex()]
    + ensembleSizeRun[ensembleSizeComboBox.getSelectedIndex()]
    + randomizeRun[randomizeComboBox.getSelectedIndex()]);

    label6.setText("Configs: " + configs);
    }
    else if(o == randomizeComboBox){
    configs = (baseModelRun[baseModelComboBox.getSelectedIndex()] + " "
    + dataChoicesRun[dataChoicesComboBox.getSelectedIndex()]
    + ensembleSizeRun[ensembleSizeComboBox.getSelectedIndex()]
    + randomizeRun[randomizeComboBox.getSelectedIndex()]);

    label6.setText("Configs: " + configs);
    }
    }

    void constructComboBoxs(){
    baseModelComboBox = new JComboBox(baseModelItems);
    baseModelComboBox.setSelectedIndex(START_INDEX);
    ensembleSizeComboBox = new JComboBox(ensembleSizeItems);
    ensembleSizeComboBox.setSelectedIndex(0);
    randomizeComboBox = new JComboBox(randomizeItems);
    randomizeComboBox.setSelectedIndex(2);
    dataChoicesComboBox = new JComboBox(dataChoicesItems);
    dataChoicesComboBox.setSelectedIndex(0);
    }

    void constructComponents(){
    label1 = new JLabel();
    label1.setFont(new Font("sanserif", Font.BOLD, 20));
    label1.setForeground(Color.red);
    label1.setText("MAIN MENU");

    label2 = new JLabel("Select size of ensemble:");
    label3 = new JLabel("Select a Base Model:");
    label4 = new JLabel("Select randomising method:");
    label5 = new JLabel("Select data set:");
    label6 = new JLabel(" ");
    label7 = new JLabel(" ");

    label1.setHorizontalAlignment(JLabel.CENTER);
    label2.setHorizontalAlignment(JLabel.RIGHT);
    label3.setHorizontalAlignment(JLabel.RIGHT);
    label4.setHorizontalAlignment(JLabel.RIGHT);
    label5.setHorizontalAlignment(JLabel.RIGHT);
    label6.setHorizontalAlignment(JLabel.LEFT);
    label7.setHorizontalAlignment(JLabel.CENTER);
    reset = new JButton("Reset");
    run = new JButton("Run!");
    }

    void positionComponents(){
    setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    c.insets = new Insets(4, 4, 4, 4);

    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = GridBagConstraints.REMAINDER;
    add(label1, c);

    c.gridwidth = 1;

    c.gridy = 1;
    c.anchor = GridBagConstraints.EAST;
    add(label2, c);
    c.gridx = 1;
    c.anchor = GridBagConstraints.WEST;
    add(ensembleSizeComboBox, c);

    c.gridx = 0;
    c.gridy = 2;
    c.anchor = GridBagConstraints.EAST;
    add(label3, c);
    c.gridx = 1;
    c.anchor = GridBagConstraints.WEST;
    add(randomizeComboBox, c);

    c.gridx = 0;
    c.gridy = 3;
    c.anchor = GridBagConstraints.EAST;
    add(label4, c);
    c.gridx = 1;
    c.anchor = GridBagConstraints.WEST;
    add(baseModelComboBox, c);

    c.gridx = 0;
    c.gridy = 4;
    c.anchor = GridBagConstraints.EAST;
    add(label5, c);
    c.gridx = 1;
    c.anchor = GridBagConstraints.WEST;
    add(dataChoicesComboBox, c);

    c.anchor = GridBagConstraints.CENTER;
    c.gridx = 0;
    c.gridy = 5;
    c.gridwidth = GridBagConstraints.REMAINDER;
    add(label6, c);

    c.gridy = 6;
    c.weightx = 1.0;
    add(label7, c);

    c.gridwidth = 1;

    c.gridx = 0;
    c.gridy = 7;
    c.weightx = 0.5;
    c.anchor = GridBagConstraints.NORTHEAST;
    add(reset, c);
    c.gridx = 1;
    c.anchor = GridBagConstraints.NORTHWEST;
    add(run, c);

    run.setToolTipText("Press this button to run experiment");
    reset.setToolTipText
    ("Press this button to reset all options for experiment");
    }

    public static void main(String[] args){
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("JPanel Preview");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new Menu());
    frame.pack();
    frame.setVisible(true);
    }
    }

Similar Threads

  1. Java Swing GUI Resize Questions
    By lokelo in forum Java
    Replies: 2
    Last Post: 01-20-2006, 02:15 AM
  2. Trouble trying to display integer values
    By tadhg88 in forum ASP.NET
    Replies: 3
    Last Post: 04-11-2005, 01:02 PM
  3. Swing GUI and Multithreading
    By Ketan Taylor in forum Java
    Replies: 0
    Last Post: 04-24-2003, 08:53 AM
  4. Reading control values and id within ASPX
    By Greg Rothlander in forum ASP.NET
    Replies: 2
    Last Post: 06-27-2002, 10:46 AM
  5. Multi-row calculations
    By Bob Hines in forum Database
    Replies: 7
    Last Post: 04-27-2000, 11:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center