Questions on Implementing JComboBox with ActionListening device

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 6995 Views - Last Post: 24 April 2011 - 04:37 AM Rate Topic: -----

#1 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

Questions on Implementing JComboBox with ActionListening device

Posted 18 April 2011 - 10:51 AM

Quick Update: I am working on an area solving equation where the user can pick which method he/she would like to use to solve for the area. There are four methods, three of which that deal with perfect geometric relationships and the fourth an extraneous way to solve the area of any irregular shape in a coordinate plane, provided that the lines of the shape do not intersect. Anyhow, I have the formulae written properly, thus is not the source of the problem here, but rather my problem pertains to the structure of my code. I have it set up where I want the code to display certain JLabels, JFields, JButtons, & etc. when a specific object within the JComboBox is selected, but I get no response from the code. No errors, no nothing, which leads me to assume it is working just fine and the problem is my lack of knowledge. This is a first time for me doing JComboBoxes, so all that I do know was what I barely pulled off the internet and synthesized myself. So any further indepth insights or linked tutorials would be awesome.

I will provide the following code as well as the equation sub-program in an attachment (is a bit long).

The following is where I desire a calling device/listener/whatever to do what i want below it when the appropriate tag is selected:

public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() instanceof JComboBox)
		{
			JComboBox selected = (JComboBox)(e.getSource());
			//default drop-down menu option, does nothing other than set visibility to false on everything
			if(selected == equationComboBox)
			{
				System.out.println("default option selected");
				
				numberBox.setVisible(false);
				numberField.setVisible(false);
				variableBox.setVisible(false);
				variableField.setVisible(true);
				XCoordinateBox.setVisible(false);
				XCoordinateField.setVisible(false);
				YCoordinateBox.setVisible(false);
				YCoordinateField.setVisible(false);
				areaAnswerBox.setVisible(false);
				areaBox.setVisible(false);
				Compute.setVisible(false);
				Clear.setVisible(false);
			}

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Questions on Implementing JComboBox with ActionListening device

#2 MrLuke187  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 81
  • View blog
  • Posts: 237
  • Joined: 09-July 10

Re: Questions on Implementing JComboBox with ActionListening device

Posted 18 April 2011 - 11:47 AM

Do you mean something like this:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;

public class ActionComboBox implements ActionListener{
	
	private JComboBox box;
	
	public ActionComboBox(){
		JFrame f = new JFrame();
		String[] box_in = {"USA", "Germany", "Asia", "Africa"};
		box = new JComboBox(box_in);
		box.addActionListener(this);
		f.add(box);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.pack();
		f.setVisible(true);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == box){
			System.out.println("Slected: Nr."+box.getSelectedIndex()+
					" Value: \""+box.getSelectedItem().toString()+"\"");
		}
	}
	
	public static void main(String[] args){
		new ActionComboBox();
	}

}



Greetings: Luke
Was This Post Helpful? 0
  • +
  • -

#3 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

Re: Questions on Implementing JComboBox with ActionListening device

Posted 18 April 2011 - 12:49 PM

That depends if it can be made to work with the mathematical functions I need it to operate with when data is inputed in a JField. The code shown was only my default option, therefore (in hindsight)irrelevent to what I want to do with the selected option. Did you first check out the attached files? I'll mess with your idea to see if it gets me results that I want.
Was This Post Helpful? 0
  • +
  • -

#4 MrLuke187  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 81
  • View blog
  • Posts: 237
  • Joined: 09-July 10

Re: Questions on Implementing JComboBox with ActionListening device

Posted 18 April 2011 - 01:01 PM

Quote

Did you first check out the attached files?

No, I didn't. You shouldn't attach Source Code to a post, just post the relevant code-lines with your question.

Quote

if it can be made to work with the mathematical functions I need it to operate with when data is inputed in a JTextField.

This depends on the mathematical functions you want to use and how you want to use them. If you simply want to add a new entry to the JComboBox, the addItem()-method should do the job.

Greetings: Luke
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Questions on Implementing JComboBox with ActionListening device

Posted 18 April 2011 - 08:42 PM

Is this statement

System.out.println("default option selected");

executed ?
Was This Post Helpful? 0
  • +
  • -

#6 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

Re: Questions on Implementing JComboBox with ActionListening device

Posted 19 April 2011 - 12:48 PM

View Postpbl, on 18 April 2011 - 09:42 PM, said:

Is this statement

System.out.println("default option selected");

executed ?

I wrote that as a statement to see if on the Java environment that it has read that bit of code to help me see if it was operating correctly, so its technically superfluous. Please ignore it.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Questions on Implementing JComboBox with ActionListening device

Posted 19 April 2011 - 07:45 PM

View PostBloodyInari, on 19 April 2011 - 02:48 PM, said:

View Postpbl, on 18 April 2011 - 09:42 PM, said:

Is this statement

System.out.println("default option selected");

executed ?

I wrote that as a statement to see if on the Java environment that it has read that bit of code to help me see if it was operating correctly, so its technically superfluous. Please ignore it.

This is precisely what I want to test
I want to know if you enter the if()
So is it executed ?
Was This Post Helpful? 0
  • +
  • -

#8 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

Re: Questions on Implementing JComboBox with ActionListening device

Posted 20 April 2011 - 06:32 AM

Unfortunately no, I dont seem to know how to get the "if" statement to execute when the appropriate JComboBox option is selected
Was This Post Helpful? 0
  • +
  • -

#9 MrLuke187  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 81
  • View blog
  • Posts: 237
  • Joined: 09-July 10

Re: Questions on Implementing JComboBox with ActionListening device

Posted 20 April 2011 - 07:17 AM

So, where doe's equationComboBox come from?
I guess it's an Object (maybe a JComboBox?). If you want to compare Objects, use the equals()-method.

Also, i don't quite understand why you don't simply check the selected Item to see if it's your default selection.

Greetings: Luke
Was This Post Helpful? 0
  • +
  • -

#10 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

Re: Questions on Implementing JComboBox with ActionListening device

Posted 20 April 2011 - 10:44 AM

View PostMrLuke187, on 20 April 2011 - 08:17 AM, said:

So, where doe's equationComboBox come from?
I guess it's an Object (maybe a JComboBox?).


You are correct that equationComboBox is an Object. see the posted source code for more details. its the string array which gives the names of each option I desire to be selected. And to reiterate, I have barely any idea how to implement JComboBoxes, this IS my first time doing them and that's about all I could pull out of tutorials and what not that made sense to me.
Was This Post Helpful? 0
  • +
  • -

#11 MrLuke187  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 81
  • View blog
  • Posts: 237
  • Joined: 09-July 10

Re: Questions on Implementing JComboBox with ActionListening device

Posted 20 April 2011 - 03:55 PM

You can't simply compare a JComboBox with a String Array.

You can use the getSelectedItem()-method of the JComboBox to receive the Object on the selected entry. If you filled your ComboBox with Strings, it'll return a String.

Greetings: Luke
Was This Post Helpful? 1
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Questions on Implementing JComboBox with ActionListening device

Posted 20 April 2011 - 05:50 PM

View PostMrLuke187, on 20 April 2011 - 09:17 AM, said:

So, where doe's equationComboBox come from?
I guess it's an Object (maybe a JComboBox?). If you want to compare Objects, use the equals()-method.

Not in that case :)
Yes I know many cases when we say to compare object with equals() but here we want to know if it is the SAME comboBox not two equal ComboBoc

if(selected == equationComboBox)

isn't your ComboBox named "box" ?

Post your code
Was This Post Helpful? 0
  • +
  • -

#13 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

Re: Questions on Implementing JComboBox with ActionListening device

Posted 21 April 2011 - 06:42 AM

View PostMrLuke187, on 20 April 2011 - 04:55 PM, said:

You can't simply compare a JComboBox with a String Array.

You can use the getSelectedItem()-method of the JComboBox to receive the Object on the selected entry.


so I need to create an Object for each entry space to my appropriate selection?
Hmm, so how would you recomend doing that?

Oh and propably should have added this too right? Kinda useless without it.

equationComboBox.addActionListener(this);


Was This Post Helpful? 0
  • +
  • -

#14 MrLuke187  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 81
  • View blog
  • Posts: 237
  • Joined: 09-July 10

Re: Questions on Implementing JComboBox with ActionListening device

Posted 21 April 2011 - 02:08 PM

The Question is: What do you need from your ComboBox? Do you need the String of the Selected Entry or do you need it's number?

Post some Code on what it's supposed to do (not only your ActionCommand), maybe I can help you get it working.

Greetings: Luke
Was This Post Helpful? 0
  • +
  • -

#15 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

Re: Questions on Implementing JComboBox with ActionListening device

Posted 22 April 2011 - 06:44 AM

Quote

working on an area solving equation where the user can pick which method he/she would like to use to solve for the area.


It needs to be able to link to whatever "if" statement that corresponds to it and execute that specified "if" statement. So if the "Sidelength" option is selectd in my JComboBox, it should read that as the "if" statement that corresponds to the "Sidelength" option selected and execute.
So it just needs to be able to recognize "hey, this option in my JComboBox corresponds to this if statement in my ActionListener" and go from there for each one. I hope that clarified it up a bit.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2