Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #2288 > unrolled thread

How to implement jcombobox with name value pair from database?

Started byrogerdoger777 <kedward777@gmail.com>
First post2012-11-29 09:14 -0800
Last post2012-11-29 13:17 -0800
Articles 8 — 5 participants

Back to article view | Back to comp.lang.java.help


Contents

  How to implement jcombobox with name value pair from database? rogerdoger777 <kedward777@gmail.com> - 2012-11-29 09:14 -0800
    Re: How to implement jcombobox with name value pair from database? kedward777@gmail.com - 2012-11-29 11:21 -0800
      Re: How to implement jcombobox with name value pair from database? Roedy Green <see_website@mindprod.com.invalid> - 2012-12-04 18:08 -0800
    Re: How to implement jcombobox with name value pair from database? Knute Johnson <nospam@knutejohnson.com> - 2012-11-29 12:55 -0800
      Re: How to implement jcombobox with name value pair from database? Lew <lewbloch@gmail.com> - 2012-11-29 13:12 -0800
        Re: How to implement jcombobox with name value pair from database? Knute Johnson <nospam@knutejohnson.com> - 2012-11-29 15:05 -0800
          Re: How to implement jcombobox with name value pair from database? Lew <lewbloch@gmail.com> - 2012-11-29 16:08 -0800
    Re: How to implement jcombobox with name value pair from database? kedward777@gmail.com - 2012-11-29 13:17 -0800

#2288 — How to implement jcombobox with name value pair from database?

Fromrogerdoger777 <kedward777@gmail.com>
Date2012-11-29 09:14 -0800
SubjectHow to implement jcombobox with name value pair from database?
Message-ID<9b70426c-e378-4852-bc77-fc67440a93ef@googlegroups.com>
Hello,

I have database values for name and employee id:
John 123
David 222
Kevin 444

In my app I have the employee id, and I need to update the jcombobox to make the person with employee id 222 the selected item.... HOW?

mycombobox.setSeletedIndex(222) should return David, but instead it returns the 222nd array element in the pull down....

Thanks for any and all help!

[toc] | [next] | [standalone]


#2289

Fromkedward777@gmail.com
Date2012-11-29 11:21 -0800
Message-ID<9d60511c-a776-4861-a7d6-94f8620e92f4@googlegroups.com>
In reply to#2288
        Why won't this work?????


class Employee  
    {  
        private int id;  
        private String name;  
  
        public Employee(int id, String name)  
        {  
            this.id = id;  
            this.name = name;  
        }  
  
        public int getId()  
        {  
            return id;  
        }  
  
        public String getName()  
        {  
            return name;  
        }  
  
        public String toString()  
        {  
            return name;  
        }  
    }  

        Vector model = new Vector();
        model.addElement(new Employee(1111, "me1" ));
        model.addElement(new Employee(1112, "me2" ));
        model.addElement(new Employee(1113, "me3" ));
        model.addElement(new Employee(1114, "me4" ));
        model.addElement(new Employee(1115, "me5" ));
        
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(model));
       
       Employee myItem = new Employee(1114,"me4");
       jComboBox1.setSelectedItem(myItem);

[toc] | [prev] | [next] | [standalone]


#2323

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-12-04 18:08 -0800
Message-ID<e2btb89d3137hilec80mbtm19tppbtfdhi@4ax.com>
In reply to#2289
On Thu, 29 Nov 2012 11:21:24 -0800 (PST), kedward777@gmail.com wrote,
quoted or indirectly quoted someone who said :

       Employee x = new Employee(1114, "me4" );
       Employee y = new Employee(1114, "me4" );
        x == y false. There are two different objects.
        x.equals( y ) maybe. it depends on how equals is defined.



-- 
Roedy Green Canadian Mind Products http://mindprod.com
Students who hire or con others to do their homework are as foolish 
as couch potatoes who hire others to go to the gym for them. 

[toc] | [prev] | [next] | [standalone]


#2292

FromKnute Johnson <nospam@knutejohnson.com>
Date2012-11-29 12:55 -0800
Message-ID<k98i3b$bgi$1@dont-email.me>
In reply to#2288
On 11/29/2012 9:14 AM, rogerdoger777 wrote:
> Hello,
>
> I have database values for name and employee id:
> John 123
> David 222
> Kevin 444
>
> In my app I have the employee id, and I need to update the jcombobox to make the person with employee id 222 the selected item.... HOW?
>
> mycombobox.setSeletedIndex(222) should return David, but instead it returns the 222nd array element in the pull down....
>
> Thanks for any and all help!
>

There are lots of ways to do this.  Here is one simple possibility.

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;

public class test extends JPanel {
     private final Vector<Employee> v = new Vector<>();
     private final Map<Integer,Employee> map = new HashMap<>();
     private final JComboBox<Employee> box;
     private final JTextField f;

     public test() {
         Employee bob = new Employee(12,"Bob");
         Employee jerry = new Employee(65,"Jerry");
         Employee jane = new Employee(70,"Jane");
         Employee sharon = new Employee(82,"Sharon");
         Employee anne = new Employee(23,"Anne");

         map.put(bob.getNumber(),bob);
         map.put(jerry.getNumber(),jerry);
         map.put(jane.getNumber(),jane);
         map.put(sharon.getNumber(),sharon);
         map.put(anne.getNumber(),anne);

         v.addAll(map.values());
         Collections.sort(v);

         box = new JComboBox<Employee>(v);
         add(box);

         f = new JTextField("",5);
         add(f);

         JButton b = new JButton("Find");
         b.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent ae) {
                 boolean found = false;
                 try {
                     int number = Integer.parseInt(f.getText().trim());
                     Employee emp = map.get(number);
                     if (emp == null)
                         JOptionPane.showMessageDialog(test.this,
                          "Employee Doesn't Exist",
                          "Error!",
                          JOptionPane.ERROR_MESSAGE);
                     box.setSelectedItem(emp);
                 } catch (NumberFormatException nfe) {
                     JOptionPane.showMessageDialog(test.this,nfe);
                 }
             }
         });
         add(b);
     }

     public class Employee implements Comparable {
         private int number;
         private String name;

         public Employee(int number, String name) {
             this.number = number;
             this.name = name;
         }

         public String getName() {
             return name;
         }

         public int getNumber() {
             return number;
         }

         public int compareTo(Object emp) {
             return getName().compareTo(((Employee)emp).getName());
         }

         public String toString() {
             return String.format("%s %d",getName(),getNumber());
         }
     }

     public static void main(String[] args) {
         EventQueue.invokeLater(new Runnable() {
             public void run() {
                 JFrame f = new JFrame("test");
                 f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                 test t = new test();
                 f.add(t,BorderLayout.CENTER);
                 f.pack();
                 f.setVisible(true);
             }
         });
     }
}


-- 

Knute Johnson

[toc] | [prev] | [next] | [standalone]


#2293

FromLew <lewbloch@gmail.com>
Date2012-11-29 13:12 -0800
Message-ID<aa3c6fd2-9566-400d-8df2-3525550f2cd9@googlegroups.com>
In reply to#2292
Knute Johnson wrote:
>      public class Employee implements Comparable {
>          private int number;
>          private String name;
> 
>          public Employee(int number, String name) {
>              this.number = number;
>              this.name = name;
>          }
> 
>          public String getName() {
>              return name;
>          }
> 
>          public int getNumber() {
>              return number;
>          }
> 
>          public int compareTo(Object emp) {
>              return getName().compareTo(((Employee)emp).getName());
>          }
> 
>          public String toString() {
>              return String.format("%s %d",getName(),getNumber());
>          }
>      }

'Employee' needs to override 'equals()' to be consistent with 'compareTo()'.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#2295

FromKnute Johnson <nospam@knutejohnson.com>
Date2012-11-29 15:05 -0800
Message-ID<k98pnk$qb7$1@dont-email.me>
In reply to#2293
On 11/29/2012 1:12 PM, Lew wrote:
> Knute Johnson wrote:
>>       public class Employee implements Comparable {
>>           private int number;
>>           private String name;
>>
>>           public Employee(int number, String name) {
>>               this.number = number;
>>               this.name = name;
>>           }
>>
>>           public String getName() {
>>               return name;
>>           }
>>
>>           public int getNumber() {
>>               return number;
>>           }
>>
>>           public int compareTo(Object emp) {
>>               return getName().compareTo(((Employee)emp).getName());
>>           }
>>
>>           public String toString() {
>>               return String.format("%s %d",getName(),getNumber());
>>           }
>>       }
>
> 'Employee' needs to override 'equals()' to be consistent with 'compareTo()'.

 From what I can read in the docs Employee only needs to be "consistent 
with equals" if it is to be used in a Set or as a key in SortedMap.  I'm 
not using Sets.  Employees are values in the HashMap and so neither 
compareTo() nor equals() would be called if the map were sorted. 
Collections.sort() uses the compareTo() to accomplish the sort and 
doesn't need equals() as far as I can tell.

So maybe it would have been better to create a Comparator instead of 
making Employee Comparable.  Employee would still need an equals() 
method if it were to be used in a Set.

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;

public class test extends JPanel {
     private final Vector<Employee> v = new Vector<>();
     private final Map<Integer,Employee> map = new HashMap<>();
     private final JComboBox<Employee> box;
     private final JTextField f;

     public test() {
         Employee bob = new Employee(12,"Bob");
         Employee jerry = new Employee(65,"Jerry");
         Employee jane = new Employee(70,"Jane");
         Employee sharon = new Employee(82,"Sharon");
         Employee anne = new Employee(23,"Anne");

         map.put(bob.getNumber(),bob);
         map.put(jerry.getNumber(),jerry);
         map.put(jane.getNumber(),jane);
         map.put(sharon.getNumber(),sharon);
         map.put(anne.getNumber(),anne);

         v.addAll(map.values());
         Collections.sort(v,new EmployeeNameComparator());

         box = new JComboBox<Employee>(v);
         add(box);

         f = new JTextField("",5);
         add(f);

         JButton b = new JButton("Find");
         b.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent ae) {
                 boolean found = false;
                 try {
                     int number = Integer.parseInt(f.getText().trim());
                     Employee emp = map.get(number);
                     if (emp == null)
                         JOptionPane.showMessageDialog(test.this,
                          "Employee Doesn't Exist",
                          "Error!",
                          JOptionPane.ERROR_MESSAGE);
                     box.setSelectedItem(emp);
                 } catch (NumberFormatException nfe) {
                     JOptionPane.showMessageDialog(test.this,nfe);
                 }
             }
         });
         add(b);
     }

     public class Employee {
         private int number;
         private String name;

         public Employee(int number, String name) {
             this.number = number;
             this.name = name;
         }

         public String getName() {
             return name;
         }

         public int getNumber() {
             return number;
         }

         public String toString() {
             return String.format("%s %d",getName(),getNumber());
         }
     }

     public class EmployeeNameComparator implements Comparator<Employee> {
         public int compare(Employee emp1, Employee emp2) {
             return emp1.getName().compareTo(emp2.getName());
         }
     }

     public static void main(String[] args) {
         EventQueue.invokeLater(new Runnable() {
             public void run() {
                 JFrame f = new JFrame("test");
                 f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                 test t = new test();
                 f.add(t,BorderLayout.CENTER);
                 f.pack();
                 f.setVisible(true);
             }
         });
     }
}

-- 

Knute Johnson

[toc] | [prev] | [next] | [standalone]


#2297

FromLew <lewbloch@gmail.com>
Date2012-11-29 16:08 -0800
Message-ID<77e23a07-f99f-4c04-b4f7-88f0e4b099e7@googlegroups.com>
In reply to#2295
Knute Johnson wrote:
> Lew wrote:
>> 'Employee' needs to override 'equals()' to be consistent with 'compareTo()'.
> 
>  From what I can read in the docs Employee only needs to be "consistent 
> with equals" if it is to be used in a Set or as a key in SortedMap.  I'm 

This is fine if 'Employee' never leaks outside its current use.

> not using Sets.  Employees are values in the HashMap and so neither 

So far.
 
> compareTo() nor equals() would be called if the map were sorted. 
> Collections.sort() uses the compareTo() to accomplish the sort and 
> doesn't need equals() as far as I can tell.
> 
> So maybe it would have been better to create a Comparator instead of 

Yes. The problem with internal inconsistency in the class is that it is fragile 
the instant you expand the type's use. With an external 'Comparator' that does not 
happen.

> making Employee Comparable.  Employee would still need an equals() 
> method if it were to be used in a Set.

Or any other context where you don't want identity equality.

There was another recent question in this forum that looks like someone fell afoul 
of the 'equals()'/'==' dichotomy.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#2294

Fromkedward777@gmail.com
Date2012-11-29 13:17 -0800
Message-ID<ef7d26ea-68b5-469f-9820-f0cdf79888d8@googlegroups.com>
In reply to#2288
Perfect! Thank YOU Knute! You  rock!

On Thursday, November 29, 2012 12:14:12 PM UTC-5, rogerdoger777 wrote:
> Hello,
> 
> 
> 
> I have database values for name and employee id:
> 
> John 123
> 
> David 222
> 
> Kevin 444
> 
> 
> 
> In my app I have the employee id, and I need to update the jcombobox to make the person with employee id 222 the selected item.... HOW?
> 
> 
> 
> mycombobox.setSeletedIndex(222) should return David, but instead it returns the 222nd array element in the pull down....
> 
> 
> 
> Thanks for any and all help!

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.help


csiph-web