Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Error for JCombobox Editable

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Error for JCombobox Editable

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.awt.event.*;
    public class JVideo2 extends JFrame implements ItemListener
    {
    final int FRAME_WIDTH = 800; 
    final int FRAME_HEIGHT = 200; 
    int totalprice;
    int dislike = 1;
    int most = 2;
    int favorite = 3;
    JLabel label = new JLabel("Please type the movie you would like to rent and press enter. The price is as followed:"); 
    String[] movietitles = { "" ,"Taken", "Monty Python and the Holy Grail", "Ong-bak", "Ip Man", "The Goonies", "Labyrinth", "The Hobbit", "Balto", "Fairy Tail: Priestess of the Phoenix", "The NeverEnding Story"}; 
    JComboBox movietitlesbox = new JComboBox(movietitles);
     
    JTextField totPrice = new JTextField(5);
    public JVideo2() 
    { 
    super("Movies For Rent");
    movietitlesbox.addItemListener(this);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    getContentPane().setBackground(Color.black);
    label.setFont(new Font("Arial", Font.BOLD, 18)); 
    label.setForeground(Color.white);
    setLayout (new FlowLayout());
    add(movietitlesbox); 
    add(totPrice);
    add(label);
    movietitlesbox.setEditable(true);
    totPrice.setText("$" + totalprice);
    setSize(FRAME_WIDTH, FRAME_HEIGHT); 
    }
    public void itemStateChanged(ItemEvent event) {
    Object source = event.getSource();
    int select = event.getStateChange();
     
    if (movietitlesbox.getSelectedItem().equals("")) {
    totalprice = 0;
    }
    if (movietitlesbox.getSelectedItem().equals("Taken")) {
    totalprice = most;
    }
    if (movietitlesbox.getSelectedItem().equals("Monty Python and the Holy Grail")) {
    totalprice = favorite;
    }
    if (movietitlesbox.getSelectedItem().equals("Ong-bak")) {
    totalprice = dislike;
    }
    if (movietitlesbox.getSelectedItem().equals("Ip Man")) {
    totalprice = favorite;
    }
    if (movietitlesbox.getSelectedItem().equals("The Goonies")) {
    totalprice = dislike;
    }
    if (movietitlesbox.getSelectedItem().equals("Labyrinth")) {
    totalprice = favorite;
    }
    if (movietitlesbox.getSelectedItem().equals("The Hobbit")) {
    totalprice = most;
    }
    if (movietitlesbox.getSelectedItem().equals("Balto")) {
    totalprice = favorite;
    }
    if (movietitlesbox.getSelectedItem().equals("Fairy Tail: Priestess of the Phoenix")) {
    totalprice = most;
    }
    if (movietitlesbox.getSelectedItem().equals("The NeverEnding Story")) {
    totalprice = most;
    }
    totPrice.setText("$" + totalprice);
    } 
    public static void main(String[] args) 
    { 
    JVideo2 frame = new JVideo2(); 
    frame.setVisible(true); 
    }
    }
    Hello.
    I am trying to make it that if you type anything else not listed in the string within the editable JCOMBOBOX the code tells you please use one of the names of the movies located in the string. I was wondering how you go about doing that.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Error for JCombobox Editable

    A suggestion on the code: Don't have the same long String typed into more than one place. If you spell it differently, the equals() method won't work. Define one String in a variable and use the variable in all the places you need it.
    The compiler will not tell you that you have spelled it differently.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Error for JCombobox Editable

    Thank you . The code works already. I know and it would be simpler to do the way you described. However I'm wondering how I would be able to display a message when they type a movie or anything that's not listed in the string. Like an error messAge

  4. #4
    Junior Member
    Join Date
    Aug 2012
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Error for JCombobox Editable

    I cant find the exact error message its called in my book. Does anyone know how to use a catch error in a Editable JComboBox as in someone typed a string that isnt part of the ones specified.
    If so, does it have special name so that I could research it?

    My assignment altogethor is to make an application which has an editable JComboBox. Allow the user to type the name of the movie to rent.
    Display an error message if the movie is not available

    --- Update ---

    Well, I figured out how to do it. It was so simple I didn't think about it. In the if statements... i typed
    if (totalprice<1) {
    JOptionPane.showMessageDialog(null, "Please type a movie located in the box");
    }

    as well i gave totalprice a value

    int totalprice = 0;

    so simple... haha. Basically if anyone typed anything else in the editable Jcombobox it will return a value of zero cause thats the total price's default value unless one of the other if statements works.

Similar Threads

  1. Editable JTable
    By ellias2007 in forum AWT / Java Swing
    Replies: 1
    Last Post: June 2nd, 2012, 02:26 PM
  2. Non-Editable DateField in J2me
    By thiruv in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: December 22nd, 2011, 05:03 AM
  3. JTable - multiple lines in a cell AND cells not editable?
    By friday in forum AWT / Java Swing
    Replies: 3
    Last Post: January 10th, 2011, 08:44 AM
  4. JComboBox
    By nasi in forum AWT / Java Swing
    Replies: 1
    Last Post: April 29th, 2010, 08:40 AM
  5. JComboBox null pointer Exception error
    By F_Arches in forum AWT / Java Swing
    Replies: 2
    Last Post: November 29th, 2009, 02:32 PM