Results 1 to 4 of 4
  1. #1
    pravin2008 is offline Member
    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    0

    Question jComboBox and database

    Hi All,

    I am working on a application in which i hv to display a list of customer in combobox from a database, I am using derby client for database.

    The main problem is I have to display cutomer id in a combobox and cutomer name in another combobox and when I will choose a name it should be display his ID in another and vise versa.

    How it will be possible ???


    Thanks in advanced

    Pravin

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    11

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ComboComm {
        private JPanel getContent() {
            int[] ids = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            String[] names = {
                "one", "two", "three", "four", "five",
                "six", "seven", "eight", "nine", "ten"
            };
            ComboCoordinator coordinator = new ComboCoordinator(ids, names);
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.weightx = 1.0;
            panel.add(coordinator.getIdCombo(), gbc);
            panel.add(coordinator.getNameCombo(), gbc);
            return panel;
        }
    
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new ComboComm().getContent());
            f.setSize(300,140);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }
    
    class ComboCoordinator {
        int[] ids;
        String[] names;
        JComboBox idCombo;
        JComboBox nameCombo;
    
        public ComboCoordinator(int[] ids, String[] names) {
            this.ids = ids;
            this.names = names;
            idCombo = new JComboBox();
            loadCombo(ids);
            idCombo.addActionListener(idListener);
            nameCombo = new JComboBox(names);
            nameCombo.addActionListener(nameListener);
        }
    
        public JComboBox getIdCombo() { return idCombo; }
    
        public JComboBox getNameCombo() { return nameCombo; }
    
        public void setData(int[] ids, String[] names) {
            if(ids.length != names.length) {
                throw new IllegalArgumentException("ids and " +
                              "names arrays must be same length");
            }
            this.ids = ids;
            this.names = names;
            idCombo.removeAll();
            nameCombo.removeAll();
            for(int i = 0; i < ids.length; i++) {
                idCombo.addItem(Integer.valueOf(ids[i]));
                nameCombo.addItem(names[i]);
            }
        }
    
        private void loadCombo(int[] array) {
            for(int i = 0; i < array.length; i++) {
                idCombo.addItem(Integer.valueOf(array[i]));
            }
        }
    
        private ActionListener idListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int id = ((Integer)idCombo.getSelectedItem()).intValue();
                int index = getIndex(id);
                nameCombo.setSelectedIndex(index);
            }
        };
    
        private ActionListener nameListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String name = (String)nameCombo.getSelectedItem();
                int index = getIndex(name);
                idCombo.setSelectedIndex(index);
            }
        };
    
        private int getIndex(int id) {
            for(int i = 0; i < ids.length; i++) {
                if(ids[i] == id) {
                    return i;
                }
            }
            return -1;
        }
    
        private int getIndex(String name) {
            for(int i = 0; i < names.length; i++) {
                if(names[i].equals(name)) {
                    return i;
                }
            }
            return -1;
        }
    }

  3. #3
    pravin2008 is offline Member
    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    0

    Default

    Thanks to HardWired.

  4. #4
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    10

    Default

    You may now mark this thread as solved.....
    freedom exists in the world of ideas

Similar Threads

  1. Interesting topic in JComboBox
    By success21061985 in forum NetBeans
    Replies: 0
    Last Post: 08-21-2008, 12:43 PM
  2. JComboBox
    By Fosters in forum AWT / Swing
    Replies: 0
    Last Post: 08-10-2008, 01:22 PM
  3. JComboBox setDisabledTextColor
    By Jack in forum AWT / Swing
    Replies: 2
    Last Post: 07-02-2007, 05:32 AM
  4. Help with jComboBox
    By Marcus in forum AWT / Swing
    Replies: 2
    Last Post: 07-01-2007, 11:08 PM
  5. jcombobox
    By Freddie in forum AWT / Swing
    Replies: 4
    Last Post: 05-11-2007, 12:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •