Jump to content

ComboBox change

- - - - -

  • Please log in to reply
3 replies to this topic

#1
xle_camry

xle_camry

    Programmer

  • Members
  • PipPipPipPip
  • 141 posts
Hi guys!

Can you help me please:

I have a combo box and 3 items there: Text1, Text2, Text3.

Default is Text1.

If I change from Text1 to Text2 or to Text3, button at the bottom must change its color.

Any change must show in the button by color changing.

Thanks.

#2
cdg10620

cdg10620

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 389 posts
You mean something like this?

<html>
	<head>
		<script type="text/javascript">
			function doSomething()
			{
				var colors = new Array( "Red", "Green", "Blue", "Orange" );
				var dropDown = document.getElementById("selectId");
				
				dropDown.style.backgroundColor = colors[dropDown.selectedIndex];
			}
		</script>
	</head>
<body>


	<form>
		<select id="selectId" name="cars" onChange="doSomething()">
			<option value="volvo">Volvo</option>
			<option value="saab">Saab</option>
			<option value="fiat" selected="selected">Fiat</option>
			<option value="audi">Audi</option>
		</select>
	</form>


</body>
</html>

-CDG10620
Software Developer

#3
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
I take it since this was posted in the Java forum, he is talking about combo boxes in either Swing or SWT.

What you are going to need to do is add an ActionListener to your combo box. JComboBox fires an ActionEvent any time the user selects an item from the list, which is what you want in your case.

Here's a very simple example of adding an ActionListener to a JComboBox:

JComboBox jCombo = new JComboBox();

... // add your selection items here.

jCombo.addActionListener(

    new ActionListener() {

        @Override

        public void actionPerformed(ActionEvent e) {

            JComboBox source = (JComboBox) e.getSource(); // This gets the source control which originated the event.

            String selection = (String) source.getSelectedItem(); // This gets the text that was selected by the user.

            ... // Now you can do something with this information here.

        }

    }

);


Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#4
xle_camry

xle_camry

    Programmer

  • Members
  • PipPipPipPip
  • 141 posts
Yep, I meant the swing. Thanks gregwarner, I understood it. :rolleyes:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users