This week's book giveaway is in the General Computing forum.
We're giving away four copies of Raising Young Coders: A Parent’s Guide to Teaching Programming at Home and have Cassandra Chin on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Reading values from comboBoxs with actionListeners

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is this the same problem you have here
http://forum.java.sun.com/thread.jspa?threadID=720898&tstart=0

if so, has it been resolved?
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two possibilities come to mind: a collection like a Map/HashMap and a data store.
In the map you can store a key : value pair. The key can be the String that is displayed in the combo box and you use it to find the Object value from the map. The value can be an Object such as a String, or something more complex.
The data store can store about anything you want. It's an easy way to store, retrieve and organize data.

Another easy way is to put the second array inside the ActionListener class and access it with the selectedIndex. This is more elegant if you really are using a separate ActionListener for each JComboBox.
 
Armygizmo
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi yeah its the same as the one posted in the sun forum I just figured posting in few different forums would increase my chances of getting help

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);
}
}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic