Error for JCombobox Editable
Code :
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.
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.
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
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
Code :
if (totalprice<1) {
JOptionPane.showMessageDialog(null, "Please type a movie located in the box");
}
as well i gave totalprice a value
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.