Solved

JCombo Box

Posted on 2004-09-01
31
195 Views
Last Modified: 2010-03-31
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,Itemlistener....

Could you help me on how to use these two.. When there is a change in state and action should occcr..

Thanks
0
Comment
Question by:segar123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 9
  • 6
  • +1
31 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 11952706
yourComboBox.addActionListener( new ActionListener() {
     public void actionPerformed(ActionEvent evt) {
          System.out.println("the following item is selected: " + yourComboBox.getSelectedItem() );
     }
});
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11952720
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/ComboBoxDemo.java

Tim
0
 
LVL 12

Expert Comment

by:Giant2
ID: 11953166
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.
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

by:segar123
ID: 11953418
How about the ItemState Changed...What if the user changes option..How to trigger that..

Thanks
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11953458
yourComboBox.addItemListener( new ItemListener()
                                                       {
                                                           public void itemStateChanged( ItemEvent e )
                                                           {
                                                               If( e.getStateChange() ==  ItemEvent.SELECTED )
                                                               {
                                                                   System.out.println( e.getItem() + " has been selected" ) ;
                                                               }
                                                           }
                                                       }
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11953475
>> 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.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11953492
Tim, why should you use an item listener (listening for ItemEvent.SELECTED) instead of an actionListener?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11953505
>  Tim, why should you use an item listener (listening for ItemEvent.SELECTED) instead of an actionListener?

No reason...  But he did ask :-)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11953526
:°D
0
 

Author Comment

by:segar123
ID: 11953549
Guys you are confusing me now...???

Who is Right??
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11953572
zzynx

:-)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11953612
Did you try the code of my first comment?
0
 

Author Comment

by:segar123
ID: 11953782
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..
0
 

Author Comment

by:segar123
ID: 11953834
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.getSelectedItem() );
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11953891
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
0
 
LVL 37

Accepted Solution

by:
zzynx earned 40 total points
ID: 11953936
In my code

new ActionListener() {
     public void actionPerformed(ActionEvent evt) {
          System.out.println("the following item is selected: " + yourComboBox.getSelectedItem() );
     }
}

is an "unamed" action listener. It is certainly not the one used by your button.
0
 

Author Comment

by:segar123
ID: 11953958
ok..Thanks a lot..Its working...

I finish my program and comment if it works...
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11954001
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(ActionEvent evt) {
       System.out.println("the following item is selected: " + ((JComboBox)evt.getSource()).getSelectedItem() );
   }
}

Then you could write:

    yourComboBox.addActionListener( new MyListener() );

with "yourComboBox" being the name of your JComboBox variable
0
 

Author Comment

by:segar123
ID: 11954099
Guys here i my codes to make my pic change where the combobox is clicked...but there seems to be no change??

selectTitleList.addActionListener( new ActionListener() {
     public void actionPerformed(ActionEvent evt) {
     
         String pic = selectTitleList.getSelectedItem() + ".jpg";
        displayTextPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 1));
        imageLabel1 = new JLabel(new ImageIcon(pic));
        displayTextPanel.add(imageLabel1);
        bookingDisplayArea = new JTextArea(12,50);
        displayTextPanel.add(bookingDisplayArea);      
                                                     
        }
});

my String pic..is the name of my jpg...

Please help...
0
 
LVL 12

Expert Comment

by:Giant2
ID: 11954322
you are not repainting the zone where you paint the image.
displayTextPanel.paintAll(); //or repaint();
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11954325
Better close this one and post another Q for that.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11954372
>> you are not repainting the zone where you paint the image

displayTextPanel.revalidate();
displayTextPanel.repaint();

should do the job.
0
 

Author Comment

by:segar123
ID: 11954409
Cant work..My picture doesnt change...If i change options..
0
 
LVL 12

Expert Comment

by:Giant2
ID: 11954444
zzynx, as I told before.


segar123,
try to insert a System.out.println(pic); after
String pic = selectTitleList.getSelectedItem() + ".jpg";

In this manner you are shure you are passing from this piece of code.
0
 

Author Comment

by:segar123
ID: 11954496
I have inserted the printlin...

When i choose a option...It prints correctly the name of the jpeg...

But my image does not change...
0
 

Author Comment

by:segar123
ID: 11954850
Anyone there???
0
 
LVL 12

Expert Comment

by:Giant2
ID: 11960684
displayTextPanel is visible in your Frame, Dialog, Panel (or someother Container)?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11961080
you could do with asking another question for this... 40 points for 2 questions is a little unfair :-(
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11961121
Exactly.
But in the meantime he did post another Q for the image problem.
Just don't know why this Q stays open.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11961132
Ahhhh....I haven't got round to looking at other Qs yet...just clearing my mail ;-)

hehehe
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11963497
Thanks for accepting.

P.S.
Why a B-grade?
What information do you want me to have given you to deserve an A-grade?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Convert from a json string array to a Java object 3 81
eclipse console opening separately 2 50
Tagging and Merging on Branch 1 70
Java ArrayList and if statement 2 55
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question