hey guys how do i change the value in a JLabel when a value in the JComboBox changes, example when i change a combo box value from airplanes to cars, the value in jlabel changes from $100k to $10k. thanks.

googled and got that link, didnt really understand how to use it. :(

this is my code and what i wanted was so that when i change the airlines, the pricing will change too.
i know i cant use if statement at the airlines part.

import java.util.Vector;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.Border;
public class TourPackagesBooking4 extends JFrame
{

        JLabel jlblDestination,jlblImage,jlblAirlines,jlblHotel,jlblPrice,jlblPrice1,jlblPrice2,jlblPrice3,jlblFood;
        JComboBox jcb1,jcb2,jcb3;
        JButton jbInfo,jbTotalPrice;
        JPanel jp1,jp2,jp3,jp4,jp31,jp32,jp33;
        JCheckBox jcbMedeterrian, jcbVegetarian, jcbHalal;
        Border[] border = new Border[]{BorderFactory.createTitledBorder("Select Destination"),BorderFactory.createTitledBorder("Select Airlines"),BorderFactory.createTitledBorder("Select Hotel"),BorderFactory.createTitledBorder("Total Price"),BorderFactory.createTitledBorder("Special Requirements")};
        String country,airlines;                                
        int i = 1;      

public TourPackagesBooking4() 
        {
        setSize(650,700);
        setTitle("Tour Packages Booking");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(4,4));

        JFrame frame = new JFrame();
        setLayout(new BorderLayout());

        jp2 = new JPanel(); 
        jp2.setBorder(border[0]);
        jp2.setSize(600,400);
        jp2.setLayout(new FlowLayout());   

        String[] valueStrings = {"USA","New York","Las Vegas"};
        jcb1=new JComboBox(valueStrings);
        jcb1.setSelectedIndex(0);

        jcb1.addActionListener(new ActionListener() 
        {
        public void actionPerformed(ActionEvent e) {
        JComboBox jcmbType = (JComboBox)e.getSource();
        String cmbType = (String)jcmbType.getSelectedItem();

        jlblImage.setIcon(new ImageIcon(""+cmbType.trim().toLowerCase() + ".jpg"));
            }
        });
        jlblImage = new JLabel(new ImageIcon("" +
                                   valueStrings[jcb1.getSelectedIndex()] +
                                   ".jpg"));


        jbInfo = new JButton("Get Information");
        jbInfo.addActionListener(new InfoHandler());

        jp2.add(jcb1);
        jp2.add(jlblImage);
        jp2.add(jbInfo);

        jp3=new JPanel();

        jp3.setLayout(new BorderLayout());

        jp31 = new JPanel();
        jp31.setLayout(new FlowLayout());
        jp31.setBorder(border[1]);


        String[] valueStrings2 = {"SIA","Tiger Airways","United Airlines"};
        jcb2=new JComboBox(valueStrings2);
        jcb2.setSelectedIndex(0);
        airlines =(String)jcb2.getSelectedItem();  
        if(airlines == "SIA")
        {
        jlblPrice1 = new JLabel("Add $700");
        }
        if(airlines == "Tiger Airways")
        {
        jlblPrice1 = new JLabel("Add $30");
        }


        jp31.add(jcb2);
        jp31.add(jlblPrice1);

        jp32 = new JPanel();
        jp32.setLayout(new FlowLayout());
        jp32.setBorder(border[2]);

        jlblHotel = new JLabel("Select Hotel");
        String[] valueStrings3 = {"Hyatt Hotel","New York Hotel","Budget Hotel"};
        jcb3=new JComboBox(valueStrings3);
        jcb3.setSelectedIndex(0);
        jlblPrice2 = new JLabel("Add $50");

        jp32.add(jlblHotel);
        jp32.add(jcb3);
        jp32.add(jlblPrice2);

        jp33 = new JPanel();
        jp33.setLayout(new FlowLayout());
        jp33.setBorder(border[3]);

        jlblPrice = new JLabel("Total Price");
        jbTotalPrice = new JButton("Total Price");
        jlblPrice3 = new JLabel("Add $2800");

        jp33.add(jbTotalPrice);
        jp33.add(jlblPrice3);

        jp3.add(jp31, BorderLayout.WEST);
        jp3.add(jp32, BorderLayout.CENTER);
        jp3.add(jp33, BorderLayout.EAST);

        jp4 = new JPanel();
        jp4.setBorder(border[4]);
        jp4.setSize(600,100);
        jp4.setLayout(new FlowLayout());
        jlblFood = new JLabel("Food");
        jcbVegetarian = new JCheckBox("Vegetarian");
        jcbVegetarian.addActionListener(new PromoHandler());
        jcbMedeterrian = new JCheckBox("Medeterrian");
        jcbMedeterrian.addActionListener(new PromoHandler());
        jcbHalal = new JCheckBox("Halal");
        jcbHalal.addActionListener(new PromoHandler());

        jp4.add(jlblFood);
        jp4.add(jcbVegetarian);
        jp4.add(jcbMedeterrian);
        jp4.add(jcbHalal);


        add(jp2, BorderLayout.NORTH);
        add(jp3, BorderLayout.CENTER);
        add(jp4, BorderLayout.SOUTH);

        setVisible(true);
    } 

class InfoHandler implements ActionListener
    {               
        public void actionPerformed(ActionEvent evt)

        {

        String valueStrings = (String)jcb1.getSelectedItem();
        country =(String)jcb1.getSelectedItem();

        if(country == "New York")
        {
        String strDisplay = "Itinerary of this package to New York is : Day 1 : Visit statue of Liberty";
        JOptionPane.showMessageDialog(null,strDisplay,"", JOptionPane.PLAIN_MESSAGE);       
        }
        if(country == "USA")
        {
        String strDisplay = "Itinerary of this package to USA is : Day 1 : Visit the White House";
        JOptionPane.showMessageDialog(null,strDisplay,"", JOptionPane.PLAIN_MESSAGE);       
        }
            if(country == "Las Vegas")
        {
        String strDisplay = "Itinerary of this package to Las Vegas is : Day 1 : Visit the Integrated Resort(Casino)";
        JOptionPane.showMessageDialog(null,strDisplay,"", JOptionPane.PLAIN_MESSAGE);       
        }       
    }
    }   



class PromoHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent evt)
        {

            if(evt.getSource() == jcbVegetarian)
            {
                String strMsg = "You Have Choosen : Vegetarian Food.";
                String strTitle = "Special Requirements.";

                JOptionPane.showMessageDialog(null, strMsg , strTitle, JOptionPane.PLAIN_MESSAGE);
            }

            //checking which is the source of event
            if(evt.getSource() == jcbMedeterrian)
            {
                String strMsg = "You Have Choosen : Medeterrian Food.";
                String strTitle = "Special Requirements.";

                JOptionPane.showMessageDialog(null, strMsg , strTitle, JOptionPane.PLAIN_MESSAGE);
            }
            if(evt.getSource() == jcbHalal)
                {
                String strMsg = "You Have Choosen : Halal Food.";
                String strTitle = "Special Requirements.";

                JOptionPane.showMessageDialog(null, strMsg , strTitle, JOptionPane.PLAIN_MESSAGE);
            }   
        }
    }      





    public static void main(String[] args) {
       TourPackagesBooking4 main = new TourPackagesBooking4();  

    } 

}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Please use code tags when posting code.

Also please add some comments to the code describing what it is you want the code to do.

This article has been dead for over six months. Start a new discussion instead.