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.


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

Results 1 to 2 of 2

Thread: JComboBox conditions?

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Thanked 0 Times in 0 Posts

    Default JComboBox conditions?

    package LockerSystem;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.SQLException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.border.TitledBorder;
    public class transfer extends JFrame {
        JComboBox cmbfrom, cmbto;
        JLabel lblfrom, lblto;
        JPanel Transfer;
        JButton btnok,btncancel;
        public transfer() {
            Transfer = new JPanel(new FlowLayout());
            Transfer.setBounds(5, 15, 275, 120);
            Transfer.setBorder(BorderFactory.createTitledBorder(null, "Transfer Locker", TitledBorder.LEFT, TitledBorder.TOP, Font.getFont("batang", null), Color.BLACK));
            lblfrom = new JLabel("From:");
            lblfrom.setBounds(50, 30, 70, 25);
            lblto = new JLabel("To:");
            lblto.setBounds(50, 70, 70, 25);
            final String[] program1 = {"Locker 1", "Locker 2", "Locker 3", "Locker 4", "Locker 5"};
            cmbfrom = new JComboBox(program1);
            cmbfrom.setBounds(110, 30, 100, 25);
            cmbto = new JComboBox(program1);
            cmbto.setBounds(110, 70, 100, 25);
            btnok = new JButton("Ok");
            btnok.setBounds(110, 140, 80, 25);
            btncancel = new JButton("Cancel");
            btncancel.setBounds(195, 140, 80, 25);
            Container cp = this.getContentPane();
        public void run() {
            setBounds(0, 0, 300, 220);
            setTitle("Administrative Tools");
        public static void main(String[] main) {
            transfer t1 = new transfer();

    My problem is how do you do it when I select item at "FROM : [comboBox]", that item I selected not listed in "TO : [comboBox]".

    For example if i select "Locker 1" at the upper combobox, only the items "Locker 2", "Locker 3", "Locker 4", "Locker 5" will be available items to be selected at the lower combobox.

  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    My Mood
    Thanked 140 Times in 135 Posts

    Default Re: JComboBox conditions?

    You would change the combo box's model -- remove that String from the second JComboBox's model. Another way is to give your second JComboBox a custom renderer, and in that renderer, make a cell's foreground color a color that makes it look disabled if it is the same String as was chosen by the first JComboBox. I'm not sure how to prevent the user from selecting it though.

Similar Threads

  1. Can you help me decipher what these two conditions mean in pseudocode?
    By divebomb33 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 2nd, 2012, 07:49 PM
  2. Conditions and 'might not have been initialized' error.
    By mwebb in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 2nd, 2011, 11:22 AM
  3. Loop conditions not working
    By bmxershane in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 1st, 2011, 02:25 AM
  4. Hangman help. Comparing arrays and conditions.
    By javanewbie1 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 18th, 2011, 09:26 AM
  5. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM