segar123
asked on
JCombo Box
Hi..
I am doing a small GUI application a movie ticket vending machine..
I have this combo box...Inside the combo box are 3 Movie titles..
I want my program to trigger when a user selects a movie my program should display an image...
I have settle how to display the image...
But im not sure how to trigger...Look through the API and found fireItemStateChanged,Iteml istener... .
Could you help me on how to use these two.. When there is a change in state and action should occcr..
Thanks
I am doing a small GUI application a movie ticket vending machine..
I have this combo box...Inside the combo box are 3 Movie titles..
I want my program to trigger when a user selects a movie my program should display an image...
I have settle how to display the image...
But im not sure how to trigger...Look through the API and found fireItemStateChanged,Iteml
Could you help me on how to use these two.. When there is a change in state and action should occcr..
Thanks
Look inside the directory:
demo/jfc/SwingSet2
that comes with the JDK... The JComboBox example does exactly that (and the source code is in:
demo/jfc/SwingSet2/src/Com boBoxDemo. java
Tim
demo/jfc/SwingSet2
that comes with the JDK... The JComboBox example does exactly that (and the source code is in:
demo/jfc/SwingSet2/src/Com
Tim
Here you can find some example code about the Image (load, display, etc.)
http://www.javaalmanac.com/cgi-bin/search/find.pl?words=image
Hope this help you.
Bye, Giant.
http://www.javaalmanac.com/cgi-bin/search/find.pl?words=image
Hope this help you.
Bye, Giant.
ASKER
How about the ItemState Changed...What if the user changes option..How to trigger that..
Thanks
Thanks
yourComboBox.addItemListen er( new ItemListener()
{
public void itemStateChanged( ItemEvent e )
{
If( e.getStateChange() == ItemEvent.SELECTED )
{
System.out.println( e.getItem() + " has been selected" ) ;
}
}
}
{
public void itemStateChanged( ItemEvent e )
{
If( e.getStateChange() == ItemEvent.SELECTED )
{
System.out.println( e.getItem() + " has been selected" ) ;
}
}
}
>> What if the user changes option
You mean if the user selects another item in the combo box?
That's exactly what the actionlistener takes care of. Just try it.
You mean if the user selects another item in the combo box?
That's exactly what the actionlistener takes care of. Just try it.
Tim, why should you use an item listener (listening for ItemEvent.SELECTED) instead of an actionListener?
> Tim, why should you use an item listener (listening for ItemEvent.SELECTED) instead of an actionListener?
No reason... But he did ask :-)
No reason... But he did ask :-)
:°D
ASKER
Guys you are confusing me now...???
Who is Right??
Who is Right??
zzynx
:-)
:-)
Did you try the code of my first comment?
ASKER
Would like to confirm this..Since i got an action listener for my button too..
So is it ok to have 2 action listneners??any problems..
So is it ok to have 2 action listneners??any problems..
ASKER
zzynx your first code doesnt work..i get an error saying cannot resolve symbol at the
System.out.println("the following item is selected: " + yourComboBox.getSelectedIt em() );
System.out.println("the following item is selected: " + yourComboBox.getSelectedIt
you have to replace "yourComboBox" with the name of your combo box
;°)
>> So is it ok to have 2 action listneners?
As much as you need
;°)
>> So is it ok to have 2 action listneners?
As much as you need
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok..Thanks a lot..Its working...
I finish my program and comment if it works...
I finish my program and comment if it works...
You could create an apart class for it too:
MyListener.java :
----------------------
import java.awt.event.*;
import javax.swing.*;
public class MyListener implements ActionListener {
public MyListener() {
}
public void actionPerformed(ActionEven t evt) {
System.out.println("the following item is selected: " + ((JComboBox)evt.getSource( )).getSele ctedItem() );
}
}
Then you could write:
yourComboBox.addActionList ener( new MyListener() );
with "yourComboBox" being the name of your JComboBox variable
MyListener.java :
----------------------
import java.awt.event.*;
import javax.swing.*;
public class MyListener implements ActionListener {
public MyListener() {
}
public void actionPerformed(ActionEven
System.out.println("the following item is selected: " + ((JComboBox)evt.getSource(
}
}
Then you could write:
yourComboBox.addActionList
with "yourComboBox" being the name of your JComboBox variable
ASKER
Guys here i my codes to make my pic change where the combobox is clicked...but there seems to be no change??
selectTitleList.addActionL istener( new ActionListener() {
public void actionPerformed(ActionEven t evt) {
String pic = selectTitleList.getSelecte dItem() + ".jpg";
displayTextPanel = new JPanel(new FlowLayout(FlowLayout.CENT ER, 5, 1));
imageLabel1 = new JLabel(new ImageIcon(pic));
displayTextPanel.add(image Label1);
bookingDisplayArea = new JTextArea(12,50);
displayTextPanel.add(booki ngDisplayA rea);
}
});
my String pic..is the name of my jpg...
Please help...
selectTitleList.addActionL
public void actionPerformed(ActionEven
String pic = selectTitleList.getSelecte
displayTextPanel = new JPanel(new FlowLayout(FlowLayout.CENT
imageLabel1 = new JLabel(new ImageIcon(pic));
displayTextPanel.add(image
bookingDisplayArea = new JTextArea(12,50);
displayTextPanel.add(booki
}
});
my String pic..is the name of my jpg...
Please help...
you are not repainting the zone where you paint the image.
displayTextPanel.paintAll( ); //or repaint();
displayTextPanel.paintAll(
Better close this one and post another Q for that.
>> you are not repainting the zone where you paint the image
displayTextPanel.revalidat e();
displayTextPanel.repaint() ;
should do the job.
displayTextPanel.revalidat
displayTextPanel.repaint()
should do the job.
ASKER
Cant work..My picture doesnt change...If i change options..
zzynx, as I told before.
segar123,
try to insert a System.out.println(pic); after
String pic = selectTitleList.getSelecte dItem() + ".jpg";
In this manner you are shure you are passing from this piece of code.
segar123,
try to insert a System.out.println(pic); after
String pic = selectTitleList.getSelecte
In this manner you are shure you are passing from this piece of code.
ASKER
I have inserted the printlin...
When i choose a option...It prints correctly the name of the jpeg...
But my image does not change...
When i choose a option...It prints correctly the name of the jpeg...
But my image does not change...
ASKER
Anyone there???
displayTextPanel is visible in your Frame, Dialog, Panel (or someother Container)?
you could do with asking another question for this... 40 points for 2 questions is a little unfair :-(
Exactly.
But in the meantime he did post another Q for the image problem.
Just don't know why this Q stays open.
But in the meantime he did post another Q for the image problem.
Just don't know why this Q stays open.
Ahhhh....I haven't got round to looking at other Qs yet...just clearing my mail ;-)
hehehe
hehehe
Thanks for accepting.
P.S.
Why a B-grade?
What information do you want me to have given you to deserve an A-grade?
P.S.
Why a B-grade?
What information do you want me to have given you to deserve an A-grade?
public void actionPerformed(ActionEven
System.out.println("the following item is selected: " + yourComboBox.getSelectedIt
}
});